CSUOJ 1005 Binary Search Tree analog(二叉搜索树模拟)
2017-04-05 17:23
1106 查看
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
Nintegers
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
题目大意:输入一串数字,按照输入顺序建立BST,输出前序、中序和后序遍历的结果。注意输出格式和插入顺序。
代码如下:
#include <stdio.h>
#include <stdlib.h>
#define maxn 1005
int _pre[maxn];
int _in[maxn];
int _post[maxn];
int cnt;
typedef struct node* ptr_to_node;
typedef struct node* tree;
struct node {
int element;
ptr_to_node left;
ptr_to_node right;
};
tree insert(tree t, int x) {
if(t == NULL) {
t = malloc(sizeof(struct node));
t->element = x;
t->right = t->left = NULL;
} else if(x < t->element) {
t->left = insert(t->left, x);
} else if(x >= t->element) {
t->right = insert(t->right,x);
}
return t;
}
void preorder(tree t) {
if(t == NULL) return ;
_pre[cnt++] = t->element;
preorder(t->left);
preorder(t->right);
}
void inorder(tree t) {
if(t == NULL) return ;
inorder(t->left);
_in[cnt++] = t->element;
inorder(t->right);
}
void postorder(tree t) {
if(t == NULL) return ;
postorder(t->left);
postorder(t->right);
_post[cnt++] = t->element;
}
void print(int* p)
{
printf("%d",p[0]);
for(int i = 1;i < cnt;i++)
printf(" %d",p[i]);
putchar('\n');
}
int main(void) {
int n,m,l;
scanf("%d",&n);
while(n--) {
scanf("%d",&m);
tree t = NULL;
for(int i = 0;i < m;i++) {
scanf("%d",&l);
t = insert(t,l);
}
cnt = 0,preorder(t);
cnt = 0,inorder(t);
cnt = 0,postorder(t);
print(_pre),print(_in),print(_post);
putchar('\n');
}
return 0;
}
相关文章推荐
- CSUOJ 1005 Binary Search Tree analog
- csuoj1005: Binary Search Tree analog
- CSU1005-Binary Search Tree analog-模拟
- CSU 1005: Binary Search Tree analog
- CSU-ACM 1005: Binary Search Tree analog
- LeetCode OJ:Recover Binary Search Tree(恢复二叉搜索树)
- COJ-1005-Binary Search Tree analog
- Leetcode Convert Sorted Array to Binary Search Tree 有序数组转换成二叉搜索树BST
- [LeetCode] Trim a Binary Search Tree 修剪一棵二叉搜索树
- Binary Search Tree (二叉搜索树)
- [LeetCode] Convert Sorted List to Binary Search Tree 将有序链表转为二叉搜索树
- [Leetcode] Recover binary search tree 恢复二叉搜索树
- LeetCode OJ 之 Convert Sorted Array to Binary Search Tree(把有序数组转化为二叉搜索树)
- C++数据结构 之 二叉搜索树_Binary Search Tree
- BinarySearchTree二叉搜索树的实现
- LeetCode--108. Convert Sorted Array to Binary Search Tree(将有序数组转为二叉搜索树)Python
- 二叉树系列 - 二叉搜索树 - [LeetCode] 中序遍历中利用 pre节点避免额外空间。题:Recover Binary Search Tree,Validate Binary Search Tree
- LeetCode--Validate Binary Search Tree(二叉搜索树)
- leetCode 98.Validate Binary Search Tree (有效二叉搜索树) 解题思路和方法
- 二叉搜索树(Binary Search Tree )的定义及分析