Example analysis of a specified node in a delete tree

Source: Internet
Author: User

Tree *delete (Tree *t,intN) {    //function to delete the specified node in the treeTree *tmp; if(t==NULL)returnNULL; if(t->element==N) {if(T->right==null)//If there is no right subtree, there is only one node for the son left dial hand tree{//is deleted directlytmp=u; T=t->left;//Replace parent node with left Dial hand tree node             Free(TMP); }        Else                                     //if the right sub-tree exists{tmp=t->Right ;  while(tmp->left!=NULL) TMP=tmp->left;//Find the node with the lowest value of the right subtreet->element=tmp->element;//Replace the value of the original node with the value of the nodeT->right=delete (t->right,tmp->element);//recursively delete nodes in the right subtree instead of the source node valuesT->height = MAX (height (t->left), height (t->right)) +1; }        returnT; }    if(N < t->Element) T->left = Delete (t->left,n); ElseT->right = Delete (t->right,n); T->height = MAX (height (t->left), height (t->right)) +1; returnT;}


This is explained in the following example, assuming that you want to delete node 2,

, to delete node 2, first see Node 2 there is no right subtree, there is a right subtree, find its right sub-tree value of the smallest node, so found 3; Assign 3 to Node 2, now becomes the 2nd drawing; The next one is to recursively delete the 3 nodes in its right subtree.
5:3 large, so left recursive found 3;3 has right subtree 4, so to find 3 of the right subtree the value of the smallest node. 4 is its only value and must be its smallest node. So I gave 4 to 3 and became the 3rd picture; The following is to delete the node 4 assigned to it in the right subtree of 4. The first one is 4, good coincidence, to see if there is no right subtree, no, guanggansiling one; Delete the node directly, assign NULL to 4, and become the 4th picture.

Example analysis of a specified node in a delete 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.