One or two fork tree
Definition: Each node cannot have more than two sons of trees.
Binary tree Node declaration:
1 struct TreeNode 2 {3 elementtype element; 4 TreeNode * Left ; 5 TreeNode * Right ; 6 }
Application:
Infix expression--postfix expression (application of stacks)--expression tree (application of stack 2)
Stack Application 2: Read the suffix expression, the operand into the stack, after the operator, point to the stack of the first two elements t1 and t2 the pointer out of the stack (T1 first popped, as the operator's right son), and will point to the operator pointer into the stack.
Two or two fork find tree
Defined:
Structure: two fork tree;
Sorting: Minimum value in right subtree > x keyword > Maximum value in left dial hand tree (true for any node keyword x)
1. Empty the tree (recursive) makeempty
1Searchtree * MAKEEMPTY (Searchtree *T)2 {3 if(T! =NULL)4 {5Makeempty (T-Left );6Makeempty (T-Right );7 Delete(T);//Benchmark Situation8 } 9 returnT; Ten}
2. Find
Searchtree * FIND (ElementType X, Searchtree *T) { if(T =NULL)returnNULL;//non-null judgment if(X < t->Element)returnFind (X, t->Left ); Else if(X > t->Element)returnFind (X, t->Right ); Else returnT//Find element x}
3, Findmin && Findmax (for example, (non) Recursive method, using its sorting to find the corresponding node)
Recursive method:
Searchtree * Findmax ( searchtree * t) { if(t = NULL) return// non-null judge Else if(t->right = = NULL)return T; // Benchmark Situation Else return Findmax (t-> right);}
Non-recursive method:
Searchtree * Findmax ( searchtree * t) { if(t = NULL) return// non-null judgment elsewhile (t->right! = NULL) = t-> right; return T;}
4. Insert
Searchtree * Insert (ElementType X, Searchtree *T) { if(T = =NULL) {T=searchtree New (searchtree); if(T = =NULL) cout<<"Out of space."<<Endl; Else{T->element =X; T->left = T->right =NULL; } } Else if(X < t->Element) T->left = insert (X, t->Left ); Else if(X > t->Element) T->right = insert (X, t->Right ); returnT;}
Not to be continued ...
Binary Tree-Two forks find tree-avl Tree