【 数据结构】实现二叉树以及其基本操作
来源:互联网 发布:别墅设计软件下载 编辑:程序博客网 时间:2024/06/06 09:50
//实现二叉树以及其基本操作//头文件#include <iostream>using namespace std;template<class Type>class Bintree;template<class Type>class BintreeNode{friend class Bintree<Type>;public:BintreeNode() :data(Type()), leftchild(NULL), rightchild(NULL){}BintreeNode(Type d, BintreeNode<Type> *left = NULL, BintreeNode<Type> *right = NULL) : data(d), leftchild(left), rightchild(right){}//~BintreeNode();private:BintreeNode<Type> *leftchild;BintreeNode<Type> *rightchild;Type data;};template<class Type>class Bintree{public:Bintree() :Ref(Type()), root(NULL){}Bintree(Type ref, BintreeNode<Type> *_root = NULL) : Ref(ref), root(_root){}//~Bintree();public:void CreatBintree(){CreatBintree(root);}void PreOrder(){PreOrder(root);}void InOrder(){InOrder(root);}void PostOrder(){PostOrder(root);}int Height(){return Height(root);}int Size(){return Size(root);}BintreeNode<Type>* Search(Type const key){return Search(root, key);}BintreeNode<Type>* PreOrder_Find(Type const key){return PreOrder_Find(root, key);}BintreeNode<Type>* InOrder_Find(Type const key){return InOrder_Find(root, key);}BintreeNode<Type>* PostOrder_Find(Type const key){return PostOrder_Find(root, key);}BintreeNode<Type>* Parent(BintreeNode <Type> *p){return Parent(root, p);}BintreeNode<Type>* Leftchild(Type key){return Leftchild(root, key);}BintreeNode<Type>* Rightchild(Type key){return Rightchild(root, key);}void quit_system(int &a){a = quit_system();}BintreeNode<Type>* Root(){return Root(root);}bool IsEmpty(){return IsEmpty(root);}void Destory(){Destory(root);}protected:void CreatBintree(BintreeNode<Type> *&t) //创造树{Type input;cin >> input;if (input == Ref){t = NULL;}else{t = new BintreeNode<Type>(input);CreatBintree(t->leftchild);CreatBintree(t->rightchild);}}void PreOrder(const BintreeNode<Type> *t) //前序{if (t == NULL){return;}else{cout << t->data << " ";PreOrder(t->leftchild);PreOrder(t->rightchild);}}void InOrder(const BintreeNode<Type> *t) //中序{if (t == NULL){return;}else{InOrder(t->leftchild);cout << t->data << " ";InOrder(t->rightchild);}}void PostOrder(const BintreeNode<Type> *t) //后序{if (t == NULL){return;}else{PostOrder(t->leftchild);PostOrder(t->rightchild);cout << t->data << " ";}}int Height(const BintreeNode<Type> *t) //树的高度{if (t == NULL)return 0;return (Height(t->leftchild) >Height(t->rightchild)) ?( Height(t->leftchild)+1) :( Height(t->rightchild)+1);}int Size(const BintreeNode<Type> *t) //树的大小{if (t == NULL)return 0;else{return (Size(t->leftchild)+Size(t->rightchild)+1);}}BintreeNode<Type> * Search(BintreeNode<Type>* t, Type const k) //查找{if (t == NULL )return NULL;if (t->data == k)return t;BintreeNode<Type> *p;if ((p = Search(t->leftchild, k)) != NULL);elseSearch(t->rightchild, k);}BintreeNode<Type>* PreOrder_Find(BintreeNode<Type>* t, Type const key) //前序查找{if (t == NULL)return NULL;if (t->data == key)return t;BintreeNode<Type> *p;if ((p = PreOrder_Find(t->leftchild, key)) != NULL);elsePreOrder_Find(t->rightchild, key);}BintreeNode<Type>* InOrder_Find(BintreeNode<Type>* t, Type const key) //中序查找{if (t == NULL)return NULL;BintreeNode<Type> *p;if ((p = InOrder_Find(t->leftchild, key)) != NULL);else if (t->data == key)return t;elseInOrder_Find(t->rightchild, key);}BintreeNode<Type>* PostOrder_Find(BintreeNode<Type>* t, Type const key) //后序查找{if (t == NULL)return NULL;BintreeNode<Type> *p;BintreeNode<Type> *q;if ((p = PostOrder_Find(t->leftchild, key)) != NULL);else if ((q = PostOrder_Find(t->rightchild, key)) != NULL);elseif (t->data == key)return t;}BintreeNode<Type>* Parent(BintreeNode<Type>* t, BintreeNode<Type>* q) //查找父节点{if (t == NULL)return NULL;if (q == t || q == t->leftchild || q == t->rightchild)return t;BintreeNode<Type>* p;if ((p = Parent(t->leftchild, q))!= NULL)return p;elsereturn Parent(t->rightchild, q);}BintreeNode<Type>* Leftchild(BintreeNode<Type>* t, Type const key) //查找左孩子{if (t == NULL)return NULL;BintreeNode<Type>*p = Search(t, key);if ( p->leftchild == NULL)return NULL;return (p->leftchild);}BintreeNode<Type>* Rightchild(BintreeNode<Type>* t, Type const key) //查找右孩子{if (t == NULL)return NULL;BintreeNode<Type>*p = Search(t, key);if (p->rightchild == NULL)return NULL;return (p->rightchild);}int quit_system() //退出{return 0;}BintreeNode<Type>* Root(BintreeNode<Type>* t) //根节点{return t;}bool IsEmpty(BintreeNode<Type>* t){return t == NULL;}void Destory(BintreeNode<Type>* t){if (t!= NULL){Destory(t->leftchild);Destory(t->rightchild);delete t;}}private:BintreeNode<Type> *root;Type Ref;};#include "Bintree.h"//主函数int main(){Bintree<char> bt('#');int select = 1;char Item;while (select){cout << "******************************************************************" << endl;cout << "* [1] creat [2] PreOrder [3] InOrder *" << endl;cout << "* [4] PostOrder [5] Height [6] Size *" << endl;cout << "* [7] search [8] PreOrder_Find [9] InOrder_Find *" << endl;cout << "* [10] PostOrder_Find [11] parent [12] leftchild *" << endl;cout << "* [13] rightchild [14] root [15] destory *" << endl;cout << "* [16] Isempty [17] quit_system *" << endl;cout << "******************************************************************" << endl;cout << "pleae choose:";cin >> select;switch (select){case 1:cout << "please enter:";bt.CreatBintree();break;case 2:bt.PreOrder();cout << endl;break;case 3:bt.InOrder();cout << endl;break;case 4:bt.PostOrder();cout << endl;break;case 5:cout << "树的大小为:" << bt.Height() << endl;break;case 6:cout << "树的大小为:" << bt.Size() << endl;break;case 7:cout << "请输入要查找的节点:";cin >> Item;cout << bt.Search(Item) << endl;break;case 8:cout << "请输入要查找的节点:";cin >> Item;cout << bt.PreOrder_Find(Item) << endl;break;case 9:cout << "请输入要查找的节点:";cin >> Item;cout << bt.InOrder_Find(Item) << endl;break;case 10:cout << "请输入要查找的节点:";cin >> Item;cout << bt.PostOrder_Find(Item) << endl;break;case 11:cout << "请输入要查找的节点:";cin >> Item;cout <<"父节点为:"<< bt.Parent(bt.Search(Item)) << endl;break;case 12:cout << "请输入要查找的节点:";cin >> Item;cout <<"左孩子为:"<< bt.Leftchild(Item) << endl;break;case 13:cout << "请输入要查找的节点:";cin >> Item;cout << "右孩子为:" << bt.Rightchild(Item) << endl;break;case 14:cout << "根节点为:" << bt.Root() << endl;break;case 15:bt.Destory();break;case 16:if (bt.IsEmpty())cout << "该树为空" << endl;elsecout << "树为不为空" << endl;break;case 17:bt.quit_system(select);break;default:break;}}return 0;}<img src="http://img.blog.csdn.net/20150609111330362?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZG91ZG91d2ExMjM0/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
0 0
- 【 数据结构】实现二叉树以及其基本操作
- 数据结构中栈的实现以及其基本操作
- 【数据结构】二叉树基本操作的程序实现
- 数据结构.二叉树的基本操作(C语言实现)
- 数据结构:二叉树的基本操作(JAVA实现)
- 数据结构 — 二叉树的基本操作(递归实现)
- 数据结构 — 二叉树的基本操作实现(递归算法)
- javascript实现数据结构: 树和二叉树,二叉树的遍历和基本操作
- 数据结构--二叉树(链表)基本操作
- 数据结构之二叉树的基本操作
- 【数据结构】二叉树的基本操作
- 数据结构—二叉树基本操作
- (数据结构)二叉树的基本操作
- 【数据结构笔记】二叉树的基本操作
- 数据结构-二叉树的基本操作
- 数据结构->二叉树的基本操作
- 二叉树基本操作以及面试题
- 二叉搜索树基本数据结构实现
- ubuntu安装postgresql和使用方法
- Activity生命执行中的几个方法
- C#:异步编程和线程的使用(.NET 4.5 )
- 帝友P2P任意密码支付密码重置让我超越马云爸爸不再是梦想
- usleep()和sleep()的区别
- 【 数据结构】实现二叉树以及其基本操作
- C#读文件操作
- libc++abi.dylib: terminate_handler unexpectedly threw an exception
- 浅谈Spring事务隔离级别
- 04Top K算法问题
- Java线程_wait,sleep,notify,notifyall,join
- 【剑指offer】 面试题7: 用两个栈实现队列
- 动态计算UITableviewCell高度
- JS 传值到jsp 中文字符乱码