Insert and delete operations for binary lookup trees

Source: Internet
Author: User

structtreenode{Searchtree left;    Searchtree right; ElementType Ele;};/*there must be an exit for recursion .*//*The recursive code is to be reused*/Searchtreeinsert (Searchtree T, X) {/*This is not the parameter in the tree, increasing the node*/    if(T = =NULL) {T=malloc(sizeof(structTreeNode)); if(T = =NULL) fatalerror (); T->ele =X; T->left =NULL; T->right =NULL:}/*continue to compare*/    Else    if(T->ele >X) T->left = Insert (T->left, X);//for no left child node, the new node pointer is returned, and if so, it is equivalent to nothing when recursive returns    Else    if(T->ele <X) T->right = Insert (t->Right , X); returnT;} Searchtreefindmin (Searchtree T) {if(T = =NULL)returnNULL;//for the first time only    if(T->left! =NULL)returnFindmin (t->Left ); returnT;}//Two-fork find tree removal routinesSearchtreedelete (Searchtree T, ElementType X) {S earchtree Tmpcell; if(T = =NULL) Error ("no Element found"); if(T->ele >X) T->left = Delete (t->Left , X); Else if(T->ele <X) Delete (T-Right , X); Else if(T->right && t->Left ) {Tmpcell= Findmin (t->Right ); T->ele = tmpcell->Ele; Delete (Tmpcell, T-Ele); }    Else//Supply 1. Delete parent node with only one or no sons 2. Delete Right subtree minimum node    {        if(T->left = =NULL) T= t->Right ; Else if(T->right = =NULL) T= t->Left ; }    returnT;}
View Code

Insert and delete operations for binary lookup trees

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.