CSU-1005

来源:互联网 发布:数据分析师月薪 编辑:程序博客网 时间:2024/06/06 05:07

1005: Binary Search Tree analog

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 723  Solved: 148
[Submit][Status]

Description

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

1. each node has a Key value, which can be used to compare with each other.

2. For every node in the tree, every Key value in its left subtree is smaller than its own Key value.

3. 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

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

l  Visit the root.

l  Traverse the left subtree.

l  Traverse the right subtree.

 

l  To traverse a non-empty binary tree in inorder (symmetric), perform the following operations recursively at each node:

l  Traverse the left subtree.

l  Visit the root.

l  Traverse the right subtree.

 

l  To traverse a non-empty binary tree in postorder, perform the following operations recursively at each node:

l  Traverse the left subtree.

l  Traverse the right subtree.

l  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 figure:


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

二叉排序树。

#include<iostream>#include<stdio.h>#include<stdlib.h>#include <string.h>using namespace std;typedef struct BitNode{int data;struct BitNode *lchild,*rchild;}BitNode,*Bintree;int a[10000];int flag=0;int i=0;void insert(Bintree &T,int n){if(T->data==0){T->data=n;}else if(T->data>n){if(T->lchild==NULL){Bintree Temp;Temp=(Bintree)malloc(sizeof(BitNode));Temp->lchild=NULL;Temp->rchild=NULL;Temp->data=n;T->lchild=Temp;}else{insert(T->lchild,n);}}else {if(T->rchild==NULL){Bintree Temp;Temp=(Bintree)malloc(sizeof(BitNode));Temp->lchild=NULL;Temp->rchild=NULL;Temp->data=n;T->rchild=Temp;}else{insert(T->rchild,n);}}}void pretravel(Bintree &T){if(T){a[flag++]=T->data;pretravel(T->lchild);pretravel(T->rchild);}}void midtravel(Bintree &T){if(T){midtravel(T->lchild);a[flag++]=T->data;midtravel(T->rchild);}}void posttravel(Bintree &T){if(T){posttravel(T->lchild);posttravel(T->rchild);a[flag++]=T->data;}}int main(){int T;cin>>T;while(T--){int num;int n;cin>>num;int T1=num;Bintree t;t=(Bintree)malloc(sizeof(BitNode));t->data=0;t->lchild=NULL;t->rchild=NULL;while(T1--){cin>>n;insert(t,n);}memset(a,0,sizeof(a));flag=0;i=0;pretravel(t);for(i=0;i<num-1;i++){cout<<a[i]<<' ';}cout<<a[num-1]<<endl;memset(a,0,sizeof(a));flag=0;i=0;midtravel(t);for(i=0;i<num-1;i++){cout<<a[i]<<' ';}cout<<a[num-1]<<endl;memset(a,0,sizeof(a));flag=0;i=0;posttravel(t);for(i=0;i<num-1;i++){cout<<a[i]<<' ';}cout<<a[num-1]<<endl;cout<<endl;}return 0;}


0 0
原创粉丝点击