#include <iostream>#include <Stdlib.H>#include <Stack>Using namespace Std;class expection//A custom exception class{ Public:voidNull_thing ()//null pointer exception.{cout<<"Expection!!!, this is null"<<Endl Exit (0); }};template<TypeNameType>struct node{Type Data; Node<Type> *Left Node<Type> *Right BOOL Ltag;//Clue I store with bool type. BOOL Rtag; Node (TypeD= Type()):Data(d), left (NULL), Right (NULL), Ltag (false), Rtag (false) {}};template<TypeNameType>Class sbtree{ Public: Sbtree () {root= NULL; }voidInsert (const char*s) {Insert (root,s); }voidprintf () {printf (root); }voidInit_thread ()//Two The clue of the fork tree. {Node<Type> *Or:= NULL; _init_thread (Root,pre); } Node<Type> *Fist () {returnFist (root); }voidInit_setdlist ()//Constructs a doubly linked list, with Root as the head node.{_init_setdlist (root); } Node<Type> *Find (TypeVal//Find node, find it normal, throw exception if not found. {Node<Type> *P= NULL; try {P=Find (Root,val); } catch (Expection exp) {EXP.Null_thing (); }returnP }TypeGetValue (Node<Type> *T//Remove the type value of the node. {try {T=Find (t -Data); } catch (Expection exp) {EXP.Null_thing (); }returnT -Data; }TypeGetcommparent (TypeVal1,TypeVAL2)//Get two nodes of the shortest public fathers node, this is what I want to focus on, and the rest are auxiliary,{if(_getcommparent (ROOT,VAL1,VAL2))return_getcommparent (ROOT,VAL1,VAL2) -Data; }Private: Node<Type>*_getcommparent (Node<Type> *TTypeVal1,TypeVAL2) {Stack<Node<Type>* >Qtyif(Find_commparent (T,VAL1)&&Find_commparent (T,val2)) St.Push (t); while(1) {if(Find_commparent (t -LEFT,VAL1)&&Find_commparent (t -Left,val2)) {T=T -Left St.Push (t); }Else if(Find_commparent (t -RIGHT,VAL1)&&Find_commparent (t -Right,val2)) {T=T -Right St.Push (t); }Else{if(St.Empty ()==false) {T=St.Top ();//stack it up, as if I didn't start with the original intention. St.Pop ();//Tragedy I did not use recursion to get out, think I want to vomit, thought is understood, but the code is always a problem, alas, let me reconsider. returnT }Else return NULL; }}} bool Find_commparent (Node<Type> *TTypeVal) {if(t==NULL)return false;if(t -Data ==Valreturn true;Else{BOOL BOOL=Find_commparent (t -Left,val);if(BOOL==true)return true; BOOL=Find_commparent (t -Right,val);if(BOOL== true)return true; }} Node<Type>*Find (Node<Type> *TTypeVal) {Node<Type> *P= NULL; try {P=First (t); } catch (Expection exp) {EXP.Null_thing (); } while(p!=NULL) {if(p -Data ==val) break; P=P -Right }if(p!=NULL)returnPElseThrow Expection (); }void_init_setdlist (Node<Type> *T) {Node<Type> *P= NULL; try {P=First (t); } catch (Expection exp) {EXP.Null_thing (); } root=P while(p!=NULL) {cout<<P -Data<<" "; P=P -Right }} Node<Type> *First (Node<Type> *T) {if(t==NULL) throw expection ();Else while(t -Left!=NULL) {T=T -Left }returnT } bool _init_thread (Node<Type> *&T,node<Type> *&Pre) {if(t==NULL) {return true; } _init_thread (t -LEFT,PRE);if(Pre!= NULL &&Pre -Right==NULL) {Pre -Right=T Or: -Rtag= true; }if(t!=NULL &&T -Left==NULL) {T -Left=Pre T -Ltag= true; } Pre=T _init_thread (t -RIGHT,PRE); } bool Insert (Node<Type> *&T,const Char*&s) {if(*S==' # ') {T= NULL;return true; }Else{T= NewNode<Type>(*s); Insert (t -Left++s); Insert (t -Right++s); } }voidPrintf (Node<Type> *T) {if(t!=NULL) {cout<<T -Data<<"\ T"; Printf (t -left); Printf (t -right); } }Private: Node<Type> *Root;}; int main () {char str[]= "abcd## #EF # #G # #HI # #J #k##"; Sbtree<char> SB; Sb. Insert (str); Sb. Init_thread (); Sb. Find (' 2 '); COUT<<SB. Getcommparent (' C ', ' D ') <<endl; Sb. Init_setdlist (); Constructs a doubly linked list. Sb. Printf (); return 0;}
C + + clue two fork tree to find the shortest common fathers node