From 5eb5daa04eb1228494d7682146196ac4dd2552f5 Mon Sep 17 00:00:00 2001 From: Jidong Xiao Date: Thu, 26 Oct 2023 10:29:58 -0400 Subject: [PATCH] adding ds set starter code --- lectures/17_trees_I/ds_set_starter.h | 130 +++++++++++++++++++++++++++ 1 file changed, 130 insertions(+) create mode 100644 lectures/17_trees_I/ds_set_starter.h diff --git a/lectures/17_trees_I/ds_set_starter.h b/lectures/17_trees_I/ds_set_starter.h new file mode 100644 index 0000000..800ec31 --- /dev/null +++ b/lectures/17_trees_I/ds_set_starter.h @@ -0,0 +1,130 @@ +// 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 +#include + +// ------------------------------------------------------------------- +// TREE NODE CLASS +template +class TreeNode { +public: + TreeNode() : left(NULL), right(NULL) {} + TreeNode(const T& init) : value(init), left(NULL), right(NULL) {} + T value; + TreeNode* left; + TreeNode* right; +}; + +template class ds_set; + +// ------------------------------------------------------------------- +// TREE NODE ITERATOR CLASS +template +class tree_iterator { +public: + tree_iterator() : ptr_(NULL) {} + tree_iterator(TreeNode* 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_; } + // increment & decrement will be discussed in Lecture 18 and Lab 11 + +private: + // representation + TreeNode* ptr_; +}; + +// ------------------------------------------------------------------- +// DS SET CLASS +template +class ds_set { +public: + ds_set() : root_(NULL), size_(0) {} + ds_set(const ds_set& old) : size_(old.size_) { + root_ = this->copy_tree(old.root_); } + ~ds_set() { this->destroy_tree(root_); root_ = NULL; } + ds_set& operator=(const ds_set& old) { + if (&old != this) { + this->destroy_tree(root_); + root_ = this->copy_tree(old.root_); + size_ = old.size_; + } + return *this; + } + + typedef tree_iterator iterator; + + int size() const { return size_; } + bool operator==(const ds_set& 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& 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 { + // Implemented in Lecture 17 + + + + + + + + } + iterator end() const { return iterator(NULL); } + +private: + // REPRESENTATION + TreeNode* root_; + int size_; + + // PRIVATE HELPER FUNCTIONS + TreeNode* copy_tree(TreeNode* old_root) { /* Implemented in Lab 9 */ } + void destroy_tree(TreeNode* p) { /* Implemented in Lecture 18 */ } + + iterator find(const T& key_value, TreeNode* p) { + // Implemented in Lecture 17 + + + + + + + + + + } + + std::pair insert(const T& key_value, TreeNode*& p) { /* Discussed in Lecture 18 */ } + int erase(T const& key_value, TreeNode* &p) { /* Implemented in Lecture 19 */ } + + void print_in_order(std::ostream& ostr, const TreeNode* p) const { + // Discussed in Lecture 18 + 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* p, int depth) const { + /* Discussed in Lecture 17 */ } +}; + +#endif