二叉排序树(BinarySortTree)又称二叉查找树,亦称二叉搜索树。
它或者是一棵空树;或者是具有下列性质的二叉树:
(1)若左子树不空,则左子树上所有结点的值均小于它的根结点的值;
(2)若右子树不空,则右子树上所有结点的值均大于它的根结点的值;
(3)左、右子树也分别为二叉排序树;
现在贴出对于二叉树实现的代码。
二叉树节点
BTNode.h
1 #include "stdafx.h" 2 #include <iostream> 3 using namespace std; 4 template <typename Type>class BinaryTree; 5 template <typename Type> 6 class BTNode{ 7 public: 8 friend class BinaryTree<typename Type>; 9 BTNode():rchild(NULL),lchild(NULL){} 10 BTNode(Type item , BTNode<Type>*r=NULL,BTNode<Type>*l=NULL):data(item),rchild(r),lchild(l){} 11 Type getData()const; // 获取数据 12 BTNode<Type>* getLeft()const; // 获取左子树 13 BTNode<Type>* getRight()const; // 获取右子树 14 void setData(const Type data); // 改变节点值 15 void setLeft(const BTNode<Type>*l); // 更改左子树的值 16 void setRight(const BTNode<Type>*r); // 更改右子树的值 17 void inOrder(); // 中序遍历 18 void preOrder(); // 前序遍历 19 void postOrder(); // 后序遍历 20 int size(); // 获取节点个数 21 int hight(); // 计算树高 22 BTNode<Type>* copy(const BTNode<Type> *copy); 23 friend bool equal<Type>(const BTNode<Type> *s,const BTNode<Type> *t); 24 void distroy(){ // 销毁树 25 if(this !=NULL){ 26 this->lchild->distroy(); 27 this->rchild->distroy(); 28 delete this; 29 } 30 } 31 private: 32 Type data; 33 BTNode<Type>*lchild; 34 BTNode<Type>*rchild; 35 }; 36 // 获取数据 37 template <typename Type> 38 Type BTNode<Type>::getData()const{ 39 return (this == NULL)? -1: data; 40 } 41 // 获取左子树 42 template <typename Type> 43 BTNode<Type>* BTNode<Type>::getLeft()const{ 44 return (this == NULL)? NULL:lchild; 45 } 46 //获取右子树 47 template <typename Type> 48 BTNode<Type>* BTNode<Type>::getRight()const{ 49 return (this == NULL)? NULL:rchild; 50 } 51 52 // 更改节点值 53 template<typename Type> 54 void BTNode<Type>::setData(Type data){ 55 if(NULL!=this){ 56 this->data =data; 57 } 58 else{ 59 cout<<"can't set"<<endl; 60 } 61 } 62 // 更改左子树 63 template<typename Type> 64 void BTNode<Type>::setLeft(const BTNode<Type> *left){ 65 if(this!=NULL){ 66 lchild=left; 67 } 68 } 69 70 // 更改右子树 71 template<typename Type> 72 void BTNode<Type>::setRight(const BTNode<Type> *right){ 73 if(this!=NULL){ 74 rchild=right; 75 } 76 } 77 // 中序遍历 78 template<typename Type> 79 void BTNode<Type>::inOrder(){ 80 if(NULL!=this){ 81 this->lchild->inOrder(); 82 cout <<"->"<< this->data ; 83 this->rchild->inOrder(); 84 } 85 86 } 87 88 // 前序遍历 89 template<typename Type> 90 void BTNode<Type>::preOrder(){ 91 if(NULL!=this){ 92 cout <<"->"<< this->data ; 93 this->lchild->preOrder(); 94 this->rchild->preOrder(); 95 } 96 } 97 // 后序遍历 98 template<typename Type> 99 void BTNode<Type>::postOrder(){ 100 if(NULL!=this){ 101 this->lchild->postOrder(); 102 this->rchild->postOrder(); 103 cout <<"->"<< this->data ; 104 } 105 } 106 107 // 获取树的大小 108 template<typename Type> 109 int BTNode<Type>::size(){ 110 if(this==NULL)return 0; 111 return (1+this->lchild->size()+this->rchild->size()); 112 } 113 // 计算树的高度 114 template<typename Type> 115 int BTNode<Type>::hight(){ 116 if(this==NULL)return -1; 117 int lhight = this->lchild->hight(); 118 int rhight = this->rchild->hight(); 119 return (1+(lhight>rhight?lhight:rhight)); 120 } 121 // 拷贝树 122 template <typename Type> 123 BTNode<Type>* BTNode<Type>::copy(const BTNode<Type> *copy){ 124 if(copy==NULL)return NULL; 125 BTNode<Type>*newNode = new BTNode<Type>(copy->data); 126 newNode->lchild=this->copy(copy->lchild); 127 newNode->rchild=this->copy(copy->rchild); 128 return newNode; 129 } 130 131 //判断两棵树是否相等 132 template<typename Type> 133 bool equal(const BTNode<Type> *s,const BTNode<Type> *t){ 134 if(s==NULL&&t==NULL){ 135 return 1; 136 } 137 if(s&&t&&s->data==t->data&&equal(s->lchild,t->lchild)&&equal(s->rchild,t->rchild)){ 138 return 1; 139 } 140 return 0; 141 }
二叉树实现
BinaryTree.h
1 #include "BTNode.h" 2 template<typename Type> 3 class BinaryTree{ 4 public: 5 BinaryTree():root(NULL){} 6 BinaryTree(Type stop):m_stop(stop),root(NULL){} 7 // 拷贝构造函数 8 BinaryTree(BinaryTree<Type>& copy); 9 // 析构函数 10 virtual ~BinaryTree(){ 11 root->distroy(); 12 } 13 // 判空 14 virtual bool is_empty(){ 15 return root==NULL; 16 } 17 // 获取左子树 18 virtual BTNode<Type>* getLeft(BTNode<Type>* current); 19 // 获取右子树 20 virtual BTNode<Type>* getRight(BTNode<Type>* current); 21 // 获取父节点 22 virtual BTNode<Type>* getParent(BTNode<Type>* current); 23 // 获取根节点 24 const BTNode<Type>* getRoot() const; 25 // 插入一个节点 26 virtual bool insert(const Type item); 27 // 查找 28 virtual BTNode<Type> *find(const Type item) const; 29 void inOrder(); // 中序遍历 30 void preOrder(); // 先序遍历 31 void postOrder(); // 后序遍历 32 int size(); // 树的大小 33 int height(); // 树的高度 34 BinaryTree<Type>& operator=(const BinaryTree<Type> copy); // 树赋值 35 // 判等 36 friend bool operator== <Type>(const BinaryTree<Type> s,const BinaryTree<Type> t); 37 // 输入树 38 friend ostream& operator<< <Type>(ostream& ,BinaryTree<Type>&); 39 // 输出树 40 friend istream& operator>> <Type>(istream& ,BinaryTree<Type>&); 41 private: 42 Type m_stop; 43 BTNode<Type>*root; 44 // 获取父节点 45 BTNode<Type> *getParent(BTNode<Type> *start,BTNode<Type> *current); 46 // 打印树 47 void print(BTNode<Type> *start,int n=0); 48 49 }; 50 template <typename Type> 51 BinaryTree<Type>::BinaryTree(BinaryTree<Type>& copy){ 52 if(copy.root){ 53 this->m_stop = copy.m_stop; 54 } 55 root=root->copy(copy.root); 56 } 57 template<typename Type> 58 BTNode<Type>* BinaryTree<Type>::getLeft(BTNode<Type>* current){ 59 return (root&¤t?current->lchild:NULL); 60 } 61 62 template<typename Type> 63 BTNode<Type>* BinaryTree<Type>::getRight(BTNode<Type>* current){ 64 return (root&¤t?current->rchild:NULL); 65 } 66 67 template<typename Type> 68 BTNode<Type>* BinaryTree<Type>::getParent(BTNode<Type> *current){ 69 return root==NULL||current==root?NULL:getParent(root,current); 70 } 71 72 template<typename Type> 73 BTNode<Type>* BinaryTree<Type>::getParent(BTNode<Type>* start,BTNode<Type>* current){ 74 if(start==NULL||current==NULL){ 75 return NULL; 76 } 77 if(start->rchild==current||start->lchild==current){ 78 return start; 79 } 80 return (!getParent(start->lchild,current)?getParent(start->lchild,current):getParent(start->rchild,current)); 81 } 82 // 获取root 83 template <typename Type> 84 const BTNode<Type>* BinaryTree<Type>::getRoot()const{ 85 return ((NULL!=root)?root:NULL); 86 } 87 // 插入节点 88 template <typename Type> 89 bool BinaryTree<Type>::insert(const Type item){ 90 BTNode<Type> *newNode = new BTNode<Type>(item); 91 if(root==NULL){ 92 root = newNode; 93 return 1; 94 } 95 BTNode<Type>*pmove = root; 96 while(1){ 97 if(item>=pmove->data){ 98 if(pmove->rchild==NULL){ 99 pmove->rchild = newNode; 100 return 1; 101 } 102 pmove = pmove->rchild; 103 } 104 else{ 105 if(pmove->lchild==NULL){ 106 pmove->lchild = newNode; 107 return 1; 108 } 109 pmove = pmove->lchild; 110 } 111 } 112 } 113 // 查找 item 114 template<typename Type> 115 BTNode<Type>* BinaryTree<Type>::find(const Type item)const{ 116 if(root==NULL)return NULL; 117 BTNode<Type> * pmove = root; 118 while(pmove){ 119 if(item==pmove->data)return pmove; 120 else if(item>pmove->data){ 121 pmove=pmove->rchild; 122 } 123 else{ 124 pmove = pmove->lchild; 125 } 126 } 127 return NULL; 128 } 129 // 中序遍历 130 template<typename Type> 131 void BinaryTree<Type>::inOrder(){ 132 if(root==NULL){ 133 cout << "this is a empty tree" <<endl; 134 return; 135 } 136 root->inOrder(); 137 } 138 //先序遍历 139 template<typename Type> 140 void BinaryTree<Type>::preOrder(){ 141 if(root==NULL){ 142 cout << "this is a empty tree" <<endl; 143 return; 144 } 145 root->preOrder(); 146 } 147 //后序遍历 148 template<typename Type> 149 void BinaryTree<Type>::postOrder(){ 150 if(root==NULL){ 151 cout << "this is a empty tree" <<endl; 152 return; 153 } 154 root->postOrder(); 155 } 156 157 //树的大小 158 template <typename Type> 159 int BinaryTree<Type>::size(){ 160 if(root==NULL)return 0; 161 return root->size(); 162 } 163 //树的高度 164 template <typename Type> 165 int BinaryTree<Type>::height(){ 166 if(root==NULL)return 0; 167 return root->hight(); 168 } 169 // 赋值 170 template <typename Type> 171 BinaryTree<Type>& BinaryTree<Type>::operator=(const BinaryTree<Type>copy){ 172 if(copy->root){ 173 m_stop = copy.m_stop; 174 } 175 root = root->copy(copy.root); 176 return *this; 177 } 178 // 判等 179 template<typename Type> 180 bool operator==(const BinaryTree<Type> s,const BinaryTree<Type> t){ 181 return equal(s.root,t.root); 182 } 183 // 输出 184 template<typename Type> 185 ostream& operator<<(ostream&os ,BinaryTree<Type>&out){ 186 out.print(out.root); 187 return os; 188 } 189 //输入 190 template<typename Type> 191 istream& operator>>(istream&is ,BinaryTree<Type>&in){ 192 Type item; 193 cout<<"initialize the tree:"<< endl 194 << "Input data(end with "<<in.m_stop<<"!):"; 195 is >>item; 196 while(item!=in.m_stop){ 197 in.Insert(item); 198 is>>item; 199 } 200 return is; 201 } 202 template<typename Type> void BinaryTree<Type>::print(BTNode<Type> *start, int n){ 203 if(start==NULL){ 204 for(int i=0;i<n;i++){ 205 cout<<" "; 206 } 207 cout<<"NULL"<<endl; 208 return; 209 } 210 print(start->rchild,n+1); 211 for(int i=0;i<n;i++){ 212 cout<<" "; 213 } 214 if(n>=0){ 215 cout<<start->data<<"->"<<endl; 216 } 217 print(start->lchild,n+1); 218 }