Binary Tree-Two forks find tree-avl Tree

Source: Internet
Author: User
Tags benchmark

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.