Basic operation routines for binary lookup trees

Source: Internet
Author: User

For the biggest difficulty of binary search tree, I think is to delete, when the deletion of an element, there are two simple cases, one is a node, one is a leaf. Assume that the deleted value is D

1. Leaves, very simple, find the parent node of the leaf, and then the parent node points to the leaf of the branch is assigned to 0; there is a special case of a root, then release the root, then return 0.

2. node, need to do two steps, find the node's left subtree of the maximum value or the minimum value of the right subtree, assuming F, with F instead of the deleted node's value D, and then delete F, and F is definitely a leaf, using the method of 1.

When this algorithm is deleted, the worst case is that the deletion point is a node, the first traversal to find the node D, and then traverse a subtree of that node to find F, and then traverse through to find the parent node of F, logn1+2logn2, less than 3logn, can be raised again is the leaf node and its father can be done at once , without needing two times, the algorithm complexity is 2LOGN. Of course, the complexity of the words can be repeated over and over again to keep all the nodes to be changed, when the complexity of the algorithm is Logn

Here is the implementation code:

  

typedefstruct_node{intelement; struct_node *Lefttree; struct_node *Righttree;} Node;node* Insert (node * root,intElement) {    if(Root = =0) {root= (node *)malloc(sizeof(node)); if(Root = =0)            return 0; Root->element =element; Root->lefttree = Root->righttree =0; }    Else if(Root->element >Element) {Root->lefttree = insert (root->lefttree,element); }    Else if(Root->element <Element) {Root->righttree = insert (root->righttree,element); }    returnRoot;} Node* Find (node * root,intElement) {    if(Root = =0)        return 0; if(Root->element >Element)returnFind (root->lefttree,element); Else if(Root->element <Element)returnFind (root->righttree,element); returnRoot;}voidPrinttree (Node *root) {    if(Root = =0)        return 0; Printtree (Root-lefttree); printf ("%d\t",root->element); Printtree (Root-righttree);} Node* Destroytree (Node *root) {    if(Root = =0)        return 0; Root->lefttree = Destroytree (root->lefttree); Root->righttree = Destroytree (root->righttree);  Free(root); return 0;} Node*findmax (Node *root) {    if(Root = =0)        return 0; if(Root->righttree = =0)        returnRoot; returnFindmax (root->righttree);} Node*findmin (Node *root) {    if(Root = =0)        return 0; if(Root->lefttree = =0)        returnRoot; returnFindmin (root->lefttree);} Node*findparent (Node *root,intElement) {    if(Root = =0)        return 0; Else if(Root->lefttree && Root->lefttree->element = =Element)returnRoot; Else if(Root->righttree && Root->righttree->element = =Element)returnRoot; Else if(Root->element >Element)returnFindparent (root->lefttree,element); Else if(Root->element <Element)returnFindparent (root->righttree,element); Else        return 0xFFFFFFFF;} Node* Deleteelement (Node *root,intElement) {Node*parent; Node*Self ; if(Root = =0)        return-1; Self=find (root,element); if(Self = =0)        return 0; if(self->Lefttree) {Node*tmp = Findmax (self->lefttree); Self->element = tmp->element; Parent= Findparent (self->lefttree,tmp->element); if(Parent = =0xFFFFFFFF) { self->lefttree =0; }        Else{Parent->righttree =0; }         Free(TMP); }    Else if(self->Righttree) {Node*tmp = Findmin (self->righttree); Self->element = tmp->element; Parent= Findparent (self->righttree,tmp->element); if(Parent = =0xFFFFFFFF) { self->righttree =0; }        Else{Parent->lefttree =0; }         Free(TMP); }    Else{Parent= Findparent (root,self->element); if(Parent = =0xFFFFFFFF)        {             Free(root); }        Else        {            if(Parent->lefttree&&parent->lefttree->element = =Element) Parent->lefttree =0; ElseParent->righttree =0;  Free(self); }    }    returnRoot;}intMain () {inta[Ten] = {5,6,8,1,2,9,3,7,4,0}; inti =0; Node*root =0; Node*parent =0; Root= Insert (root,a[0]);  for(i =1;i<Ten; i++) Insert (Root,a[i]); Root= Deleteelement (Root,0); /*For (i = 0;i<11;i++) {parent = Findparent (root,i);        if (parent = = 0xFFFFFFFF) printf ("root =%d\n", i);        else if (parent = = 0) printf ("not exist =%d\n", i);    else printf ("%d ' s parent =%d\n", i,parent->element); }*/Printtree (root);    Destroytree (root); return 1;}

Basic operation routines 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.