diff --git a/TinySTL/BinarySearchTree.h b/TinySTL/BinarySearchTree.h index e32d7a1..14e212a 100644 --- a/TinySTL/BinarySearchTree.h +++ b/TinySTL/BinarySearchTree.h @@ -17,6 +17,7 @@ namespace TinySTL{ friend class bst_iter; private: struct node{ + typedef T value_type; T data_; node *left_; node *right_; @@ -37,6 +38,14 @@ namespace TinySTL{ } void insert(const T& val); + template + void insert(Iterator first, Iterator last); + + bool empty()const{ return root_ == 0; } + + iterator find_min(); + iterator find_max(); + iterator find(const T& val); void print_preorder(const string& delim = " ", std::ostream& os = std::cout)const; void print_inorder(const string& delim = " ", std::ostream& os = std::cout)const; @@ -50,12 +59,15 @@ namespace TinySTL{ } } void insert_elem(const T& val, node *&ptr); + iterator find_min_aux(node *ptr); + iterator find_max_aux(node *ptr); + iterator find_aux(const T& val, node *ptr); void print_preorder_aux(const string& delim, std::ostream& os, const node *ptr)const; void print_inorder_aux(const string& delim, std::ostream& os, const node *ptr)const; void print_postorder_aux(const string& delim, std::ostream& os, const node *ptr)const; };//end of bst class template - void binary_search_tree::insert_elem(const T& val, node *&ptr){ + void binary_search_tree::insert_elem(const T& val, node *&ptr){//重复的元素不插入 if (ptr == 0){ ptr = nodeAllocator::allocate(); ptr->data_ = val; @@ -75,6 +87,12 @@ namespace TinySTL{ insert_elem(val, root_); } template + template + void binary_search_tree::insert(Iterator first, Iterator last){ + for (; first != last; ++first) + insert(*first); + } + template void binary_search_tree::print_preorder_aux(const string& delim, std::ostream& os, const node *ptr)const{ if (ptr){ os << ptr->data_ << delim; @@ -110,11 +128,62 @@ namespace TinySTL{ void binary_search_tree::print_postorder(const string& delim, std::ostream& os)const{ print_postorder_aux(delim, os, root_); } + template + typename binary_search_tree::iterator binary_search_tree::find_min_aux(node *ptr){ + while (ptr && ptr->left_ != 0){ + ptr = ptr->left_; + } + return iterator(ptr); + } + template + typename binary_search_tree::iterator binary_search_tree::find_min(){ + return find_min_aux(root_); + } + template + typename binary_search_tree::iterator binary_search_tree::find_max_aux(node *ptr){ + while (ptr && ptr->right_ != 0){ + ptr = ptr->right_; + } + return iterator(ptr); + } + template + typename binary_search_tree::iterator binary_search_tree::find_max(){ + return find_max_aux(root_); + } + template + typename binary_search_tree::iterator binary_search_tree::find_aux(const T& val, node *ptr){ + while (ptr){ + if (val == ptr->data_) + break; + else if (val < ptr->data_) + ptr = ptr->left_; + else + ptr = ptr->right_; + } + return iterator(ptr); + } + template + typename binary_search_tree::iterator binary_search_tree::find(const T& val){ + return find_aux(val, root_); + } namespace{ //class of bst iterator - template - class bst_iter{}; + template//T = node + class bst_iter{ + private: + typedef typename binary_search_tree::vaule_type value_type; + typedef typename binary_search_tree::reference reference; + typedef typename T::value_type *pointer; + private: + T *ptr_; + public: + bst_iter(T *ptr) :ptr_(ptr){} + + operator T*(){ return ptr_; } + reference operator*(){ return ptr_->data_; } + pointer operator ->(){ return &(operator*()); } + }; } }