diff --git a/TinySTL/AVLTree.h b/TinySTL/AVLTree.h index fa13409ed569eee77b4d62d0747ee1f7eb7dee4d..2d0fba9be7c4cba9feba851756fc53a448c89a9e 100644 --- a/TinySTL/AVLTree.h +++ b/TinySTL/AVLTree.h @@ -7,6 +7,10 @@ #include namespace TinySTL{ + namespace { + template + class avl_iter; + } //class of avl tree template class avl_tree{ @@ -18,10 +22,11 @@ namespace TinySTL{ T data_; node *left_, *right_; size_t height_; + typedef T value_type; explicit node(T d = T(), node *l = 0, node *r = 0, size_t h = 0) :data_(d), left_(l), right_(r), height_(h){} }; - typedef TinySTL::allocator dataAllocator; + typedef TinySTL::allocator dataAllocator; public: typedef T value_type; typedef avl_iter const_iterator; @@ -38,7 +43,14 @@ namespace TinySTL{ size_t height()const{ return root_ == 0 ? -1 : root_->height_; } size_t size()const{ return size_; } bool empty()const{ return root_ == 0; } - /*const_iterator root(){ return const_iterator(root_, this); }*/ + const_iterator root(){ return const_iterator(root_, this); } + + const_iterator cbegin(){ return find_min(); } + const_iterator cend(){ return const_iterator(0, this); } + + const_iterator find_min(); + const_iterator find_max(); + const_iterator find(const T& val); private: void destroyAndDeallocateAllNodes(node *p){ if (p != 0){ @@ -48,17 +60,56 @@ namespace TinySTL{ dataAllocator::deallocate(p); } } - }; + const_iterator find_min_aux(const node *ptr); + const_iterator find_max_aux(const node *ptr); + const_iterator find_aux(const T& val, const node *ptr); + };// end of avl_tree + template + typename avl_tree::const_iterator avl_tree::find_aux(const T& val, const node *ptr){ + while (ptr != 0){ + if (ptr->data_ < val) + ptr = ptr->right_; + else if (ptr->data_ > val) + ptr = ptr->left_; + else + break; + } + return const_iterator(ptr, this); + } + template + typename avl_tree::const_iterator avl_tree::find(const T& val){ + return find_aux(val, root_); + } + template + typename avl_tree::const_iterator avl_tree::find_max_aux(const node *ptr){ + while (ptr != 0 && ptr->right_ != 0) + ptr = ptr->right_; + return const_iterator(ptr, this); + } + template + typename avl_tree::const_iterator avl_tree::find_max(){ + return find_max_aux(root_); + } + template + typename avl_tree::const_iterator avl_tree::find_min_aux(const node *ptr){ + while (ptr != 0 && ptr->left_ != 0) + ptr = ptr->left_; + return const_iterator(ptr, this); + } + template + typename avl_tree::const_iterator avl_tree::find_min(){ + return find_min_aux(root_); + } namespace{ - //class of bst iterator + //class of avl tree iterator template//T = node class avl_iter{ private: template friend class avl_tree; private: - typedef typename avl_tree::vaule_type value_type; + typedef typename avl_tree::value_type value_type; typedef typename avl_tree::const_reference const_reference; typedef typename const T::value_type *const_pointer; typedef avl_tree * cntrPtr;