用C++实现二叉树

来源:互联网 发布:野蛮人升级数据 编辑:程序博客网 时间:2024/06/05 17:43
#include <iostream>using namespace std;template<class Comparable>class BinarySearchTree{public:BinarySearchTree():root(NULL){}/*复制构造函数*/BinarySearchTree(const BinarySearchTree & rhs){root = clone(rhs.root);}/*重载赋值运算符函数*/const BinarySearchTree & operator=(const BinarySearchTree & rhs){if (this != & rhs){clear();root = clone(rhs.root);}return *this;}/*析构函数*/~BinarySearchTree(){clear();}/*查找最小值*/const Comparable & findMin() const{return findMin(root)->element;}/*查找最大值*/const Comparable & findMax() const{return findMax(root)->element;}/*判断是否包含结点x*/bool contains(const Comparable & x) const{return contains(x, root);}/*判断树是否为空*/bool isEmpty() const{return isEmpty(root);}/*前序输出*/void printTree_ELR() const{printTree_ELR(root);cout << endl;}/*中序输出*/void printTree_LER() const{printTree_LER(root);cout << endl;}/*后序输出*/void printTree_LRE() const{printTree_LRE(root);cout << endl;}/*清空树*/void clear(){clear(root);}/*插入结点x*/void insert(const Comparable & x){insert(x, root);}/*删除结点x*/void remove(const Comparable & x){remove(x, root);}private:struct BinaryNode{Comparable element;BinaryNode * left;BinaryNode * right;BinaryNode(const Comparable & theElement, BinaryNode * lt, BinaryNode * rt)//:element(theElement), left(lt), right(rt){element = theElement;left = lt;right = rt;}};BinaryNode * root;/*插入结点x*/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);else ;}/*判断树是否为空*/bool isEmpty(BinaryNode * t) const{if (root == NULL)return true;elsereturn false;}/*删除结点x*/void remove(const Comparable & x, BinaryNode * & t){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;else if(t->left == NULL)return t;return findMin(t->left);}/*查找最大值*/BinaryNode * findMax(BinaryNode *t) const{if (t == NULL)return NULL;else if (t->right == NULL)return t;return findMax(t->right);}/*判断是否包含结点x*/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;}/*清空树注意此处的值为根节点的引用(也就是跟结点本身),因为要清空树之后要把根结点置为NULL*/void clear(BinaryNode *  &t){if (t != NULL){clear(t->left);clear(t->right);delete t;}t = NULL;}/*下面这个清空树的函数也是正确的*///void clear(BinaryNode * t){//if (t != NULL){//clear(t->left);//clear(t->right);//delete t;//}//root = NULL;//}BinaryNode * clone(BinaryNode * t) const{if (t == NULL)return NULL;return new BinaryNode(t->element, clone(t->left), clone(t->right));}/*前序输出*/void printTree_ELR(BinaryNode * t) const{if (t == NULL)return;cout << t->element << " ";printTree_ELR(t->left);printTree_ELR(t->right);}/*中序输出*/void printTree_LER(BinaryNode * t) const{if (t == NULL)return;printTree_LER(t->left);cout << t->element << " ";printTree_LER(t->right);}/*后序输出*/void printTree_LRE(BinaryNode * t) const{if (t == NULL)return;printTree_LRE(t->left);printTree_LRE(t->right);cout << t->element << " ";}};


#include "binarytree.h"int main(){BinarySearchTree<int> tree;if (tree.isEmpty())cout << "tree is empty.\n";elsecout << "tree is not empty.\n";tree.insert(10);tree.insert(9);tree.insert(8);tree.insert(18);cout << "After insert four node in the tree.\n";if (tree.isEmpty())cout << "tree is empty.\n";elsecout << "tree is not empty.\n";cout << "中序输出:";tree.printTree_LER();cout << "前序输出:";tree.printTree_ELR();cout << "后序输出:";tree.printTree_LRE();if (tree.contains(18))cout << "tree containes 18.\n";else cout << "tree doesn't contain 18.\n";if (tree.contains(20))cout << "tree containes 20.\n";else cout << "tree doesn't contain 20.\n";cout << "The max value in the tree is " << tree.findMax() << endl;cout << "The min value in the tree is " << tree.findMin() << endl;tree.remove(10);cout << "删除10之后,中序输出:";tree.printTree_LER();BinarySearchTree<int> tree2;tree2 = tree;//调用赋值运算符函数cout << "tree2中序输出:";tree2.printTree_LER();BinarySearchTree<int> tree3(tree);cout << "tree3中序输出:";tree3.printTree_LER();tree3.clear();if (tree3.isEmpty())cout << "tree3 is empty.\n";elsecout << "tree3 is not empty.\n";cout << "tree3中序输出:";tree3.printTree_LER();cout << endl;system("pause");exit(0);}


0 0