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