create set main and ds set main
This commit is contained in:
@@ -1,183 +0,0 @@
|
|||||||
// 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;
|
|
||||||
TreeNode* left;
|
|
||||||
TreeNode* right;
|
|
||||||
};
|
|
||||||
|
|
||||||
// -------------------------------------------------------------------
|
|
||||||
// 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; }
|
|
||||||
// comparions operators are straightforward
|
|
||||||
bool operator== (const tree_iterator& rgt) { return ptr_ == rgt.ptr_; }
|
|
||||||
bool operator!= (const tree_iterator& rgt) { return ptr_ != rgt.ptr_; }
|
|
||||||
|
|
||||||
private:
|
|
||||||
// representation
|
|
||||||
TreeNode<T>* ptr_;
|
|
||||||
};
|
|
||||||
|
|
||||||
// -------------------------------------------------------------------
|
|
||||||
// DS_SET CLASS
|
|
||||||
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_;
|
|
||||||
}
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
typedef tree_iterator<T> iterator;
|
|
||||||
|
|
||||||
int size() const { return size_; }
|
|
||||||
bool operator==(const ds_set<T>& old) const { return (old.root_ == this->root_); }
|
|
||||||
|
|
||||||
// FIND, INSERT & ERASE
|
|
||||||
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_); }
|
|
||||||
int erase(T const& key_value) { return erase(key_value, root_); }
|
|
||||||
|
|
||||||
// OUTPUT & PRINTING
|
|
||||||
friend std::ostream& operator<< (std::ostream& ostr, const ds_set<T>& s) {
|
|
||||||
s.print_in_order(ostr, s.root_);
|
|
||||||
return ostr;
|
|
||||||
}
|
|
||||||
void print_as_sideways_tree(std::ostream& ostr) const {
|
|
||||||
print_as_sideways_tree(ostr, root_, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
// ITERATORS
|
|
||||||
iterator begin() const {
|
|
||||||
if (!root_) return iterator(NULL);
|
|
||||||
TreeNode<T>* p = root_;
|
|
||||||
while (p->left) p = p->left;
|
|
||||||
return iterator(p);
|
|
||||||
}
|
|
||||||
iterator end() const { return iterator(NULL); }
|
|
||||||
|
|
||||||
private:
|
|
||||||
// REPRESENTATION
|
|
||||||
TreeNode<T>* root_;
|
|
||||||
int size_;
|
|
||||||
|
|
||||||
// PRIVATE HELPER FUNCTIONS
|
|
||||||
TreeNode<T>* copy_tree(TreeNode<T>* old_root) {
|
|
||||||
if (old_root == NULL)
|
|
||||||
return NULL;
|
|
||||||
TreeNode<T> *answer = new TreeNode<T>();
|
|
||||||
answer->value = old_root->value;
|
|
||||||
answer->left = copy_tree(old_root->left);
|
|
||||||
answer->right = copy_tree(old_root->right);
|
|
||||||
return answer;
|
|
||||||
}
|
|
||||||
|
|
||||||
void destroy_tree(TreeNode<T>* p) {
|
|
||||||
if (!p) return;
|
|
||||||
destroy_tree(p->right);
|
|
||||||
destroy_tree(p->left);
|
|
||||||
delete p;
|
|
||||||
}
|
|
||||||
|
|
||||||
iterator find(const T& key_value, TreeNode<T>* p) {
|
|
||||||
if (!p) return iterator(NULL);
|
|
||||||
if (p->value > key_value)
|
|
||||||
return find(key_value, p->left);
|
|
||||||
else if (p->value < key_value)
|
|
||||||
return find(key_value, p->right);
|
|
||||||
else
|
|
||||||
return iterator(p);
|
|
||||||
}
|
|
||||||
|
|
||||||
std::pair<iterator,bool> insert(const T& key_value, TreeNode<T>*& p) {
|
|
||||||
if (!p) {
|
|
||||||
p = new TreeNode<T>(key_value);
|
|
||||||
this->size_++;
|
|
||||||
return std::pair<iterator,bool>(iterator(p), true);
|
|
||||||
}
|
|
||||||
else if (key_value < p->value)
|
|
||||||
return insert(key_value, p->left);
|
|
||||||
else if (key_value > p->value)
|
|
||||||
return insert(key_value, p->right);
|
|
||||||
else
|
|
||||||
return std::pair<iterator,bool>(iterator(p), false);
|
|
||||||
}
|
|
||||||
|
|
||||||
int erase(T const& key_value, TreeNode<T>* &p) {
|
|
||||||
if (!p) return 0;
|
|
||||||
|
|
||||||
// look left & right
|
|
||||||
if (p->value < key_value)
|
|
||||||
return erase(key_value, p->right);
|
|
||||||
else if (p->value > key_value)
|
|
||||||
return erase(key_value, p->left);
|
|
||||||
|
|
||||||
// Found the node. Let's delete it
|
|
||||||
assert (p->value == key_value);
|
|
||||||
if (!p->left && !p->right) { // leaf
|
|
||||||
delete p; p=NULL;
|
|
||||||
} else if (!p->left) { // no left child
|
|
||||||
TreeNode<T>* q = p; p=p->right; delete q;
|
|
||||||
} else if (!p->right) { // no right child
|
|
||||||
TreeNode<T>* q = p; p=p->left; delete q;
|
|
||||||
} else { // Find rightmost node in left subtree
|
|
||||||
TreeNode<T>* &q = p->left;
|
|
||||||
while (q->right) q = q->right;
|
|
||||||
p->value = q->value;
|
|
||||||
int check = erase(q->value, q);
|
|
||||||
assert (check == 1);
|
|
||||||
}
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
void print_in_order(std::ostream& ostr, const TreeNode<T>* p) const {
|
|
||||||
if (p) {
|
|
||||||
print_in_order(ostr, p->left);
|
|
||||||
ostr << p->value << "\n";
|
|
||||||
print_in_order(ostr, p->right);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void print_as_sideways_tree(std::ostream& ostr, const TreeNode<T>* p, int depth) const {
|
|
||||||
if (p) {
|
|
||||||
print_as_sideways_tree(ostr, p->right, depth+1);
|
|
||||||
for (int i=0; i<depth; ++i) ostr << " ";
|
|
||||||
ostr << p->value << "\n";
|
|
||||||
print_as_sideways_tree(ostr, p->left, depth+1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif
|
|
||||||
@@ -1,28 +1,20 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include "ds_set_starter.h"
|
#include "ds_set_starter.h"
|
||||||
// #include <set>
|
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
// create a set of integers
|
// create a set of integers
|
||||||
std::set<int> numbers;
|
ds_set<int> numbers;
|
||||||
|
|
||||||
// insert some values into the set
|
// insert some values into the set
|
||||||
numbers.insert(10);
|
numbers.insert(10);
|
||||||
numbers.insert(5);
|
numbers.insert(5);
|
||||||
numbers.insert(20);
|
numbers.insert(20);
|
||||||
numbers.insert(15);
|
numbers.insert(15);
|
||||||
numbers.insert(5); // Duplicate value (won't be inserted)
|
numbers.insert(5); // duplicate value (won't be inserted)
|
||||||
|
|
||||||
// print the elements of the set
|
|
||||||
std::cout << "The elements in the set are:" << std::endl;
|
|
||||||
for (int num : numbers) {
|
|
||||||
std::cout << num << " ";
|
|
||||||
}
|
|
||||||
std::cout << std::endl;
|
|
||||||
|
|
||||||
// check if a specific value exists in the set
|
// check if a specific value exists in the set
|
||||||
int value = 15;
|
int value = 15;
|
||||||
if (numbers.find(value) != numbers.end()) {
|
if (numbers.find(value) == true) {
|
||||||
std::cout << value << " is found in the set." << std::endl;
|
std::cout << value << " is found in the set." << std::endl;
|
||||||
} else {
|
} else {
|
||||||
std::cout << value << " is not found in the set." << std::endl;
|
std::cout << value << " is not found in the set." << std::endl;
|
||||||
|
|||||||
31
lectures/18_trees_I/set_main.cpp
Normal file
31
lectures/18_trees_I/set_main.cpp
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#include <set>
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
// create a set of integers
|
||||||
|
std::set<int> numbers;
|
||||||
|
|
||||||
|
// insert some values into the set
|
||||||
|
numbers.insert(10);
|
||||||
|
numbers.insert(5);
|
||||||
|
numbers.insert(20);
|
||||||
|
numbers.insert(15);
|
||||||
|
numbers.insert(5); // duplicate value (won't be inserted)
|
||||||
|
|
||||||
|
// print the elements of the set
|
||||||
|
std::cout << "The elements in the set are:" << std::endl;
|
||||||
|
for (int num : numbers) {
|
||||||
|
std::cout << num << " ";
|
||||||
|
}
|
||||||
|
std::cout << std::endl;
|
||||||
|
|
||||||
|
// check if a specific value exists in the set
|
||||||
|
int value = 15;
|
||||||
|
if (numbers.find(value) != numbers.end()) {
|
||||||
|
std::cout << value << " is found in the set." << std::endl;
|
||||||
|
} else {
|
||||||
|
std::cout << value << " is not found in the set." << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -1,155 +0,0 @@
|
|||||||
// -------------------------------------------------------------------
|
|
||||||
// TREE NODE CLASS
|
|
||||||
template <class T>
|
|
||||||
class TreeNode {
|
|
||||||
public:
|
|
||||||
TreeNode() : left(NULL), right(NULL)/*, parent(NULL)*/ {}
|
|
||||||
TreeNode(const T& init) : value(init), left(NULL), right(NULL)/*, parent(NULL)*/ {}
|
|
||||||
T value;
|
|
||||||
TreeNode* left;
|
|
||||||
TreeNode* right;
|
|
||||||
// one way to allow implementation of iterator increment & decrement
|
|
||||||
// TreeNode* parent;
|
|
||||||
};
|
|
||||||
|
|
||||||
// -------------------------------------------------------------------
|
|
||||||
// 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& rgt) { return ptr_ == rgt.ptr_; }
|
|
||||||
bool operator!= (const tree_iterator& rgt) { return ptr_ != rgt.ptr_; }
|
|
||||||
// increment & decrement operators
|
|
||||||
tree_iterator<T> & operator++() { /* discussed & implemented in Lecture 18 */
|
|
||||||
// if i have right subtree, find left most element of those
|
|
||||||
if (ptr_->right_ != NULL) {
|
|
||||||
ptr_ = ptr_->right_;
|
|
||||||
while (ptr_->left != NULL) {
|
|
||||||
ptr_ = ptr_->left_;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
//TreeNode<T> *tmp = ptr_;
|
|
||||||
// Keep going up as long as I'm my parent's right child
|
|
||||||
//while (tmp->value < value ) {
|
|
||||||
while (ptr_->parent && ptr_->parent_->right == ptr_)
|
|
||||||
ptr_ = ptr_->parent_;
|
|
||||||
}
|
|
||||||
// Go up one more time
|
|
||||||
ptr_ = ptr_->parent;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
tree_iterator<T> operator++(int) { tree_iterator<T> temp(*this); ++(*this); return temp; }
|
|
||||||
tree_iterator<T> & operator--() { /* implementation omitted */ }
|
|
||||||
tree_iterator<T> operator--(int) { tree_iterator<T> temp(*this); --(*this); return temp; }
|
|
||||||
|
|
||||||
private:
|
|
||||||
// representation
|
|
||||||
TreeNode<T>* ptr_;
|
|
||||||
};
|
|
||||||
|
|
||||||
// -------------------------------------------------------------------
|
|
||||||
// DS_SET CLASS
|
|
||||||
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_,NULL); }
|
|
||||||
~ds_set() { this->destroy_tree(root_); }
|
|
||||||
ds_set& operator=(const ds_set<T>& old) { /* implementation omitted */ }
|
|
||||||
|
|
||||||
typedef tree_iterator<T> iterator;
|
|
||||||
|
|
||||||
int size() const { return size_; }
|
|
||||||
bool operator==(const ds_set<T>& old) const { return (old.root_ == this->root_); }
|
|
||||||
|
|
||||||
// FIND, INSERT & ERASE
|
|
||||||
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_); }
|
|
||||||
int erase(T const& key_value) { return erase(key_value, root_); }
|
|
||||||
|
|
||||||
// OUTPUT & PRINTING
|
|
||||||
friend std::ostream& operator<< (std::ostream& ostr, const ds_set<T>& s) {
|
|
||||||
s.print_in_order(ostr, s.root_);
|
|
||||||
return ostr;
|
|
||||||
}
|
|
||||||
|
|
||||||
// ITERATORS
|
|
||||||
iterator begin() const {
|
|
||||||
if (!root_) return iterator(NULL);
|
|
||||||
TreeNode<T>* p = root_;
|
|
||||||
while (p->left) p = p->left;
|
|
||||||
return iterator(p);
|
|
||||||
}
|
|
||||||
iterator end() const { return iterator(NULL); }
|
|
||||||
|
|
||||||
private:
|
|
||||||
// REPRESENTATION
|
|
||||||
TreeNode<T>* root_;
|
|
||||||
int size_;
|
|
||||||
|
|
||||||
// PRIVATE HELPER FUNCTIONS
|
|
||||||
TreeNode<T>* copy_tree(TreeNode<T>* old_root) { /* Implemented in Lab 9 */ }
|
|
||||||
|
|
||||||
void destroy_tree(TreeNode<T>* p) {
|
|
||||||
if (!p) return;
|
|
||||||
destroy_tree(p->left);
|
|
||||||
destroy_tree(p->right);
|
|
||||||
delete p;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*void destroy_tree(TreeNode<T>* & p) {
|
|
||||||
// Implemented in Lecture 19
|
|
||||||
if (!p) {
|
|
||||||
p = NULL;
|
|
||||||
size = 0;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
destroy_tree(p->left);
|
|
||||||
TreeNode<T>* tmp = p->right;
|
|
||||||
delete p;
|
|
||||||
destroy_tree(tmp);
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
}
|
|
||||||
|
|
||||||
iterator find(const T& key_value, TreeNode<T>* p) { /* Implemented in Lecture 17 */ }
|
|
||||||
|
|
||||||
std::pair<iterator,bool> insert(const T& key_value, TreeNode<T>*& p) {
|
|
||||||
// NOTE: will need revision to support & maintain parent pointers
|
|
||||||
if (!p) {
|
|
||||||
p = new TreeNode<T>(key_value);
|
|
||||||
this->size_++;
|
|
||||||
return std::pair<iterator,bool>(iterator(p), true);
|
|
||||||
}
|
|
||||||
else if (key_value < p->value)
|
|
||||||
return insert(key_value, p->left);
|
|
||||||
else if (key_value > p->value)
|
|
||||||
return insert(key_value, p->right);
|
|
||||||
else
|
|
||||||
return std::pair<iterator,bool>(iterator(p), false);
|
|
||||||
}
|
|
||||||
|
|
||||||
int erase(T const& key_value, TreeNode<T>* &p) { /* Implemented in Lecture 19 */ }
|
|
||||||
|
|
||||||
void print_in_order(std::ostream& ostr, const TreeNode<T>* p) const {
|
|
||||||
if (p) {
|
|
||||||
print_in_order(ostr, p->left);
|
|
||||||
ostr << p->value << "\n";
|
|
||||||
print_in_order(ostr, p->right);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
@@ -1,130 +0,0 @@
|
|||||||
// -------------------------------------------------------------------
|
|
||||||
// TREE NODE CLASS
|
|
||||||
template <class T>
|
|
||||||
class TreeNode {
|
|
||||||
public:
|
|
||||||
TreeNode() : left(NULL), right(NULL)/*, parent(NULL)*/ {}
|
|
||||||
TreeNode(const T& init) : value(init), left(NULL), right(NULL)/*, parent(NULL)*/ {}
|
|
||||||
T value;
|
|
||||||
TreeNode* left;
|
|
||||||
TreeNode* right;
|
|
||||||
// one way to allow implementation of iterator increment & decrement
|
|
||||||
// TreeNode* parent;
|
|
||||||
};
|
|
||||||
|
|
||||||
// -------------------------------------------------------------------
|
|
||||||
// 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; }
|
|
||||||
// comparions operators are straightforward
|
|
||||||
bool operator== (const tree_iterator& rgt) { return ptr_ == rgt.ptr_; }
|
|
||||||
bool operator!= (const tree_iterator& rgt) { return ptr_ != rgt.ptr_; }
|
|
||||||
// increment & decrement operators
|
|
||||||
tree_iterator<T> & operator++() { /* discussed & implemented in Lecture 19 */
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
tree_iterator<T> operator++(int) { tree_iterator<T> temp(*this); ++(*this); return temp; }
|
|
||||||
tree_iterator<T> & operator--() { /* implementation omitted */ }
|
|
||||||
tree_iterator<T> operator--(int) { tree_iterator<T> temp(*this); --(*this); return temp; }
|
|
||||||
|
|
||||||
private:
|
|
||||||
// representation
|
|
||||||
TreeNode<T>* ptr_;
|
|
||||||
};
|
|
||||||
|
|
||||||
// -------------------------------------------------------------------
|
|
||||||
// DS_SET CLASS
|
|
||||||
template <class T>
|
|
||||||
class ds_set {
|
|
||||||
public:
|
|
||||||
//CONSTRUCTORS, DESTRUCTORS, ASSIGNMENT OPERATOR
|
|
||||||
ds_set() : root_(NULL), size_(0) {}
|
|
||||||
ds_set(const ds_set<T>& old) : size_(old.size_) { root_ = this->copy_tree(old.root_,NULL); }
|
|
||||||
~ds_set() { this->destroy_tree(root_); root_ = NULL; }
|
|
||||||
ds_set& operator=(const ds_set<T>& old) { /* implementation omitted */ }
|
|
||||||
|
|
||||||
typedef tree_iterator<T> iterator;
|
|
||||||
|
|
||||||
int size() const { return size_; }
|
|
||||||
bool operator==(const ds_set<T>& old) const { return (old.root_ == this->root_); }
|
|
||||||
|
|
||||||
// FIND, INSERT & ERASE
|
|
||||||
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_); }
|
|
||||||
int erase(T const& key_value) { return erase(key_value, root_); }
|
|
||||||
|
|
||||||
// OUTPUT & PRINTING
|
|
||||||
friend std::ostream& operator<< (std::ostream& ostr, const ds_set<T>& s) {
|
|
||||||
s.print_in_order(ostr, s.root_);
|
|
||||||
return ostr;
|
|
||||||
}
|
|
||||||
|
|
||||||
// ITERATORS
|
|
||||||
iterator begin() const {
|
|
||||||
if (!root_) return iterator(NULL);
|
|
||||||
TreeNode<T>* p = root_;
|
|
||||||
while (p->left) p = p->left;
|
|
||||||
return iterator(p);
|
|
||||||
}
|
|
||||||
iterator end() const { return iterator(NULL); }
|
|
||||||
|
|
||||||
private:
|
|
||||||
// REPRESENTATION
|
|
||||||
TreeNode<T>* root_;
|
|
||||||
int size_;
|
|
||||||
|
|
||||||
// PRIVATE HELPER FUNCTIONS
|
|
||||||
TreeNode<T>* copy_tree(TreeNode<T>* old_root) { /* Implemented in Lab 9 */ }
|
|
||||||
void destroy_tree(TreeNode<T>* p) {
|
|
||||||
/* Implemented in Lecture 18 */
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
iterator find(const T& key_value, TreeNode<T>* p) { /* Implemented in Lecture 17 */ }
|
|
||||||
|
|
||||||
std::pair<iterator,bool> insert(const T& key_value, TreeNode<T>*& p) {
|
|
||||||
// NOTE: will need revision to support & maintain parent pointers
|
|
||||||
if (!p) {
|
|
||||||
p = new TreeNode<T>(key_value);
|
|
||||||
this->size_++;
|
|
||||||
return std::pair<iterator,bool>(iterator(p), true);
|
|
||||||
}
|
|
||||||
else if (key_value < p->value)
|
|
||||||
return insert(key_value, p->left);
|
|
||||||
else if (key_value > p->value)
|
|
||||||
return insert(key_value, p->right);
|
|
||||||
else
|
|
||||||
return std::pair<iterator,bool>(iterator(p), false);
|
|
||||||
}
|
|
||||||
|
|
||||||
int erase(T const& key_value, TreeNode<T>* &p) { /* Implemented in Lecture 19 */ }
|
|
||||||
|
|
||||||
void print_in_order(std::ostream& ostr, const TreeNode<T>* p) const {
|
|
||||||
if (p) {
|
|
||||||
print_in_order(ostr, p->left);
|
|
||||||
ostr << p->value << "\n";
|
|
||||||
print_in_order(ostr, p->right);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
Reference in New Issue
Block a user