Basic operation of binary search tree

Source: Internet
Author: User

Binary lookup tree: For each node in the tree X, its left subtree has all keywords that are less than the X keyword, and all the keywords for the right subtree are larger than the X keyword.

The average depth of the binary lookup tree is O (logn).

Binary search Tree Delete operation:

    1. If the node is a leaf, it can be deleted immediately.
    2. If there is a son, the tuning parent node pointer bypasses the node after it is deleted.
    3. If there are two sons, replace the node's data with the smallest data from the right subtree and delete the node recursively (now it is empty). Because the smallest node of the right subtree cannot have a left son, it is easier to delete the second time.
1 #ifndef _tree_h2 3 structTreeNode;4typedefstructTreeNode *Position;5typedefstructTreeNode *Searchtree;6 7 searchtree makeempty (Searchtree T);8 Position Find (ElementType X, Searchtree T);9 Position findmin (Searchtree T);Ten Position Findmax (Searchtree T); One searchtree Insert (ElementType X, Searchtree T); A searcgtree Delete (ElementType X, Searchtree T); - ElementType Retrieve (Position P); -  the #endif -  - structTreeNode - { + ElementType Element; - Searchtree left; + Searchtree right; A } at  -  - searchtree makeempty (searchtree T) - { -     if(T! =NULL) -     { inMakeempty (t->Left ); -Makeempty (t->Right ); to          Free(T); +     } -     returnNULL; the } *  $ Panax Notoginseng Position Find (ElementType X, Searchtree T) - { the     if(T = =NULL) +         returnNULL; A     if(X < t->Element) the         returnFind (X, t->Left ); +     Else if(X > t->Element) -         returnFind (X, t->Right ); $     Else $         returnT; - } -  the  - Position findmin (searchtree T)Wuyi { the     if(T = =NULL) -         returnNULL; Wu     Else if(T->left = =NULL) -         returnT; About     Else $         returnFindmin (t->Left ); - } -  - Position Findmax (searchtree T) A { +     if(T! =NULL) the          while(T->right! =NULL) -T = t->Right ; $     returnT; the } the  the  the searchtree Insert (ElementType X, Searchtree T) - { in     if(T = =NULL) the     { theT =malloc(sizeof(structTreeNode)); About         if(T =NULL) thePerror ("Out of Space"); the         Else the         { +T->element =X; -T->left = T->right =NULL; the         }Bayi     } the     Else if(X < t->Element) theT->left = Insert (X, t->Left ); -     Else -T->right = Insert (X, t->Right ); the  the     returnT; the } the  -  the searchtree Delete (ElementType X, Searchtree T) the { the Position Tmpcell;94  the     if(T = =NULL) thePerror ("Element not found"); the     //Found The element to be deleted98     Else if(X < t->Left ) AboutT->left = Delete (X, t->Left ); -     Else if(X > tRight )101T->right = Delete (X, t->Right );102 103     //Children104     Else if(T->left && t->Right ) the     {106Tmpcell = Findmin (t->Right );107T->element = tmpcell->Element;108T->right = Delete (t->element, t->Right );109     } the     //One or zero children111     Else the     {113Tmpcell =T; the         if(T->left = =NULL) theT = t->Right ; the         Else if(T->right = =NULL)117T = t->Left ;118          Free(Tmpcell);119     } - 121     returnT;122}

Two fork find tree basic operations

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.