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 ...
随机推荐
- Coffeescript的安装与编译
安装 npm install -g coffee-script 在cmd中输入coffee可以进入coffeescript的命令行模式(REPL),然而到我写完这篇博文为止,我觉得这并没有什么卵用 C ...
- AngularJS 、Backbone.js 和 Ember.js 的比较
1 介绍 我们准备在这篇文章中比较三款流行于Web的“模型-视图-*”框架:AngularJS.Backbone和Ember.为你的项目选择正确的框架能够对你及时交付项目的能力和在以后维护你自己代码的 ...
- 【CodeForces】914 F. Substrings in a String bitset
[题目]F. Substrings in a String [题意]给定小写字母字符串s,支持两种操作:1.修改某个位置的字符,2.给定字符串y,查询区间[l,r]内出现y多少次.|s|,Σ|y|&l ...
- 【转】 jquery easyui datagrid使用,分页、排序、查询
$('#dg').datagrid({ url: "xxx.ashx", pagination: true, p ...
- promise顺序执行,返回结果存放在数组
遇到面试的一个编程题:三个返回promise对象的异步操作,让你写一个函数可以将这些操作顺序执行,并返回一个数组包含三个异步对象的结果 异步对象: // 异步函数a var a = function ...
- 【译】第十二篇 SQL Server代理多服务器管理
本篇文章是SQL Server代理系列的第十二篇,详细内容请参考原文 在这一系列的上一篇,我们查看了维护计划,一个维护计划可能会创建多个作业,多个计划.你还简单地看了SSIS子系统,并查看了维护计划作 ...
- jquery ajax complete 方法
jquery ajax var ajaxTimeoutTest = $.ajax({ url:'', //请求的URL timeout : 1000, //超时时间设置,单位毫秒 type : 'g ...
- Linux环境下如何查看内存CPU和GPU使用情况以及界面标题栏实现
查看内存和CPU 单独查看内存使用情况的命令:free -m 查看内存及cpu使用情况的命令:top 也可以安装htop工具,这样更直观, 安装命令如下:sudo apt-ge ...
- Spring4笔记4--基于XML的DI(依赖注入)
基于XML的DI(依赖注入): Bean 实例在调用无参构造器创建了空值对象后,就要对 Bean 对象的属性进行初始化.初始化是由容器自动完成的,称为注入.根据注入方式的不同,常用的有两类:设值注入. ...
- 更改arch的默认终端
实在是厌倦了gnome的资源管理器nautilus和终端gnome-terminal,于是卸载之,然后更换了xfce4的终端,但是出现了一个问题,那就是在资源管理器中使用邮件打开终端的时候打不开了,解 ...