C ++ leads binary tree for shortest common Father's Day
# Include
# Include
# Include
Using namespace std; class Expection // a custom exception class {public: void Null_Thing () // NULL pointer exception. {cout <"Expection !!!, This is null "<
Struct Node {Type data; Node
* Left; Node
* Right; bool ltag; // The bool type is used for storing clues. Bool rtag; Node (Type d = Type (): data (d), left (NULL), right (NULL), ltag (false), rtag (false) {}}; template
Class SBTree {public: SBTree () {root = NULL;} void Insert (const char * s) {Insert (root, s);} void Printf () {Printf (root);} void Init_Thread () // clues of Binary Trees. {Node
* Pre = NULL; _ Init_Thread (root, pre);} Node
* Fist () {return Fist (root);} void Init_SetDList () // construct a two-way linked list with root as the header Node. {_ Init_SetDList (root);} Node
* Find (Type val) // Find the node. If it is found, it is normal. If it cannot be found, an exception is thrown. {Node
* P = NULL; try {p = Find (root, val);} catch (Expection exp) {exp. Null_Thing ();} return p;} Type GetValue (Node
* T) // extract the Type value of the node. {Try {t = Find (t-> data);} catch (Expection exp) {exp. null_Thing () ;}return t-> data;} Type getcommp arent (Type val1, Type val2) // get the shortest common Father's Day for the two nodes. This is what I want to focus on, all others are auxiliary. {if (_ getcommp arent (root, val1, val2) return _ getcommp arent (root, val1, val2)-> data;} private: Node
* _ Getcommp arent (Node
* T, Type val1, Type val2) {stack
*> St; if (find_comatrix arent (t, val1) & find_comatrix arent (t, val2) st. push (t); while (1) {if (find_commp arent (t-> left, val1) & find_commp arent (t-> left, val2 )) {t = t-> left; st. push (t);} else if (find_commp arent (t-> right, val1) & find_commp arent (t-> right, val2) {t = t-> right; st. push (t);} else {if (st. empty () = false) {t = st. top (); // use a stack. It seems that it is inconsistent with my original intention. St. pop (); // I did not use recursion to figure out the tragedy. I want to vomit and understand the idea. But the code is always faulty. Alas, let me think twice. Return t;} else return NULL; }}} bool find_commp arent (Node
* T, Type val) {if (t = NULL) return false; if (t-> data = val) return true; else {bool BOOL = find_comatrix arent (t-> left, val); if (BOOL = true) return true; BOOL = find_comatrix arent (t-> right, val ); if (BOOL = true) return true;} Node
* Find (Node
* T, Type val) {Node
* 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) return p; else throw Expection ();} void _ Init_SetDList (Node
* T) {Node
* P = NULL; try {p = First (t);} catch (Expection exp) {exp. Null_Thing () ;}root = p; while (p! = NULL) {cout <
Data <"; p = p-> right ;}} Node
* First (Node
* T) {if (t = NULL) throw Expection (); else while (t-> left! = NULL) {t = t-> left;} return t;} bool _ Init_Thread (Node
* & T, Node
* & Pre) {if (t = NULL) {return true;} _ Init_Thread (t-> left, pre); if (pre! = NULL & pre-> right = NULL) {pre-> right = t; pre-> rtag = true;} if (t! = NULL & t-> left = NULL) {t-> left = pre; t-> ltag = true;} pre = t; _ Init_Thread (t-> right, pre);} bool Insert (Node
* & T, const char * & s) {if (* s = '#') {t = NULL; return true;} else {t = new Node
(* S); Insert (t-> left, ++ s); Insert (t-> right, ++ s) ;}} void Printf (Node
* T) {if (t! = NULL) {cout <
Data <"\ t"; Printf (t-> left); Printf (t-> right);} private: Node
* Root;}; int main () {char str [] = "ABCD ### EF ## G ## HI ## J # K ##"; SBTree
Sb; sb. Insert (str); // sb. Init_Thread (); // sb. Find ('2'); cout <