From 7c036c46236d92bfb69774d8f42dcb80fa6e84ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B9=E6=99=93=E8=88=AA?= <1210603696@qq.com> Date: Sat, 18 Oct 2014 16:17:21 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=9F=A5=E8=AF=A2=E6=A0=91?= =?UTF-8?q?=E9=AB=98=E7=9A=84=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TinySTL/BinarySearchTree.h | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/TinySTL/BinarySearchTree.h b/TinySTL/BinarySearchTree.h index f3d7266..016ccd4 100644 --- a/TinySTL/BinarySearchTree.h +++ b/TinySTL/BinarySearchTree.h @@ -49,6 +49,7 @@ namespace TinySTL{ bool empty()const{ return root_ == 0; } size_t size()const{ return size_; } + size_t height()const{ return height_aux(root_, 0); } const_iterator root(){ return const_iterator(root_, this); } const_iterator cbegin(){ return find_min(); } @@ -70,6 +71,7 @@ namespace TinySTL{ nodeAllocator::deallocate(ptr); } } + size_t height_aux(const node *p, size_t h)const; void erase_elem(const T& val, node *&ptr); void insert_elem(const T& val, node *&ptr); const_iterator find_min_aux(const node *ptr); @@ -80,6 +82,14 @@ namespace TinySTL{ void print_postorder_aux(const string& delim, std::ostream& os, const node *ptr)const; };//end of bst class template + size_t binary_search_tree::height_aux(const node *p, size_t h)const{ + if (p == 0) + return h; + else{ + return TinySTL::max(height_aux(p->left_, h), height_aux(p->right_, h)) + 1; + } + } + template void binary_search_tree::erase_elem(const T& val, node *&ptr){ if (ptr == 0) return;