C # Data Structure --- delete a binary search tree

Source: Internet
Author: User
Deletion process:

Public VoidDelnode (IntX)//The deleted node value isXNode

{

Treenode P, Q, R, T;

P = root;

Q =Null;// PPoint to the node to be compared,QIsPForward node

While(P! =Null& P. Data! = X)

{

If(X <p. Data)

{

Q = P;

P = P. leftnode;

}

Else

{

Q = P;

P = P. rightnode;

}

}

If(P =Null)

Console. Write ("error ");

Else If(P. leftnode =Null)//No left decision tree at the deleted Node

{

If(Q =Null)

T = P. rightnode;

Else If(Q. leftnode = P)

Q. leftnode = P. leftnode;

Else

Q. rightnode = P. rightnode;

}

Else

{

//Find the rightmost node in the left decision tree of the deleted node, that is, the value is exactly lessXNode

R = P. leftnode;

While(R. rightnode! =Null)

R = R. rightnode;

R. rightnode = P. rightnode;

If(Q =Null)

T = P. rightnode;

Else If(Q. leftnode = P)

Q. leftnode = P. leftnode;

Else

Q. rightnode = P. rightnode;

}

}< br> in this instance, the data type is int. You can change it to the icomparable interface to implement any type.

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.