From 132dae1a73f99f85eb68b4202ea6eba7b3538930 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:17:12 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90bst=E7=9A=84=E5=89=8D?= =?UTF-8?q?=E4=B8=AD=E5=90=8E=E5=BA=8F=E8=BE=93=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TinySTL/BinarySearchTree.h | 105 +++++++++++++++++++++++++++++++++++-- 1 file changed, 101 insertions(+), 4 deletions(-) diff --git a/TinySTL/BinarySearchTree.h b/TinySTL/BinarySearchTree.h index 221fd32..e120dd6 100644 --- a/TinySTL/BinarySearchTree.h +++ b/TinySTL/BinarySearchTree.h @@ -2,14 +2,111 @@ #define _BINARY_SEARCH_TREE_H_ #include "Allocator.h" +#include "String.h" namespace TinySTL{ + namespace { + template + class bst_iter; + } //class of binary_search_tree - template> - class binary_search_tree{}; - //class of bst iterator template - class bst_iter{}; + class binary_search_tree{ + private: + template + friend class bst_iter; + private: + struct node{ + T data_; + node *left_; + node *right_; + }; + typedef TinySTL::allocator nodeAllocator; + public: + typedef T vaule_type; + typedef bst_iter iterator; + typedef T& reference; + private: + node *root_; + public: + 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 + + void insert(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; + void print_postorder(const string& delim = " ", std::ostream& os = std::cout)const; + private: + 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; + 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){ + if (ptr == 0){ + ptr = nodeAllocator::allocate(); + ptr->data_ = val; + ptr->left_ = ptr->right_ = 0; + } + else{ + if (val < ptr->data_){ + return insert_elem(val, ptr->left_); + } + else if (val > ptr->data_){ + return insert_elem(val, ptr->right_); + } + } + } + template + void binary_search_tree::insert(const T& val){ + insert_elem(val, root_); + } + template + void binary_search_tree::print_preorder_aux(const string& delim, std::ostream& os, const node *ptr)const{ + if (ptr){ + os << ptr->data_ << delim; + print_preorder_aux(delim, os, ptr->left_); + print_preorder_aux(delim, os, ptr->right_); + } + } + template + void binary_search_tree::print_preorder(const string& delim, std::ostream& os)const{ + print_preorder_aux(delim, os, root_); + } + template + void binary_search_tree::print_inorder_aux(const string& delim, std::ostream& os, const node *ptr)const{ + if (ptr){ + print_inorder_aux(delim, os, ptr->left_); + os << ptr->data_ << delim; + print_inorder_aux(delim, os, ptr->right_); + } + } + template + void binary_search_tree::print_inorder(const string& delim, std::ostream& os)const{ + print_inorder_aux(delim, os, root_); + } + template + void binary_search_tree::print_postorder_aux(const string& delim, std::ostream& os, const node *ptr)const{ + if (ptr){ + print_postorder_aux(delim, os, ptr->left_); + print_postorder_aux(delim, os, ptr->right_); + os << ptr->data_ << delim; + } + } + template + void binary_search_tree::print_postorder(const string& delim, std::ostream& os)const{ + print_postorder_aux(delim, os, root_); + } + + namespace{ + //class of bst iterator + template + class bst_iter{}; + } } #endif \ No newline at end of file