【CSU-1005】解题报告(数据结构,树入门,2018暑选入门B题)

原始题目

1005: Binary Search Tree analog

Time Limit: 1 Sec
Memory Limit: 128 Mb
Submitted: 1630
Solved: 473

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

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

题目大意

构建二叉搜索树,然后将树按前序,中序,后序输出。 # 解题思路 构建二叉树,写函数三种序输出即可。树入门题。 # 解题代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <cstdio>
#include <cstring>
#include <iostream>
#include <set>
#include <queue>
#include <map>
#include <algorithm>
using namespace std;
typedef long long ll;
const int maxn=1e5+5;
struct node
{
int x;
struct node *left,*right;
}list[maxn];
int t,i,n,x,flag1=1;
struct node root,*p;

void Insert(struct node *r,struct node *p)
{
if(!r) return;
if(r->x>p->x)
{
if(r->left) Insert(r->left,p);
else r->left=p;
}
else
{
if(r->right) Insert(r->right,p);
else r->right=p;
}
}

void pretral(struct node *p)
{
if(!p) return;
if(flag1) printf("%d",p->x),flag1=0;
else printf(" %d",p->x);
pretral(p->left);
pretral(p->right);
}

void intral(struct node *p)
{
if(!p) return;
intral(p->left);
if(flag1) printf("%d",p->x),flag1=0;
else printf(" %d",p->x);
intral(p->right);
}

void postod(struct node *p)
{
if(!p) return;
postod(p->left);
postod(p->right);
if(flag1) printf("%d",p->x),flag1=0;
else printf(" %d",p->x);
}


int main()
{

scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
scanf("%d",&root.x);
root.left=root.right=0;
for(i=0;i<n-1;i++)
{
scanf("%d",&x);
p=&list[i];
p->x=x;
p->left=p->right=0;
Insert(&root,p);
}
flag1=1;
pretral(&root);
printf("\n");
flag1=1;
intral(&root);
printf("\n");
flag1=1;
postod(&root);
printf("\n");
printf("\n");
}
}

# 收获与反思

数据结构入门,二叉树熟悉,水题。