树的实现文件 /* * BinarySearchTree.h * * Created on: 2011-5-27 * Author: sjiang2142 */ template<typename Comparable> class BinarySearchTree { public: //构造函数 BinarySearchTree() { root=NULL; } //复制构造函数 BinarySearchTree(const BinarySearchTree& rhs) { operator=(rhs); } //析构函数 ~BinarySearchTree() { makeEmpty(); } //该方法用于寻找最小元素 const Comparable& findMin()const { return findMin(root)->element; } //该方法用于寻找最大元素 const Comparable& findMax()const { return findMax(root)->element; } //该方法判断树中是否包含某一元素 bool contains(const Comparable& x)const { return contains(x,root); } //判断是否是空树 bool isEmpty()const { if(root==NULL) { return true; } else { return false; } } //打印一棵树 void printTree()const { printTree(root); } //清空一棵树 void makeEmpty() { makeEmpty(root); } //插入一个元素 void insert(const Comparable& x) { insert(x,root); } //移除一个元素 void remove(const Comparable& x) { remove(x,root); } //重载赋值运算符 const BinarySearchTree& operator=(const BinarySearchTree& rhs) { if(this!=&rhs) { makeEmpty(); root = clone(rhs.root); } return *this; } private: //结点结构 struct BinaryNode { Comparable element; BinaryNode* left; BinaryNode* right; BinaryNode(const Comparable& theElement,BinaryNode *lt,BinaryNode* rt):element(theElement),left(lt),right(rt){} }; BinaryNode* root;//根结点 //插入一个元素 void insert(const Comparable& x,BinaryNode*& t)const { if(t==NULL) { t = new BinaryNode(x,NULL,NULL); } else if(x<t->element) { insert(x,t->left); } else if(x>t->element) { insert(x,t->right); } } //删除某一元素 void remove(const Comparable& x,BinaryNode*& t)const { if(t==NULL) { return; } if(x<t->element)//首先找到要删除的节点 { remove(x,t->left); } else if(x>t->element) { remove(x,t->right); } else if(t->left!=NULL&&t->right!=NULL)//找到了要删除的节点,并且该节点有两个孩子 { t->element=findMin(t->right)->element;//使用右子树中最小的节点元素替代要删除的节点元素 remove(t->element,t->right); } else//找到了要删除的节点,并且该节点只有一个孩子 { BinaryNode* oldNode=t; t=(t->left!=NULL)?t->left:t->right; delete oldNode; } } //返回最小元素 BinaryNode* findMin(BinaryNode* t)const { if(t==NULL) { return NULL; } if(t->left==NULL) { return t; } return findMin(t->left); } //返回最大元素 BinaryNode* findMax(BinaryNode* t)const { if(t!=NULL) { while(t->right!=NULL) { t=t->right; } } return t; } //判断是否包含某一元素 bool contains(const Comparable& x,BinaryNode* t)const { if(t==NULL) { return false; } else if(x<t->element) { return contains(x,t->left);//递归调用 } else if(x>t->element) { return contains(x,t->right); } else { return true; } } //清空树 void makeEmpty(BinaryNode*& t) { if(t!=NULL) { makeEmpty(t->left); makeEmpty(t->right); delete t; } t=NULL; } //打印一棵树 void printTree(BinaryNode* t)const { if(t==NULL) { return; } if(t->left!=NULL) { printTree(t->left); } std::cout<<t->element<<std::endl; printTree(t->right); } //克隆一棵树 BinaryNode* clone(BinaryNode* t)const { if(t==NULL) { return NULL; } return new BinaryNode(t->element,clone(t->left),clone(t->right)); } }; 测试代码 //============================================================================ // Name : DT.cpp // Author : SJ // Version : // Copyright : Your copyright notice //============================================================================ #include <iostream> #include "BinarySearchTree.h" using namespace std; int main() { BinarySearchTree<int> binarySearchTree;//创建一个二差查找树 //向树中插入值 binarySearchTree.insert(5); binarySearchTree.insert(2); binarySearchTree.insert(38); binarySearchTree.insert(24); binarySearchTree.insert(4); binarySearchTree.insert(56); //从树中移除一个元素 binarySearchTree.remove(4); int max = binarySearchTree.findMax();//返回树中的最大值 int min = binarySearchTree.findMin();//返回树中的最小值 cout<<"max="<<max<<endl;//输出最大值 cout<<"min="<<min<<endl;//输出最小值 bool isContains = binarySearchTree.contains(4);//判断树中是否包含一个元素 cout<<"isContains 4:"<<isContains<<endl; bool isEmpty = binarySearchTree.isEmpty();//判断树是否为空 cout<<"isEmpty:"<<isEmpty<<endl; binarySearchTree.printTree();//采用中序遍历打印树 //复制构造函数 BinarySearchTree<int> binarySearchTreeX(binarySearchTree); cout<<"new tree......"<<endl; binarySearchTreeX.printTree(); return 0; } 程序运行结果 max=56min=2isContains 4:0isEmpty:025243856new tree......25243856