Header file:
#include <iostream>using namespace Std;template<class type>class bintree;//node class Template<class Type> Class Bintreenode{friend class bintree<type>;p ublic:bintreenode ():d ATA (Type ()), Leftchild (NULL), Rightchild ( NULL) {}bintreenode (Type D, bintreenode<type> *l = null, bintreenode<type> *r = null): Data (d), Leftchild (L), Rightchild (R) {}private:bintreenode<type> *leftchild; Bintreenode<type> *rightchild; Type data;};/ /two fork Tree class Template<class Type>class bintree{public:bintree (): Ref (Type ()), root (NULL) {}bintree (type Re, bintreenode <Type> *r = NULL): Ref (re), Root (R) {}~bintree () {destory ();} PUBLIC://provides interface void Creatbintree () {creat (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 key) {return Search (root, key);} Bintreenode<type> *preorder_find (Type key) {return Preorder_find (rOot, key);} Bintreenode<type> *inorder_find (Type key) {return inorder_find (root, key);} Bintreenode<type> *postorder_find (Type key) {return postorder_find (root, key);} Bintreenode<type> *parent (bintreenode<type> *q) {return Parent (root, q);} Bintreenode<type> *leftchild (Type key) {return leftchild (root, key);} Bintreenode<type> *rightchild (Type key) {return rightchild (root, key);} Type root () {return root (root);} void Destory () {return destory (root);} BOOL IsEmpty () {return IsEmpty (root);} void Quit_system (int &x) {x = 0;} protected://Create two fork tree void creat (bintreenode<type> *&t) {Type input;cin >> input;if (input = = Ref) {t = NULL;} Else{t = new bintreenode<type> (input); Creat (T->leftchild); Creat (T->rightchild);}} Pre-order traversal void preorder (const bintreenode<type> *t) {if (t = = NULL) {return;} Else{cout << t->data << ""; Preorder (T->leftchild); Preorder (T->rightchild);}} The middle sequence iterates through void inorder (const bintreenode<type> *t) {if (t == NULL) {return;} Else{inorder (t->leftchild); cout << t->data << ""; Inorder (T->rightchild);}} Post-post traversal void Postorder (const bintreenode<type> *t) {if (t = = NULL) {return;} Else{postorder (T->leftchild); Postorder (t->rightchild); cout << t->data << "";}} Find height int height (const bintreenode<type> *t) {if (t = = NULL) return 0;return (height (t->leftchild) > Height (t-& Gt;rightchild))?
(Height (t->leftchild) + 1): (height (t->rightchild) + 1);} int Size (const bintreenode<type> *t) {if (t = = NULL) return 0;return (Size (t->leftchild) + size (t->rightchild) + 1);} Find a node returns its address bintreenode<type> *search (bintreenode<type> *t,const Type key) {if (t = = NULL) {return null;} if (T->data = = key) return t; Bintreenode<type> *p;if (p = search (T->leftchild, key)) = NULL) return P;elsereturn Search (T->rightchild, key);} Pre-order Lookup bintreenode<type> *preorder_find (bintreenode<type> *t, const Type key) {if (t = = NULL) {return null;} if (T->data = = key) return t; Bintreenode<type> *p;if (p = preorder_find (T->leftchild, key)) = NULL) return P;elsereturn Preorder_find (t-& Gt;rightchild, key);} Middle order Lookup bintreenode<type> *inorder_find (bintreenode<type> *t, const Type key) {if (t = = NULL) {return null;} Bintreenode<type> *p;if (p = inorder_find (T->leftchild, key)) = NULL) return P;else if (t->data = = key) return T;elseReturn Inorder_find (T->rightchild, key);} Post-Search bintreenode<type> *postorder_find (bintreenode<type> *t, const Type key) {if (t = = NULL) {return null;} Bintreenode<type> *p; Bintreenode<type> *q;if (p = postorder_find (T->leftchild, key)) = NULL) return P;else if ((q = postorder_find (t >rightchild, key)) = NULL) return Q;else if (t->data = key) return t;} asks the parent node and returns its parent node address bintreenode<type> *parent (bintreenode<type> *&t, bintreenode<type> *q) {if (t = = NULL) {return t;} if (q = = T->leftchild | | q = = T->rightchild | | q = = t) {return t;} Bintreenode<type> *p;if (p = Parent (T->leftchild, q)) = NULL) {return p;} Elsereturn Parent (T->rightchild, q);} Ask the left child bintreenode<type> *leftchild (bintreenode<type> *t, const Type key) {bintreenode<type> *p = Search (t, key), if (p = = null) {cout << "The node is not exist!" << endl;return null;} if (P->leftchild = = NULL) {cout << "This node is not have Leftchild" << Endl;return NULL;} Elsereturn P->leftchild;} Ask right child bintreenode<type> *rightchild (bintreenode<type> *t, const Type key) {bintreenode<type> *p = Search (t, key), if (p = = null) {cout << "The node is not exist!" << endl;return null;} if (P->rightchild = = null) {cout << "This node does have rightchild" << Endl;return NULL;} Elsereturn P->rightchild;} Root Type root (const bintreenode<type> *t) {return t->data;} void Destory (const bintreenode<type> *t) {if (t! = NULL) {destory (t->leftchild);D estory (t->rightchild); Delete t;}} See if the tree is empty bool IsEmpty (const bintreenode<type> *t) {return t = = NULL;} Private:bintreenode<type> *root; Type Ref;};
Page Design:
#include "Bintree.h" int main () {bintree<char> bt (' # '); int select = 1;char c;while (select) {cout << ********* "<< endl;cout <<" * [1] creat [2] Preo Rder [3] inorder * "<< endl;cout <<" * [4] postorder [5] Height [6] Size * "<< endl;cout <<" * [7] search [8] preorder_find [9] inorder_find * "<< Endl;co UT << "* [ten] postorder_find [all] parent [] leftchild *" << endl;cout << "* [] R Ightchild [[] root [] destory * "<< endl;cout <<" * [+] Isempty [+] quit_s Ystem * "<< 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 << "Please enter the What U want to search:"; Cin >> c;cout << BT. Search (c) << endl;break;case 8:cout << "Please enter what you want to search:"; Cin >> c;cout << BT.P Reorder_find (c) << endl;break;case 9:cout << "Please enter the What U want to search:"; Cin >> C;cout << ; Bt. Inorder_find (c) << endl;break;case 10:cout << "Please enter the What U want to search:"; Cin >> C;cout <&L T Bt. Postorder_find (c) << endl;break;case 11:cout << "whose Parent U wanna Find:"; Cin >> c;cout << BT.P Arent (BT. Search (c)) << endl;break;case 12:cout << "whose Leftchild u wanna find:"; Cin >> c;cout << BT. Leftchild (c) << endl;break;case 13:coUT << "whose Rightchild u wanna find:"; Cin >> c;cout << BT. Rightchild (c) << endl;break;case 14:cout << bt. Root () << endl;break;case 15:BT. Destory (); Break;case 16:if (BT. IsEmpty ()) {cout << "It is an empty bintree" << Endl;} Else{cout << "It isn't an empty bintree" << Endl;} Break;case 17:bt.quit_system (select); break;default:break;}} return 0;}
To find height:
Middle Sequence Traversal:
Middle order Traversal Lookup:
Infer whether it is an empty tree:
To find the parent node:
Post-post traversal:
Pre-order Traversal:
Exit System:
Ask for the right child:
Seek root:
Find:
To find the number of nodes:
"Data Structure" binary tree (c + +)