Binary tree data structure (II) binary search tree

Data Structure (XI)

Finishing learning experiences and knowledge of data structures and algorithms in the process, so that I find myself, and I hope you can share with everyone.

- binary search trees -

1. Title Description

In the binary search tree with a dynamic look-up table is particularly useful, a random sequence may be ordered into a sequence by constructing a binary search tree, the tree is the process of constructing the sequence of random ordering process. Each time a new node is inserted is binary search tree new leaf nodes, the insertion operation is performed without moving the other nodes, only one node pointer changes from empty to non-empty i.e. can.
Here, we want to explore and establish a sequence of binary output.

1.1 input

Only one row, comprising a plurality of digits, separated by a space. (Numbers may overlap)

1.2 Output

Output line, as a result of the establishment of the preamble traveled binary search tree for the digital input.

1.3 Sample input and output

Sample input
41467334500169 724,478,358,962,464 705,145,281,827,961 491,995,942,827,436
sample output
41467334169145 281,358,464,436,500 478 491 724 705 962,827,961,942,995

2. code implementation

c

#include <stdio.h>
#include <stdlib.h>

typedef struct node
{
    int date;
    struct node *left;
    struct node *right;
}node;
//建立树
node *insert(node *root,int x)
{
    //第一个节点,初始化
    if(root==NULL)
    {
        root=(node*)malloc(sizeof(node));
        root->date=x;
        root->left=NULL;
        root->right=NULL;
        return root;
    }
    if(x==root->date)
        return root;
    if(x<root->date)
    {
        root->left=insert(root->left,x);
    }
    else
    {
        root->right=insert(root->right,x);
    }
    return root;
}
//前序遍历
void pre(node *root)
{
    if(root==NULL)
        return;
    printf("%d ",root->date);
    pre(root->left);
    pre(root->right);
}
int main ()
{
    int num[1000]={0};
    node *root=NULL;
    for(int g=0;g<100;g++)
    {
        scanf("%d",&num[g]);
        if(num[g]==0)
            break;
        root=insert(root,num[g]);
    }
    pre(root);

    return 0;
}

3. Code Description

Search this question is to examine the tree of knowledge, according to the definition of a binary search tree can be achievements, pay attention to the order before the order traversal.
Three traversal rule Click introduction viewing.

Binary search tree :

  • If its left subtree is not empty, then the value of the left sub-tree, all nodes areLess thanThe value of the root;
  • If its right subtree is not empty, then the value of the right sub-tree nodes are allmore than theThe value of the root;
  • Its left and right subtrees are also binary sort tree.

Binary search tree related algorithms :

  • Looking (b find x in the binary tree in the process):

    • If b is an empty tree, the search fails, or:
    • If x is equal to the value b of the root domain of the data, the search is successful; otherwise:
    • If the data field is less than the value b of the root node x, then the left subtree search; otherwise:
    • Find the right subtree.
  • Insert (b inserted into the binary tree nodes s procedure):

    • If b is empty tree nodes will be referred to as s root insert otherwise:
    • If s-> data value b is equal to the root node of the data field, is returned, otherwise:
    • If s-> data value b is less than the data fields of the root node, the node pointed to by s put inserted into the left subtree, otherwise:
    • The node pointed to by s inserted into the right subtree.
Published 36 original articles · won praise 10 · views 5939

Guess you like

Origin blog.csdn.net/qq_44867435/article/details/104377264