Basic structure and its tools
typedefintDatabype;#definePrintdivide cout << Endl << "##########################################" << Endl;classnode{ Public: Databype v; Node*l; Node*R; Node () {v=0; L=NULL; R=NULL; }Private:};typedef Node*pn;template<classT>voidExchange (T & F, T &s) {T T=F; F=s; S=t;};
Initializing a binary tree with a vector
voidBs::initwithivsbylevel (vector<databype>vs) {Queue<PN>tqn; if(!Vs.empty ()) {M_nroot=NewNode; M_nroot->v = vs[0]; Tqn.push (M_nroot); for(inti =1; I <vs.size ();) {PN cn=Tqn.front (); Tqn.pop (); PN TN=NewNode; TN->v = vs[i + +]; CN->l =TN; Tqn.push (TN); if(I <vs.size ()) {TN=NewNode; TN->v = vs[i + +]; CN->r =TN; Tqn.push (TN); } } }}
Print level
void int Level ) { if (NULL = = root )return; if (0 = = level ) << root->v ""; Printlevel (Root1); Printlevel (Root1);}
Get the depth of the tree
int Bs::getlevel (const PN root) { if (NULL = = root )return0 ; return 1 ;}
Layer Traversal Print binary tree
void BS::p rintself () { int alllevel = getlevel (m_nroot); for (int0; i < alllevel; + + i) { Printlevel (m_nroot, i); << Endl; }}
Recursive first order, middle order, sequential traversal of binary tree
voidBS::p reorder_recursion (ConstPN Root) { if(NULL! =root) {cout<< Root->v <<" "; Preorder_recursion (Root-l); Preorder_recursion (Root-R); }}voidBs::inorder_recursion (ConstPN Root) { if(NULL! =root) {Inorder_recursion (Root-l); cout<< Root->v <<" "; Inorder_recursion (Root-R); }}voidBS::p ostorder_recursion (ConstPN Root) { if(NULL! =root) {Postorder_recursion (Root-l); Postorder_recursion (Root-R); cout<< Root->v <<" "; }}
Non-recursive first-order traversal
voidBS::p reorder_no_recursion (ConstPN Root) {Stack<PN>SPN; PN TPN=Root; while(NULL! = TPN | |!Spn.empty ()) { while(NULL! =TPN) {cout<< Tpn->v <<" "; Spn.push (TPN); TPN= tpn->l; } if(!Spn.empty ()) {TPN=Spn.top (); Spn.pop (); TPN= tpn->S; } }}
Non-recursive mid-order traversal
voidBs::inorder_no_recursion (ConstPN Root) {Stack<PN>SPN; PN TPN=Root; while(NULL! = TPN | |!Spn.empty ()) { while(NULL! =TPN) {Spn.push (TPN); TPN= tpn->l; } if(!Spn.empty ()) {TPN=Spn.top (); cout<< Tpn->v <<" "; Spn.pop (); TPN= tpn->R; } }}
Non-recursive post-traversal
voidBS::p ostorder_no_recursion (ConstPN Root) {Stack<PN>SPN; PN TPN=NULL; PN Pre=NULL; Spn.push (root); while(!Spn.empty ()) {TPN=Spn.top (); if((Null = = Tpn->l && NULL = = tpn->r) | | (pre = NULL && (pre = = Tpn->l | | pre = tpn->r)) {cout<< Tpn->v <<" "; Pre=TPN; Spn.pop (); } Else { if(NULL! = tpn->r) Spn.push (TPN-R); if(NULL! = tpn->l) Spn.push (TPN-l); } }}
Binary tree inversion (recursive and non-recursive)
voidbs::reverse_recursion (PN root) {if(NULL! =root) {Exchange (Root->l, root->R); Reverse_recursion (Root-l); Reverse_recursion (Root-R); }}voidbs::reverse_no_recursion (PN root) {Queue<PN>QPN; PN TPN=Root; Qpn.push (root); while(!Qpn.empty ()) {TPN=Qpn.front (); Qpn.pop (); Exchange (TPN->l, tpn->R); if(NULL! = tpn->l) Qpn.push (TPN-l); if(NULL! = tpn->r) Qpn.push (TPN-R); }}
Get the number of K-tier nodes
int Const int k) { if0) return 0 ; if (0 = = k )return1; return 1 1 );}
Whether to include nodes
BOOL Const Const Databype & o) { if (NULL = = Root | | (o! = root->v &&!find_node (root->l, O) &&!find_node (root->R, O) ) return false ; return true ;}
Recent Public Ancestors
PN BS::GETLCP (ConstPN Root,ConstDatabype & F,ConstDatabype &s) { if(NULL = = Root | | f = = ROOT->V | | s = = root->v)returnRoot; if(Find_node (root->L, F)) { if(Find_node (root->R, s)) returnRoot; returnGETLCP (root->L, F, s); } if(Find_node (root->R, F)) { if(Find_node (root->L, s)) returnRoot; returnGETLCP (root->R, F, s); } returnNULL;}
Binary Tree to Linear table
pn bs::to_linklist_recursion (pn root) {if(NULL = =root)returnNULL; PN TL= To_linklist_recursion (root->l); PN Head=Root; if(NULL! =TL) {Head=tl; while(tl->r) TL= tl->R; TL->r =Root; Root->l =tl; } PN TR= To_linklist_recursion (root->R); Root->r =TR; if(NULL! =TR) TR->l =Root; returnhead;}
Basic operation of Binary tree