Binary lookup tree Insert, delete, find _c language

Source: Internet
Author: User

A binary lookup tree is a two-pronged tree that satisfies the following conditions:
1, all node values on the Zoozi tree are small root node values,
2, all node values on the right subtree are not small root node values,
3, the left and right subtree also meet the above two conditions.

The insert process for the binary lookup tree is as follows:
1. If the current two-fork lookup tree is empty, the inserted element is the root node,
2. If the inserted element value is less than the root node value, the element is inserted into the left subtree.
3. Inserts elements into the right subtree if the element value is not a small root node value.

Binary lookup tree deletion, divided into three kinds of conditions for processing:
1.P is a leaf node, delete the node directly, and then modify the pointer to its parent node (note that the root node and not the root node), as shown in figure A.

2.P is a single branch node (that is, only Zuozi or right subtree). Let P's subtree be connected to P's father node, and delete p (note that the root node and not the root node);

3.P Saozi Right subtree is not empty. Find the successor Y of P, because Y must have no left subtree, so you can delete y and let Y's father node become the Father node of Y's right subtree, substituting the value of Y for the value of P, or method two is to find the precursor of P x,x must not have a right subtree, so you can delete x and let X's father node become the Father node of Y's left subtree. As shown in figure C.

To insert the node's code:

Copy Code code as follows:

struct node
{
int Val;
Pnode Lchild;
Pnode Rchild;
};

Pnode BT = NULL;


Recursive method inserts a node
Pnode Insert (pnode root, int x)
{
Pnode p = (pnode) malloc (LEN);
P->val = x;
P->lchild = NULL;
P->rchild = NULL;
if (root = = NULL) {
root = P;
}
else if (x < Root->val) {
Root->lchild = insert (root->lchild, x);
}
else{
Root->rchild = insert (root->rchild, x);
}
return root;
}

Non-recursive method inserts a node
void Insert_bst (Pnode q, int x)
{
Pnode p = (pnode) malloc (LEN);
P->val = x;
P->lchild = NULL;
P->rchild = NULL;
if (q = = NULL) {
BT = p;
return;
}
while (Q->lchild!= p && q->rchild!= p) {
if (x < Q->val) {
if (q->lchild) {
Q = q->lchild;
}
else{
Q->lchild = p;
}
}
else{
if (q->rchild) {
Q = q->rchild;
}
else{
Q->rchild = p;
}
}
}
Return
}


to find the code for a node:
Copy Code code as follows:

Pnode Search_bst (pnode p, int x)
{
bool solve = false;
while (P &&!solve) {
if (x = = P->val) {
Solve = true;
}
else if (x < P->val) {
p = p->lchild;
}
else{
p = p->rchild;
}
}
if (p = = NULL) {
cout << "not Found" << x << Endl;
}
return p;
}

to delete a node's code
Copy Code code as follows:

BOOL Delete_bst (pnode p, int x)//Return a flag indicating whether the deleted element is found
{
bool find = false;
Pnode q;
p = BT;
while (P &&!find) {//Search for deleted elements
if (x = = P->val) {//Find deleted element
find = true;
}
else if (x < P->val) {//go along Zuozi
Q = p;
p = p->lchild;
}
else{//Search along the right son Tree
Q = p;
p = p->rchild;
}
}
if (p = = NULL) {//not found
cout << "not Found" << x << Endl;
}

if (P->lchild = = NULL && P->rchild = null) {//p is a leaf node
if (p = = BT) {//p as root node
BT = NULL;
}
else if (q->lchild = = p) {
Q->lchild = NULL;
}
else{
Q->rchild = NULL;
}
Free (p); Release Node P
}
else if (P->lchild = null | | p->rchild = = NULL) {//p is a single branch subtree
if (p = = BT) {//p as root node
if (P->lchild = = NULL) {
BT = p->rchild;
}
else{
BT = p->lchild;
}
}
else{
if (q->lchild = = P && p->lchild) {//p is the left subtree of Q and P has Zuozi
Q->lchild = p->lchild; Link the left subtree of p to the left pointer of Q
}
else if (q->lchild = = P && p->rchild) {
Q->lchild = p->rchild;
}
else if (q->rchild = = P && p->lchild) {
Q->rchild = p->lchild;
}
else{
Q->rchild = p->rchild;
}
}
Free (p);
}
The left and right subtree of else{//p is not empty
Pnode t = p;
Pnode s = p->lchild; Starting from the left child node of P
while (S->rchild) {//Find the precursor of P, that is, the node with the largest p China value
t = s;
s = s->rchild;
}
P->val = s->val; Assign the value of the node S to P
if (t = = p) {
P->lchild = s->lchild;
}
else{
T->rchild = s->lchild;
}
Free (s);
}
return to find;
}

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.