//实现二叉树以及其基本操作 //头文件 #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) ; else Search(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) ; else PreOrder_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; else InOrder_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) ; else if (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; else return 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; else cout << "树为不为空" << 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="" />
原文地址:http://blog.csdn.net/doudouwa1234/article/details/46424311