用C++实现二叉树
2015-10-18 17:48
351 查看
#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; else return 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"; else cout << "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"; else cout << "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"; else cout << "tree3 is not empty.\n"; cout << "tree3中序输出:"; tree3.printTree_LER(); cout << endl; system("pause"); exit(0); }
相关文章推荐
- C++ Primer 学习笔记_53_类和数据抽象 --友元、static员
- C++ 排序法之冒泡法和选择法 素数,递归调用,用星号(*)打印一个梯形
- C++中的iter_swap函数的应用
- 【转】【C/C++】实现memcpy函数
- 剑指offer第二十一题【栈的压入、弹出序列】c++实现
- Reverse Linked List(C++)
- 【C语言】 杀人凶手问题
- 【黑马训练营】IOS C语言通讯录
- 【c++程序】关于operator
- 【C语言】 跳水比赛名次预测问题
- 自由精神世界~分支结构
- 求二叉树中和为特定值的路径
- No.1 定时器接口timer的设计
- 第二章 构造函数语意学 成员们的初始化队伍
- 【转载】:【C++跨平台系列】解决STL的max()与numeric_limits::max()和VC6 min/max 宏冲突问题
- 剑指offer第二十题【包含min函数的栈】c++实现
- C++11中值得关注的几大变化
- [C++] STACK_Principle
- 【黑马训练营】IOS C语言指针
- C语言---数组,字符串数组,冒泡排序