Implement two fork tree and its basic operation//header file # include <iostream>using namespace Std;template<class type>class bintree;template <class Type>class bintreenode{friend class bintree<type>;p ublic:bintreenode ():d ATA (Type ()), Leftchild ( NULL), Rightchild (null) {}bintreenode (Type D, bintreenode<type> *left = null, bintreenode<type> *right = null ): Data (d), Leftchild (left), Rightchild (right) {}//~bintreenode ();p rivate: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 ();p ublic: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)//creation Tree {Type input;cin >> input;if (input = = R EF) {t = NULL;} Else{t = new bintreenode<type> (input); Creatbintree (T->leftchild); Creatbintree (T->rightchild);}} void Preorder (const bintreenode<type> *T)//Pre-order {if (t = = NULL) {return;} Else{cout << T->data << ""; Preorder (T->leftchild); Preorder (T->rightchild);}} void Inorder (const bintreenode<type> *t)//middle order {if (t = = NULL) {return;} Else{inorder (t->leftchild); cout << t->data << ""; Inorder (T->rightchild);}} void Postorder (const bintreenode<type> *t)//post-post {if (t = = NULL) {return;} Else{postorder (T->leftchild); Postorder (t->rightchild); cout << t->data << "";}} int height (const bintreenode<type> *t)//Tree Height {if (t = = NULL) return 0;return (height (t->leftchild) >heig HT (t->rightchild))? (Height (T->leftchild) +1):( Height (T->rightchild) +1);} int size (const bintreenode<type> *t)//The size of the tree {if (t = = NULL) return 0;else{return (Size (t->leftchild) +size ( T->rightchild) (+1);}} bintreenode<type> * Search (bintreenode<type>* T, Type const K)//Find {if (t = = NULL) return null;if (t->d ATA = = 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)//pre-order lookup {if (t = = NULL) return NU Ll;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)//middle order Lookup {if (t = = NULL) return NUL L Bintreenode<type> *p;if (p = inorder_find (T->leftchild, key))! = NULL); else if (t->data = = key) return T;elsei Norder_find (T->rightchild, key);} bintreenode<type>* Postorder_find (bintreenode<type>* t, Type const key)//post-search {if (t = = NULL) return N ULL; Bintreenode<type> *p; Bintreenode<type> *q;if (p = postorder_find (T->leftchild, key))! = NULL); else if (q = Postorder_find (T->rig Htchild, key)) = NULL); ElseIf (t->data = = key) return t;} bintreenode<type>* Parent (bintreenode<type>* T, Bintreenode<type>* q)//Find parent Node {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)//find left child {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)//Find right child {if (t = = NULL) return null; Bintreenode<type>*p = Search (t, key), if (P->rightchild = = NULL) return Null;return (P->rightchild);} int Quit_system ()//exit {return 0;} bintreenode<type>* root (bintreenode<type>* t)//root node {return t;} BOOL IsEmpty (bintreenode<type>* t) {return t = = NULL;} void Destory (bintreenode<type>* t) {if (t!= NULL) {destory (t->leftchild);D estory (t->rightchild);d elete t;}}private:bintreenode<type> *root; Type Ref;}; #include "Bintree.h"//main function 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 << "* [ten] postorder_find [one] parent [] leftchild *" << endl;cout << "* [[] rightchild [] root [] destory * "<< endl;cout <<" * [+] 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 << "Tree size:" << bt. Height () << endl;break;case 6:cout << "Tree size:" << bt. Size () << endl;break;case 7:cout << "Enter the node to find:", cin >> item;cout << BT. Search (Item) << endl;break;case 8:cout << "Enter the node to find:"; Cin >> item;cout << BT. Preorder_find (Item) << endl;break;case 9:cout << "Please enter the node to find:"; Cin >> item;cout << BT. Inorder_find (Item) << endl;break;case 10:cout << "Please enter the node to find:"; Cin >> item;cout << BT. Postorder_find (Item) << endl;break;case 11:cout << "Please enter the node to find:"; Cin >> item;cout << "Parent node:" < < BT. Parent (BT. Search (Item) << endl;break;case 12:cout << "Please enter the node to find:"; Cin >> item;cout << "left child:" << bt. LeftchiLD (Item) << endl;break;case 13:cout << "Please enter the node to find:"; Cin >> item;cout << "right Child:" << bt. Rightchild (Item) << endl;break;case 14:cout << "root node:" << bt. Root () << endl;break;case 15:BT. Destory (); Break;case 16:if (BT. IsEmpty ()) cout << "The Tree is empty" << endl;elsecout << "tree is not empty" << endl;break;case 17:bt.quit_system ( Select); break;default:break;}} return 0;}
"Data structure" implements a two-fork tree and its basic operations