CSUOJ 1005 Binary Search Tree analog
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
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
#define MAXN 1005
int pre[MAXN], in[MAXN], post[MAXN];
int cnt;
struct node{
int element;
node* left;
node* right;
};
node* insert(node* t, int x)
{
if (t == NULL)
{
t = (node*)malloc(sizeof(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 preoder(node* t)
{
if (t == NULL)
return;
pre[cnt++] = t->element;//根
preoder(t->left);
preoder(t->right);
}
void inorder(node* t)
{
if (t == NULL)
return;
inorder(t->left);
in[cnt++] = t->element;//根
inorder(t->right);
}
void postorder(node* t)
{
if (t == NULL)
return;
postorder(t->left);
postorder(t->right);
post[cnt++] = t->element;//根
}
void print(int *x,int n)
{
printf("%d", x[0]);
for (int i = 1; i < n; i++)
printf(" %d", x[i]);
printf("\n");
}
int main()
{
int T,n,x;
while (~scanf("%d", &T))
{
while (T--)
{
scanf("%d", &n);
node* tree=NULL;
for (int i = 0; i < n; i++)
{
scanf("%d", &x);
tree=insert(tree, x);
}
cnt = 0; preoder(tree);
cnt = 0; inorder(tree);
cnt = 0; postorder(tree);
print(pre,n); print(in,n); print(post,n);
cout << endl;
}
}
return 0;
}
/**********************************************************************
Problem: 1005
User: leo6033
Language: C++
Result: AC
Time:32 ms
Memory:2828 kb
**********************************************************************/
CSUOJ 1005 Binary Search Tree analog的更多相关文章
- Binary Search Tree analog
Description Binary Search Tree, abbreviated as BST, is a kind of binary tree maintains the following ...
- 04-树6 Complete Binary Search Tree
完全二叉树 刚开始只发现了中序遍历是从小到大顺序的.一直在找完全二叉树的层结点间规律...放弃了 不曾想,完全二叉树的规律早就知道啊.根结点为i,其左孩子结点2*i, 右孩子结点2*i+1. 结合此两 ...
- Pat(Advanced Level)Practice--1043(Is It a Binary Search Tree)
Pat1043代码 题目描写叙述: A Binary Search Tree (BST) is recursively defined as a binary tree which has the f ...
- 【PAT】1043 Is It a Binary Search Tree(25 分)
1043 Is It a Binary Search Tree(25 分) A Binary Search Tree (BST) is recursively defined as a binary ...
- 1064. Complete Binary Search Tree (30)【二叉树】——PAT (Advanced Level) Practise
题目信息 1064. Complete Binary Search Tree (30) 时间限制100 ms 内存限制65536 kB 代码长度限制16000 B A Binary Search Tr ...
- pat 甲级 1064 ( Complete Binary Search Tree ) (数据结构)
1064 Complete Binary Search Tree (30 分) A Binary Search Tree (BST) is recursively defined as a binar ...
- PAT-1064 Complete Binary Search Tree(完全二叉树)
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...
- [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法
二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...
- Leetcode 笔记 99 - Recover Binary Search Tree
题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...
随机推荐
- python 三种遍历列表里面序号和值的方法
list = ['html', 'js', 'css', 'python'] # 方法1 # 遍历列表方法1:' for i in list: print("序号:%s 值:%s" ...
- Mother's Mil 母亲的牛奶
Description 农民约翰有三个容量分别是A,B,C升的桶,A,B,C分别是三个从1到20的整数,最初,A和B桶都是空的,而C桶是装满牛奶的.有时,约翰把牛奶从一个桶倒到另一个桶中,直到被灌桶装 ...
- URL访问 和命名规范
手册:http://v9.help.phpcms.cn/html/2010/structure_0928/71.html http://yourdomain.com/index.php?m=conte ...
- python 操作 memcache
目录 Memcached Memcached安装 python操作Memcached Memcache模块常用方法 Memcached Memcached是一个高性能的分布式内存对象缓存系统,用于动态 ...
- python collection 中的队列
认识中的队列 在以前的认知里,队列是先进先出,就是一头进,一头出,Queue.而无意间看到了deque 双向队列. 即从该队列的头或者尾部都能插入和移除元素.而起时间复杂度竟然是一样的!O(1),是不 ...
- IE安全系列之——RES Protocol
IE安全系列之--RES Protocol res Protocol用于从一个文件里面提取指定资源.语法为:res://sFile[/sType]/sID 各Token含义: sfile:百分号编码. ...
- Dream------Java--ant zip 对压缩文件进行指定位置的修改
ant zip 对压缩文件进行指定位置的修改 实现功能: 对2中文件进行修改: 需求: 在XX文件中,从二进制流的200字节位置开始,往后的30位字节数量.插入一个值 由于涉及到公司内部,不方便写太多 ...
- shell监控网站是否自动运行并自动重启【原创】
shell监控网站是否自动运行并自动重启 #!/bin/bash -T www.baidu.com ];then echo "`date` 网站访问正常!" >> /r ...
- HDU 6212 Zuma 2017青岛网络赛 区间DP
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6212 解法:看了眼题就发现这个BZOJ 1032不是一毛一样?但是BZOJ上那是个巨坑,数据有错,原来 ...
- 记录一款Unity VR视频播放器插件的开发
效果图 先上一个效果图: 背景 公司最近在做VR直播平台,VR开发我们用到了Unity,而在Unity中播放视频就需要一款视频插件,我们调研了几个视频插件,记录两个,如下: Unity视频插件调研 网 ...