The basic operation of the C language to realize the two Fork lookup tree (BST)

Source: Internet
Author: User

We explained the two-fork tree in our last blog, and this time we're going to implement the two-fork tree--The two-fork lookup tree (Binary search trees), also called the binary sort Tree (Binary). So the abbreviation is BST. The two-insert lookup tree is defined as follows:

1. If the left subtree is not empty, the value of all nodes on the left subtree is less than the value of its root node;

2. If the right subtree is not empty, the value of all nodes on the right subtree is greater than the value of its root node;

3. The left and right subtree are also two-fork-sorted trees;

An important feature of binary sort trees is that the middle sequence traversal is an ascending sequence. Sample code uploaded to: Https://github.com/chenyufeng1991/BinarySearchTree.


(1) The construction of the node

typedef int ELEMTYPE;
typedef struct btnode{

    int data;
    struct Btnode *lchild;
    struct Btnode *rchild;
} Bitnode,*bitree;


(2) Create two fork tree

The process of creating a two interpolation tree is a process of constantly inserting nodes, and the most important thing is to find the right place to insert.

Create a two-fork lookup tree
/**
 *  Input-1 when the creation of the end, is actually a continuous insert process
 *
/int createbinarysearchtree (Bitree *t) {

    printf ( "Please enter a sequence of numbers to create a two-fork lookup tree: \ n");

    int num;
    scanf ("%d", &num);

    while (Num!=-1) {
        Insert (t,num);
        scanf ("%d", &num);
    }

    printf ("%s function execution, binary lookup tree creation succeeded \ n", __function__);
    return 1;
}

(3) Insert node

Insert node
void Insert (Bitree *t,int x) {

    //Create a node to insert here
    bitnode *pinsert = (Bitnode *) malloc (sizeof (Bitnode)) ;
    Pinsert->data = x;
    Pinsert->lchild = NULL;
    Pinsert->rchild = NULL;

    if ((*t) = = NULL) {
        *t = Pinsert;
    }

    if ((*t)->lchild = = NULL && x < (*t)->data) {
        (*t)->lchild = Pinsert;
    }

    if ((*t)->rchild = = NULL && x > (*t)->data) {
        (*t)->rchild = Pinsert;
    }

    Recursive implementation
    if (x < (*t)->data) {
        Insert (& (*t)->lchild, x);

    if (x > (*t)->data) {
        Insert (& (*t)->rchild, x);

    return;
}

(4) Sequence traversal and first-order traversal of a tree

The first sequence traversal of a tree and the sequence traversal can uniquely determine a tree. And the ordinal traversal of the binary sort tree must be an ascending sequence. In this way, we can verify that we are doing the right thing with a binary sort tree.

In-sequence traversal binary lookup tree
//print should be an incremented sequence
void Middleorder (Bitree t) {
    if (t = = NULL) {return
        ;
    } else{

        Middleorder (t->lchild);
        printf ("%d", t->data);
        Middleorder (T->rchild);
    }

First-order traversal binary lookup tree
//Because first-order traversal + sequence traversal can uniquely determine a tree, and so on can verify that the tree is correct
void preorder (Bitree t) {

    if (t = = NULL) {return
        ;
    }else{

        printf ("%d", t->data);
        Preorder (t->lchild);
        Preorder (t->rchild);
    }

(5) Find elements

Find a value
//return 1 to find the value, and 0 to indicate that the
bitnode *searchvalue (bitree t,int x) {
    if (T = = NULL) {return
        0;
    } is not found. else{
        if (x < t->data) {
            //Chazo tree
            searchvalue (t->lchild, x);
        } else if (x > T->data) {
            //Check right subtree
            searchvalue (t->rchild, x);
        } else{
            //Find the value
            printf ("The memory address for this value is:%p\n", T);
            return T;
        }
    }

    return NULL;
}

(6) Delete element

Deletes an element bitree *deletevalue (bitree *t,int x) {//To find the node first, and also to save the node's parent node Bitnode *searchnode;

    Bitnode *parentnode;
    The initial points are pointed at the root node Searchnode = *t;

    ParentNode = *t;
            while (Searchnode->data!= x) {if (Searchnode->lchild = null && Searchnode->rchild = = null) {
            is already a leaf node, but the value has not been found, indicating that the tree has no node to delete printf ("%s function execution, no value, delete failed \ \ __function__");
        return T;
            } if (x < searchnode->data) {parentnode = Searchnode;
        Searchnode = searchnode->lchild;
            }else if (x > Searchnode->data) {parentnode = Searchnode;
        Searchnode = searchnode->rchild;
        }else{break; } if (Searchnode->lchild = = NULL && Searchnode->rchild = null) {//is a leaf node if (*t
            ->lchild = = null && (*t)->rchild = = null) {//Is the root node free (*t);
 *t = NULL;       }else{//is not a root node, is a normal leaf node//need to determine whether the node to be deleted is the left child of the parent node or the right child if (Searchnode->data <
            Parentnode->data) {//is left child parentnode->lchild = NULL;
            }else{//Is the right child parentnode->rchild = NULL;
            Free (Searchnode);
        Searchnode = NULL;
    } return T; } if (Searchnode->lchild!= null && searchnode->rchild = null) {//have Zoozi tree, no right subtree//Direct parent node
        Pointer to the right subtree, and then release yourself; First you need to determine whether the current node is the left child or the right child of the parent node if (Searchnode->data < Parentnode->data) {//is the left child Paren
        Tnode->lchild = searchnode->lchild;
        }else{//Is the right child Parentnode->rchild = searchnode->lchild;
        Free (Searchnode);

        Searchnode = NULL;
    return T; } if (Searchnode->lchild = = null && searchnode->rchild!= null) {//NoZoozi tree, with right subtree if (Searchnode->data < Parentnode->data) {//is left child Parentnode->lchild
        = searchnode->rchild;
        }else{//Is the right child Parentnode->rchild = searchnode->rchild;
        Free (Searchnode);

        Searchnode = NULL;
    return T; } if (Searchnode->lchild!= null && searchnode->rchild!= null) {//node to be deleted both the left child and the right child/ * * * Algorithm: Delete node P Saozi Right subtree is not empty.
         Find the successor Y of P, since Y must have no left subtree, you can delete y and let Y's parent node become the parent of Y's right subtree, and replace the value of Y with the value for P, and how to find the successor node to delete the node, including the parent node of the successor node. * * Bitnode *nextnode;//Find the successor node to delete the node Bitnode *nextparentnode;//find the parent node of the successor node to delete the node Nextparentnode = s
        Earchnode;
        NextNode = searchnode->rchild;
            while (Nextnode->lchild!= NULL) {nextparentnode = NextNode;
        NextNode = nextnode->lchild; //Here to determine the value size of the NextNode node and the Nextparentnode node, because you need to decide whether to delete the node is the father's left child or right child if (NEXtnode->data < Nextparentnode->data) {//is left child nextparentnode->lchild = NEXTNODE-&GT;RC
        Hild;
        }else{//Is the right child Nextparentnode->rchild = nextnode->rchild;

        }//substituted value Searchnode->data = nextnode->data;
        Deletes the successor node free (nextnode);

        NextNode = NULL;
    return T;
} return T; }


(7) Test code

int main (int argc, const char * argv[]) {

    bitree tree;

    This is the reference pass
    Createbinarysearchtree (&tree);
    
    Middleorder (tree);
    printf ("\ n");

    printf ("Please enter the element to find:");
    int searchvalue;
    scanf ("%d", &searchvalue);
    Searchvalue (tree,searchvalue);


    printf ("Please enter the element to be deleted:");
    int deletevalue;
    scanf ("%d", &deletevalue);
    DeleteValue (&tree, deletevalue);
    printf ("First Order Traversal:");
    Preorder (tree);
    printf ("\ n sequence traversal:");
    Middleorder (tree)//traversal check
    printf ("\ n");
    
    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.