Data structure and algorithm--two fork sorting tree and code implementation __ Two fork sort tree

Source: Internet
Author: User
Two fork Sorting tree introduction

We know that the binary tree, each node has a maximum of 2 Shang trees, known as the Saozi right subtree.
The binary sort tree, apparently also a binary tree, has a more salient feature that renders sorting on the data field .
In the two-fork sort tree , the data field of the left subtree of each tree is less than its root node value, and the data field of the right subtree of each tree is greater than its root node value, and the left and right subtree of each tree is a two-fork sort tree . It is easy to get from its characteristics, and the sequence traversal of a two-fork sort tree must be in ascending order . Code Implementation Ideas

Binary sort tree, related algorithms mainly include: Create two-fork sorting tree node of the query node to delete the insertion point

Inquire. Very good implementation, recursion point query key value is equal, not equal to the recursive query chant, recursive export node==null, query to the node through the outgoing parameters returned, no query to return the key value of the nearest node .

Insert. According to the binary sorting characteristics of the insert chant, you can invoke the query interface , if the binary sort tree does not have the node, then allow insertion, the insertion node to hang to the return node .

Delete. Delete will be divided into the situation, the 3rd kind of situation a bit around, beginners to good combination of code understanding. To delete the node, only the Zoozi tree, you can delete a node directly with its Zuozi , because the node on its left subtree is smaller than it is, and it still conforms to the binary sort. The node, only the right subtree, can be used directly to replace the delete node with its right subtree , Because it's on the right subtree to delete the node, the left and right subtree exist, how is good . Because after we delete the node, we still have to satisfy the characteristics of the binary sort tree, each node is smaller than the Supi, and the right subtree is larger than the tree. Now it's time to think of the features mentioned above, the sequence traversal of the two-row sorted tree must be ascending, and the data field of the node can be replaced with the precursor node of the middle sequence traversal or the data field of the back-drive node . There is wood, Note here only replaces the data field, because the original logical relation also wants to retain , therefore cannot release the deletion node the memory, but releases is replaces the predecessor node or the rear drive node .

Create a two-fork sort tree. Using the insertion algorithm directly to achieve Bai, just started as an empty binary tree, using the insertion algorithm, you can generate two-fork sorting tree. Implementation Code

#define _crt_secure_no_warnings #include <stdio.h> #include <stdlib.h>//Two fork tree node typedef struct BINODE {i
    NT data;
struct binode* lchild, *rchild;

}binode,*bitree;


typedef enum bool{False,true}bool;
    In binary tree sequence traversal void Traversemid (Bitree root) {if (root = NULL) {return;
    } traversemid (Root->lchild);
    printf ("%d", root->data);
Traversemid (Root->rchild);
}/* Two fork sort tree lookup two fork root node parent tree parent node node outgoing parameter, find the node pointer, did not find the node for the closest key lookup key keyword * * BOOL Searchbst (bitree root,bitree parent,binode** node,int key) {if (root = NULL)//The header has been found, has not been found, return the most
        The pointer to the node of the near key {*node = parent;
    return false; else {if (Key < root->data)//key smaller than root, search root's left subtree {return SEARCHBST
        (Root->lchild, root, node, key); else if (Key > Root->data)//key larger than root, search root right subtree {return searchBST (Root->rchild, Root, node, key);
            else//key = = Root->data, find key to return {*node = root;
        return true;
    }}//insert bool Insertbst (bitree root, int data) {binode* node = NULL;
    if (Searchbst (root, NULL, &node, data))////has the same node in the tree, it is not allowed to insert {return false;
        else {Binode * NewNode = (binode*) malloc (sizeof (Binode));
        Newnode->lchild = Newnode->rchild = NULL;
        Newnode->data = data;
        if (Data < Node->data) {node->lchild = NewNode;
        else {node->rchild = NewNode;
    return true;
    }//Specific Delete node action bool Deletenode (bitree* node) {bitree tmp, Pre, pre_parent;
        if ((*node)->lchild = = null)//deletes the left subtree of the node as NULL, replaces the right subtree, releases the deleted node memory {tmp = *node;                                
        *node = (*node)->rchild; Free (tmp);
        else if ((*node)->rchild = = null)//delete the right subtree of the node is null, replace the left subtree, release the delete node memory {tmp = *node;
        *node = (*node)->lchild;
    Free (TMP); else//delete the left and right subtree of the node is not NULL, delete the node data for its predecessor or subsequent data node is not released, here replaced by the predecessor {Pre_p
        arent = *node;                   Pre = pre_parent->lchild; The pre deletes the middle sequence of the node to traverse the precursor node while (pre->rchild!= NULL)//while the loop to find the largest node in the left subtree {pre_
            parent = pre;
        Pre = pre->rchild;
            } if (pre = = (*node)->lchild) {(*node)->data = pre->data;
        (*node)->lchild = pre->lchild;
            else {(*node)->data = pre->data;
        Pre_parent->rchild = pre->lchild;
    Free (pre);
return true;
    }//delete bool Deletebst (bitree *root, int key) {if (root = NULL) return false; if (key = = (*root)->daTA) {return deletenode (root);
    else if (Key < (*root)->data) {return Deletebst (& (*root)->lchild, key);
    else {return Deletebst (& (*root)->rchild, key);
    }///Using binary tree insert operation, create two fork sort tree void Createbst (Bitree *tree) {int num;
    printf ("Please enter the root node data:");
    scanf ("%d", &num);
    *tree = malloc (sizeof (Binode));
    (*tree)->data = num;
    (*tree)->lchild = (*tree)->rchild = NULL;
        while (1) {printf ("Please enter the node data (-1 end):");
        scanf ("%d", &num);
        if (num = = 1) {break;
    } insertbst (*tree, num);
    {The int main (int argc, char *argv[]) {bitree binarysorttree;//two-fork sort Tree createbst (&binarysorttree);
    int menu, Key,ret;
    binode* node;
        while (1) {printf ("----menu----------------\ n");
        printf ("----1. Sequence traversal binary sort tree \ n");
        printf ("----2. Find node \ n");
        printf ("----3. Insert node \ n");
  printf ("----4. Delete node \ n");      printf ("----5. Exit \ n");
        scanf ("%d", &menu);
            Switch (menu) {case 1:traversemid (binarysorttree);
            printf ("\ n");
        Break
            Case 2:printf ("Please enter key:");
            scanf ("%d", &key);
            ret = Searchbst (Binarysorttree, NULL, &node, key); ret = true?
            printf ("%d exists \ n", key): printf ("%d not present \ n", key);
        Break
            Case 3:printf ("Please enter insert data:");
            scanf ("%d", &key);
            ret = Insertbst (Binarysorttree,key); ret = true?
            printf ("%d insert succeeded \", key): printf ("%d insert failed \ n", key);
        Break
            Case 4:printf ("Please enter delete data:");
            scanf ("%d", &key);
            ret = Deletebst (&binarysorttree,key); ret = true?
            printf ("%d delete succeeded \ n", key): printf ("%d delete failed \ n", key);
        Break
        Case 5:exit (0);
} return 0;
 }
Run Detection


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.