use the lecture version
This commit is contained in:
committed by
JamesFlare1212
parent
62611ff8c8
commit
45ee58bda2
@@ -1,79 +1,73 @@
|
||||
// Partial implementation of binary-tree based set class similar to std::set.
|
||||
// The iterator increment & decrement operations have been omitted.
|
||||
#ifndef ds_set_h_
|
||||
#define ds_set_h_
|
||||
#include <iostream>
|
||||
#include <utility>
|
||||
|
||||
// -------------------------------------------------------------------
|
||||
// TREE NODE CLASS
|
||||
template <class T>
|
||||
class TreeNode {
|
||||
public:
|
||||
TreeNode() : left(NULL), right(NULL) {}
|
||||
TreeNode(const T& init) : value(init), left(NULL), right(NULL) {}
|
||||
T value;
|
||||
T key;
|
||||
TreeNode* left;
|
||||
TreeNode* right;
|
||||
// constructor
|
||||
TreeNode(const T& k) {
|
||||
key = k;
|
||||
left = NULL;
|
||||
right = NULL;
|
||||
}
|
||||
};
|
||||
|
||||
template <class T> class ds_set;
|
||||
|
||||
// -------------------------------------------------------------------
|
||||
// TREE NODE ITERATOR CLASS
|
||||
template <class T>
|
||||
class tree_iterator {
|
||||
public:
|
||||
tree_iterator() : ptr_(NULL) {}
|
||||
tree_iterator(TreeNode<T>* p) : ptr_(p) {}
|
||||
tree_iterator(const tree_iterator& old) : ptr_(old.ptr_) {}
|
||||
~tree_iterator() {}
|
||||
tree_iterator& operator=(const tree_iterator& old) { ptr_ = old.ptr_; return *this; }
|
||||
// operator* gives constant access to the value at the pointer
|
||||
const T& operator*() const { return ptr_->value; }
|
||||
// comparison operators are straightforward
|
||||
bool operator==(const tree_iterator& r) { return ptr_ == r.ptr_; }
|
||||
bool operator!=(const tree_iterator& r) { return ptr_ != r.ptr_; }
|
||||
|
||||
private:
|
||||
// representation
|
||||
TreeNode<T>* ptr_;
|
||||
};
|
||||
|
||||
// -------------------------------------------------------------------
|
||||
// DS SET CLASS
|
||||
// internally it's a binary search tree
|
||||
template <class T>
|
||||
class ds_set {
|
||||
public:
|
||||
ds_set() : root_(NULL), size_(0) {}
|
||||
ds_set(const ds_set<T>& old) : size_(old.size_) {
|
||||
root_ = this->copy_tree(old.root_); }
|
||||
~ds_set() { this->destroy_tree(root_); root_ = NULL; }
|
||||
ds_set& operator=(const ds_set<T>& old) {
|
||||
if (&old != this) {
|
||||
this->destroy_tree(root_);
|
||||
root_ = this->copy_tree(old.root_);
|
||||
size_ = old.size_;
|
||||
ds_set(){
|
||||
root = NULL;
|
||||
m_size = 0;
|
||||
}
|
||||
return *this;
|
||||
int size() { return m_size; }
|
||||
bool find(const T& key){
|
||||
return find(key, root);
|
||||
}
|
||||
void insert(const T& key){
|
||||
if(insert(key, root) == true){
|
||||
++m_size;
|
||||
}
|
||||
|
||||
typedef tree_iterator<T> iterator;
|
||||
iterator find(const T& key_value) { return find(key_value, root_); }
|
||||
std::pair< iterator, bool > insert(T const& key_value) { return insert(key_value, root_); }
|
||||
// ITERATORS
|
||||
iterator begin() const {
|
||||
}
|
||||
|
||||
iterator end() const { return iterator(NULL); }
|
||||
|
||||
private:
|
||||
// REPRESENTATION
|
||||
TreeNode<T>* root_;
|
||||
int size_;
|
||||
iterator find(const T& key_value, TreeNode<T>* p) { /* to be implemented */ }
|
||||
std::pair<iterator,bool> insert(const T& key_value, TreeNode<T>*& p) { /* to be implemented */ }
|
||||
void destroy_tree(TreeNode<T>* p) { /* to be implemented */ }
|
||||
TreeNode<T>* root;
|
||||
int m_size;
|
||||
bool find(const T& key, TreeNode<T>* root);
|
||||
bool insert(const T& key, TreeNode<T>*& root);
|
||||
};
|
||||
|
||||
#endif
|
||||
template <class T>
|
||||
bool ds_set<T>::find(const T& key, TreeNode<T>* root){
|
||||
// base case (if root doesn't even exist)
|
||||
if(root == NULL){
|
||||
return false;
|
||||
}
|
||||
|
||||
// general case
|
||||
if(key < root->key){
|
||||
return find(key, root->left);
|
||||
}else if(key > root->key){
|
||||
return find(key, root->right);
|
||||
}else{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
template <class T>
|
||||
bool ds_set<T>::insert(const T& key, TreeNode<T>*& root){
|
||||
// base case (if root doesn't even exist)
|
||||
if(root == NULL){
|
||||
// make the first node.
|
||||
root = new TreeNode<T>(key);
|
||||
return true;
|
||||
}
|
||||
|
||||
// general case
|
||||
if(key < root->key){
|
||||
return insert(key, root->left);
|
||||
}else if(key > root->key){
|
||||
return insert(key, root->right);
|
||||
}else{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user