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>
|
template <class T>
|
||||||
class TreeNode {
|
class TreeNode {
|
||||||
public:
|
public:
|
||||||
TreeNode() : left(NULL), right(NULL) {}
|
T key;
|
||||||
TreeNode(const T& init) : value(init), left(NULL), right(NULL) {}
|
TreeNode* left;
|
||||||
T value;
|
TreeNode* right;
|
||||||
TreeNode* left;
|
// constructor
|
||||||
TreeNode* right;
|
TreeNode(const T& k) {
|
||||||
|
key = k;
|
||||||
|
left = NULL;
|
||||||
|
right = NULL;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
template <class T> class ds_set;
|
// internally it's a binary search tree
|
||||||
|
|
||||||
// -------------------------------------------------------------------
|
|
||||||
// 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
|
|
||||||
template <class T>
|
template <class T>
|
||||||
class ds_set {
|
class ds_set {
|
||||||
public:
|
public:
|
||||||
ds_set() : root_(NULL), size_(0) {}
|
ds_set(){
|
||||||
ds_set(const ds_set<T>& old) : size_(old.size_) {
|
root = NULL;
|
||||||
root_ = this->copy_tree(old.root_); }
|
m_size = 0;
|
||||||
~ds_set() { this->destroy_tree(root_); root_ = NULL; }
|
}
|
||||||
ds_set& operator=(const ds_set<T>& old) {
|
int size() { return m_size; }
|
||||||
if (&old != this) {
|
bool find(const T& key){
|
||||||
this->destroy_tree(root_);
|
return find(key, root);
|
||||||
root_ = this->copy_tree(old.root_);
|
}
|
||||||
size_ = old.size_;
|
void insert(const T& key){
|
||||||
}
|
if(insert(key, root) == true){
|
||||||
return *this;
|
++m_size;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
typedef tree_iterator<T> iterator;
|
private:
|
||||||
iterator find(const T& key_value) { return find(key_value, root_); }
|
TreeNode<T>* root;
|
||||||
std::pair< iterator, bool > insert(T const& key_value) { return insert(key_value, root_); }
|
int m_size;
|
||||||
// ITERATORS
|
bool find(const T& key, TreeNode<T>* root);
|
||||||
iterator begin() const {
|
bool insert(const T& key, TreeNode<T>*& root);
|
||||||
}
|
|
||||||
|
|
||||||
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 */ }
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#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