Delete the node with the key value e from the binary search (SORT) tree with the root node pointer R

Source: Internet
Author: User

Luban software's questions in the written test were searched and found to be soft questions.

From: http://hi.baidu.com/mqgw/blog/item/cade830f1894222c6159f36c.html

From: http://www.examda.com/soft/Programmer/060124/091040110-2.html

 

The deletenode (bitree * r, int e) function is:R's binaryDelete the node with the key value e in the search (SORT) tree. If the node is successfully deleted, the function returns 0; otherwise, the function returns-1. The binary search tree node type is defined:
Typedef struct tnode {
Int data;

Struct tnode * lchild, * rchild;

} * Bitree;
When deleting a knot in the binary search tree, consider the following three situations:
1. If the node P to be deleted is a leaf node, delete it directly;
2. If the node P to be deleted has only one subnode, connect the subnode directly with the parent node of the node to be deleted, and then delete the node P;
3. if the node P to be deleted has two subnodes, search for the node s with the maximum key value in the middle order on its left subtree, and replace the value of node P with the value of node S, then delete node S. node S is one of the above ○ 1 and ○ 2 conditions.

Int deletenode (bitree * r, int e ){
Bitree P = * r, PP, S, C;
While (P & P-> data!= E)

{/* Find the node with the key value e from the root node */
Pp = P;

If (E <p-> data)

P = p-> lchild;
Else

P = p-> rchild;

}
If (! P) Return-1;/* search failed */
If (p-> lchild & P-> rchild)

{/* Processing status 3 */
S = p-> lchild;

Pp = P;
While (S-> rchild)

{

Pp = s;

S = s-> rchild;

}
P-> DATA = s-> data;

P = s;

}
/* Handling situations ○ 1 and ○ 2 */
If (P-> lchild)

C = p-> lchild;

Else

C = p-> rchild;
If (P = * r)

* R = C;
Else

If (P = PP-> lchild)

PP-> lchild = C;
Else

PP-> rchild = C;
Free (P );

Return 0;

}

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.