Binary search Tree--c language description (GO)

Source: Internet
Author: User

Graphical binary search tree concept

Binary tree, in fact, is a two-dimensional form of the list, and binary search tree, is a special two-fork tree, this binary tree has a feature: for any node, the left child (of course, the existence of the words) is always less than itself, and the right child (existence) value is always greater than itself.

The following is an introduction to finding, inserting, and deleting algorithmic ideas in this kind of two-fork tree structure.

Find: Because this structure is to facilitate the search, so find one of the values is very easy, starting from the root, small to the left to find, large to the right, the smaller is the node;

The code is simple, and it's not written here.

Insert: Insert the same reason, starting from the root, small to the left, large to the right, until the leaves, inserted.

Code:

int Insert (bstree *t,data_type Data)//Insert the date {Bstree newnode,p;    NewNode = (bstree) malloc (sizeof (Bsnode));    Newnode->lchild = Newnode->rchild = NULL;    Newnode->data = data;    if (*t = = NULL) {*t = NewNode;        } else {p = *t;            while (1) {if (data = = P->data) {return 0;//value already exists, cannot be inserted}                    else if (Data > P->data) {if (P->rchild = = null)//Right child is empty, insert {                    P->rchild = NewNode;                return 1;                } else//continue looking down {p = p->rchild;                    }} else {if (P->lchild = = null)//left child is empty, insert {                    P->lchild = NewNode;                return 1;          } else//continue looking down {p = p->lchild;      }            }        }    }} 

Delete: And the deletion of the node is more troublesome, is the most difficult link in this structure, because we delete the node is not necessarily a leaf node, is the leaf node is very good to do, but if it is a node in the two fork tree, it is related to the deletion of the connection problem.

The deletion is divided into the following four cases:

1. Delete nodes as leaf nodes: This will not be said, delete is very simple.

2. Delete the node is not a leaf node, its left subtree is not empty, the right subtree is empty: In this case, simply connect its parent node to its right child node, and then delete, for example, 40 of the deletion, that is, connection 30 and 35.

3. Delete the node is not a leaf node, its right subtree is not empty, the left subtree is empty: In this case, simply connect its parent node with its left child node, and then delete, for example, 80 of the deletion, that is, connection 50 and 90

4. Delete the node is not the leaf node, the left and right subtrees are not empty, this is the most complex, we analyze the analysis alone.

In this case, the total idea of deletion is to move the node down until it becomes a leaf. Move rule: That is, the value of the largest child under this node exchange, the deletion of the node into the deletion of the child node operation.

Of course, this happens, after the exchange of this node is still not a leaf node, then continue to follow the rules of the move until the change to the point of the leaf node. Example

First of all, I wrote a delete algorithm, borrowed the lookup function.

My idea is still the previous list of deleted ideas, delete a node need to record the previous node, so I just use the FA record to delete the node's parent node, and then manipulate.

So the borrowed lookup function will not just return the node address, but also return the parent node address.

Bstree Find (Bstree t,data_type data,bstree *target)//Find function and return Father node address {if (T = = NULL)//Two fork tree is empty {*target = null;    return NULL;        } else if (T->data = = data)//The root is the node to find {*target = T;    return NULL;                } else {while (T) {if (t->lchild && t->lchild->data = = data) {                *target = t->lchild;            return T;                 } if (t->rchild && t->rchild->data = = data) {*target = t->rchild;            return T;            } if (Data > T->data) {T = t->rchild;            } if (Data < T->data) {T = t->lchild;    }} return NULL;    }}int Delete1 (bstree *t,data_type data)//delete {Bstree fa,target;    if (*t = = null)//The tree is empty {return 0; } FA = Find (*t,data,&target);//fa is the parent node of the delete node, target is the delete node if (Target->lchild = = null)//delete node left subtree is empty, right subtree is not empty (case where subtree is empty) {if (FA = = NULL && target->rchild            = = NULL)//The tree has only the root node condition {free (*t);            *t = NULL;        return 1;            } if (Fa->rchild = = target) {Fa->rchild = target->rchild;            Free (target);        return 1;            } if (Fa->lchild = = target) {Fa->lchild = target->rchild;            Free (target);        return 1;            }} else if (Target->rchild = = null)//delete node right subtree is empty, left subtree is not empty case {if (Fa->rchild = = target) {            Fa->rchild = target->lchild;            Free (target);        return 1;            } if (Fa->lchild = = target) {Fa->lchild = target->lchild;            Free (target);        return 1;        {if (Target->lchild->rchild = = NULL)//child node maximum node is target->lchild)}} else//left and right subtree is not empty {           Target->data = target->lchild->data;        Return Delete1 (& (Target->lchild), target->data);            } else//child node The maximum node is target->lchild->rchild.....->rchild {Bstree p1,p2;            P2 = target->lchild;            P1 = p2->rchild;                while (p1->rchild! = NULL)//loop finds the maximum node {p2 = p1;            P1 = p1->rchild;            } Target->data = p1->data;        Return Delete1 (& (P2->rchild), p1->data); }    }}

Then after writing to feel his algorithm space is big, seemingly a bit of trouble, so on the Internet to find the relevant two-fork search tree deletion algorithm, found a very subtle and small algorithm, will be recursive with very beautiful.

Of course, the algorithm is similar to the idea, so there should be no big difference in efficiency, but this algorithm recursive thinking with a great, it is worth learning.

It is suggested that the algorithm I wrote above is clear, looking at the following.

A: Delete the leaf node, as long as the parent node link to its pointer is empty. B: Delete a single node. As long as its successor pointer to the link where it is located can be//c: Delete the Double node, the general method is to first the value of its middle order precursor node assigned to the range of the node,//And then delete its central sequence precursor node, if its central sequence precursor node or a double branch node, Continue to do the same operation,//if the leaf node or a single node to do the corresponding operation, if the root node is ended.    int Delete (bstree *t,data_type data) {Bstree temp;    if (Data < (*T)->data)//continue to find {return Delete (& (*t)->lchild,data) in the left subtree;    } if (Data > (*t)->data)//continue to find {return Delete (& (*t)->rchild,data) in the right subtree; }//code to run here, locate data, which is (*t)->data temp = *t;//*t: Delete node address, T: Record the pointer to delete the node address, that is, the left/right pointer to the parent node of the Delete node if ((*t)->lchild = NU        LL)//The left subtree of the delete node is empty, and the entire right subtree is used as the root (which also contains the leaf node condition) {(*t) = (*t)->rchild;        Free (temp);    return 1;        } else if ((*t)->rchild = = null)//The right subtree of the delete node is empty, and the entire Zuozi as the root {(*t) = (*t)->lchild;        Free (temp);    return 1;            else//Delete the left and right subtree of the node is not empty, find the maximum node of Zuozi {if ((*t)->lchild->rchild = = NULL)//maximum node for left child condition {            (*t)->data = (*t)->lchild->data; Delete (& (*t)-> lchild), (*t)->data);            The Else//MAX node is the deepest right child node condition {Bstree p1,p2;            P2 = (*t)->lchild;            P1 = p2->rchild;                while (p1->rchild! = NULL) {P2 = p1;            P1 = p1->rchild;            } (*t)->data = p1->data;        Delete (& (P2->rchild), p1->data); }    }}
2:2 Fork Search Tree Complete code (C language)
#include <stdio.h> #include <stdlib.h> #include <conio.h>typedef int data_type;typedef struct bst{dat    A_type data; struct BST *lchild,*rchild;} Bsnode,*bstree;        Bstree Find (Bstree t,data_type data,bstree *target)//Find function and return Father node address {if (T = = NULL)//Two fork tree is empty {*target = null;    return NULL;        } else if (T->data = = data)//The root is the node to find {*target = T;    return NULL;                } else {while (T) {if (t->lchild && t->lchild->data = = data) {                *target = t->lchild;            return T;                 } if (t->rchild && t->rchild->data = = data) {*target = t->rchild;            return T;            } if (Data > T->data) {T = t->rchild;            } if (Data < T->data) {T = t->lchild;    }} return NULL; }}int Insert (bstree *t,data_type Data)//Insert the date {Bstree newnode,p;    NewNode = (bstree) malloc (sizeof (Bsnode));    Newnode->lchild = Newnode->rchild = NULL;    Newnode->data = data;    if (*t = = NULL) {*t = NewNode;        } else {p = *t;            while (1) {if (data = = P->data) {return 0;//value already exists, cannot be inserted}                    else if (Data > P->data) {if (P->rchild = = null)//Right child is empty, insert {                    P->rchild = NewNode;                return 1;                } else//continue looking down {p = p->rchild;                    }} else {if (P->lchild = = null)//left child is empty, insert {                    P->lchild = NewNode;                return 1;          } else//continue looking down {p = p->lchild;      }}}}}int Delete1 (bstree *t,data_type data)//delete {Bstree fa,target;    if (*t = = null)//The tree is empty {return 0; } FA = Find (*t,data,&target);//fa is the parent node of the delete node, target is the delete node if (Target->lchild = = null)//delete node left subtree is empty, right subtree is not empty (around            Also applies if the subtree is empty) {if (FA = = NULL && Target->rchild = = null)//tree only exists with root node condition {free (*t);            *t = NULL;        return 1;            } if (Fa->rchild = = target) {Fa->rchild = target->rchild;            Free (target);        return 1;            } if (Fa->lchild = = target) {Fa->lchild = target->rchild;            Free (target);        return 1;            }} else if (Target->rchild = = null)//delete node right subtree is empty, left subtree is not empty case {if (Fa->rchild = = target) {            Fa->rchild = target->lchild;            Free (target);        return 1; } if (Fa->lchild = = target) {fA->lchild = target->lchild;            Free (target);        return 1;        {if (Target->lchild->rchild = = NULL)//child node maximum node is target->lchild)}} else//left and right subtree is not empty            {Target->data = target->lchild->data;        Return Delete1 (& (Target->lchild), target->data);            } else//child node The maximum node is target->lchild->rchild.....->rchild {Bstree p1,p2;            P2 = target->lchild;            P1 = p2->rchild;                while (p1->rchild! = NULL)//loop finds the maximum node {p2 = p1;            P1 = p1->rchild;            } Target->data = p1->data;        Return Delete1 (& (P2->rchild), p1->data); }}}//a: Delete the leaf node, as long as the parent node link to its pointer is empty. B: Delete a single node. As long as its successor pointer to the link where it is located can be//c: Delete the Double node, the general method is to first the value of its middle order precursor node assigned to the range of the node,//And then delete its central sequence precursor node, if its central sequence precursor node or a double branch node, Continue to do the same operation,//if the leaf node or a single node to do the corresponding operation, if the root node is ended. int Delete (bstree *t,data_type data) {BSTree temp;    if (Data < (*T)->data)//continue to find {return Delete (& (*t)->lchild,data) in the left subtree;    } if (Data > (*t)->data)//continue to find {return Delete (& (*t)->rchild,data) in the right subtree; }//code to run here, locate data, which is (*t)->data temp = *t;//*t: Delete node address, T: Record the pointer to delete the node address, that is, the left/right pointer to the parent node of the Delete node if ((*t)->lchild = NU        LL)//The left subtree of the delete node is empty, and the entire right subtree is used as the root (which also contains the leaf node condition) {(*t) = (*t)->rchild;        Free (temp);    return 1;        } else if ((*t)->rchild = = null)//The right subtree of the delete node is empty, and the entire Zuozi as the root {(*t) = (*t)->lchild;        Free (temp);    return 1;            else//Delete the left and right subtree of the node is not empty, find the maximum node of Zuozi {if ((*t)->lchild->rchild = = NULL)//maximum node for left child condition {            (*t)->data = (*t)->lchild->data;        Delete (& (*t)->lchild), (*t)->data);            The Else//MAX node is the deepest right child node condition {Bstree p1,p2;            P2 = (*t)->lchild;            P1 = p2->rchild;           while (p1->rchild! = NULL) {P2 = p1;            P1 = p1->rchild;            } (*t)->data = p1->data;        Delete (& (P2->rchild), p1->data);    }}}void Createbstree (Bstree *t)//Create two fork tree, call Insert algorithm to create {data_type data;    Char ch;    printf ("Enter the data for the two-fork search tree you want to create (the data is separated by a space, and the input is completed by pressing ENTER):");        do {scanf ("%d", &data);        ch = getchar ();    Insert (T,data); } while (ch! = 10);}        void Inorder (Bstree t) {if (t) {inorder (t->lchild);        printf ("%d", t->data);    Inorder (T->rchild);    }}int Main () {Bstree t,fa,target;    T = NULL;    Createbstree (&t);    Inorder (T);    int select;    Data_type data;        while (1) {printf ("1. Insert 2. Delete 3. middle order traversal \ n");        scanf ("%d", &select);            if (select = = 1) {printf ("Please enter data to insert:");            scanf ("%d", &data);            Insert (&t,data);            Inorder (T);        Getch (); } else if (select = = 2) {PRintf ("Delete data:");            scanf ("%d", &data);            Delete1 (&t,data);            Inorder (T);        Getch ();            } else if (select = = 3) {inorder (T);        Getch (); }} return 0;} /* 50 30 80 20 40 90 35 85 32 88*/
http://www.cnblogs.com/ecizep/p/4778241.html

Binary search Tree--c language description (GO)

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.