From 02c95de42058fedf70de7bc78ff094c48d1279b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B9=E6=99=93=E8=88=AA?= <1210603696@qq.com> Date: Fri, 17 Oct 2014 17:22:00 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90bst=E7=9A=84=E5=8D=95?= =?UTF-8?q?=E4=B8=AA=E5=85=83=E7=B4=A0=E7=9A=84=E6=8F=92=E5=85=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TinySTL/BinarySearchTree.h | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/TinySTL/BinarySearchTree.h b/TinySTL/BinarySearchTree.h index e120dd6..e32d7a1 100644 --- a/TinySTL/BinarySearchTree.h +++ b/TinySTL/BinarySearchTree.h @@ -32,7 +32,9 @@ namespace TinySTL{ binary_search_tree() :root_(0){} binary_search_tree(const binary_search_tree&) = delete; binary_search_tree& operator=(const binary_search_tree&) = delete; - ~binary_search_tree(){}//TODO + ~binary_search_tree(){ + deallocateAllNodes(root_); + } void insert(const T& val); @@ -40,6 +42,13 @@ namespace TinySTL{ void print_inorder(const string& delim = " ", std::ostream& os = std::cout)const; void print_postorder(const string& delim = " ", std::ostream& os = std::cout)const; private: + void deallocateAllNodes(node *ptr){ + if (ptr){ + deallocateAllNodes(ptr->left_); + deallocateAllNodes(ptr->right_); + nodeAllocator::deallocate(ptr); + } + } void insert_elem(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;