CSU->1005: Binary Search Tree analog

1005: Binary Search Tree analog

            Time Limit: 1 Sec     Memory Limit: 128 Mb   

Description

Binary Search Tree, abbreviated as BST, is a kind of binary tree maintains the following property:

each node has a Key value, which can be used to compare with each other.
For every node in the tree, every Key value in its left subtree is smaller than its own Key value.
For every node in the tree, every Key value in its right subtree is equal to or larger than its own Key value.
Now we need to analog a BST, we only require one kind of operation: inserting.

First, we have an empty BST. Input is a sequence of numbers. We need to insert them one by one flowing the rules below:

If the inserted value is smaller than the root’s value, insert it to the left subtree.

If the inserted value is larger than or equal to the value of the root’s value, insert it to the right subtree.

After each input, we need to output the preorder, inorder, postorder traversal sequences.

About tree traversal, the following is from Wikipedia:

Depth-first Traversal

To traverse a non-empty binary tree in preorder, perform the following operations recursively at each node, starting with the root node:

Visit the root.
Traverse the left subtree.
Traverse the right subtree.
To traverse a non-empty binary tree in inorder (symmetric), perform the following operations recursively at each node:

Traverse the left subtree.
Visit the root.
Traverse the right subtree.
To traverse a non-empty binary tree in postorder, perform the following operations recursively at each node:

Traverse the left subtree.
Traverse the right subtree.
Visit the root.
Look at the folowing example:

Intput is a sequence of 5 integers: 3 6 9 5 1

After each integer inserted the structure of the tree is illustrated in the flowing:

3
/ \
1 6
/ \
5 9

Input

The first integer of the input is T, the number of test cases. Each test case has two lines. The first line contain an integer N,(1<=N<=1000), the number of numbers need to be inserted into the BST. The second line contain N integers separated by space, each integer is in the range of [0,230].

Output

Each test case, output must contain three lines: the preorder, inorder and postorder traversal sequence. The numbers in each line should be separated by a single space and you should not output anything at the end of the line! Output a blank line after each case.

Sample Input

1
5
3 6 9 5 1

Sample Output

3 1 6 5 9
1 3 5 6 9
1 5 9 6 3

Hint

Source

中南大学第五届大学生程序设计竞赛-热身赛

题目链接:http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1005

题解:就是一个二叉搜索树(具体可看这篇博客)。

AC代码:

#include<iostream>
#include<cstring>
#include<stdlib.h>
#define maxn 1005
using namespace std;

int t,n,tmp,cnt,pre[maxn],in[maxn],post[maxn];
struct node{
    int key;
    struct node *lchild;
    struct node *rchild;
};
struct node* tree;

void Init(){
    memset(pre,-1,sizeof(pre));
    memset(in,-1,sizeof(in));
    memset(post,-1,sizeof(post));
}

struct node* insert(struct node* T,int element){
    if(T==NULL){
        T=(struct node*)malloc(sizeof(struct node));
        T->key=element;
        T->lchild=T->rchild=NULL;
    }
    else if(element>T->key){
        T->rchild=insert(T->rchild,element);
    }
    else if(element<=T->key){
        T->lchild=insert(T->lchild,element);
    }
    return T;
}

void preTravesal(struct node *T){
    if(T==NULL){
        return;
    }
    else{
        pre[cnt++]=T->key;
        preTravesal(T->lchild);
        preTravesal(T->rchild);
    }
}
void inTravesal(struct node *T){
    if(T==NULL){
        return ;
    }
    else{
        inTravesal(T->lchild);
        in[cnt++]=T->key;
        inTravesal(T->rchild);
    }
}
void postTravesal(struct node *T){
    if(T==NULL){
        return;
    }
    else{
        postTravesal(T->lchild);
        postTravesal(T->rchild);
        post[cnt++]=T->key;
    }
}
void print(int *a){
    printf("%d",a[0]);
    for(int i=1;i<n;i++){
        printf(" %d",a[i]); 
    }
    cout<<endl;
}

int main(){
    scanf("%d",&t);
    while(t--){
        Init();
        scanf("%d",&n);
        tree=NULL;
        for(int i=0;i<n;i++){
            scanf("%d",&tmp);
            tree=insert(tree,tmp);
        }
//      delete_BST(&tree, 5);
        cnt=0;preTravesal(tree);print(pre);
        cnt=0;inTravesal(tree);print(in);
        cnt=0;postTravesal(tree);print(post);
        puts("\n"); 
    }
    return 0;
} 

猜你喜欢

转载自blog.csdn.net/wuyileiju__/article/details/81074948