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

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的更多相关文章

  1. Binary Search Tree analog

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

  2. 04-树6 Complete Binary Search Tree

    完全二叉树 刚开始只发现了中序遍历是从小到大顺序的.一直在找完全二叉树的层结点间规律...放弃了 不曾想,完全二叉树的规律早就知道啊.根结点为i,其左孩子结点2*i, 右孩子结点2*i+1. 结合此两 ...

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

  4. 【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 ...

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

  6. pat 甲级 1064 ( Complete Binary Search Tree ) (数据结构)

    1064 Complete Binary Search Tree (30 分) A Binary Search Tree (BST) is recursively defined as a binar ...

  7. PAT-1064 Complete Binary Search Tree(完全二叉树)

    A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...

  8. [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法

    二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...

  9. Leetcode 笔记 99 - Recover Binary Search Tree

    题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...

随机推荐

  1. bzoj 3790 神奇项链(Manacher,DP+BIT | 贪心)

    [题意] 你可以产生一个回文串,也可以将两个串合并成一个串,问产生目标串需要的最少合并次数. [思路] 显然我们要先产生目标串中包含的极大回文字符串. Manacher求出每个位置可以向两边延伸的最长 ...

  2. 【译】SSH隧道:本地和远程端口转发

    本文是:SSH Tunnel - Local and Remote Port Forwarding Explained With Examples 的译文 有两种方法可以创建SSH隧道,本地和远程端口 ...

  3. Error: Cannot retrieve metalink for repository: epel. Please verify its path and try again错误解决

    rpm -ivh http://dl.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm  安装了epel源 但  yum -y ...

  4. Linux下inittab文件详解

    /etc/inittab文件详解 Linux系统的启动过程为:加电自检-->根据BIOS中的设置从指定的设备启动-->找到设备MBR中的bootloader引导启动系统-->启动ke ...

  5. php魔术函数 __clone()

    原文地址: http://www.nowamagic.net/librarys/posts/php/32 PHP4面向对象功能一个很大的缺点,是将对象视为另一种数据类型,这使得很多常见的OOP方法无法 ...

  6. J - Borg Maze +getchar 的使用注意(二维字符数组的输入)

    题目链接: https://vjudge.net/contest/66965#problem/J 具体思路: 首先将每个点之间的最短距离求出(bfs),A 或者 S作为起点跑bfs,这样最短距离就求出 ...

  7. 【SLAM】安装 g2o_viewer

    2017年2月8日,那是一个阴天.为了完成高翔博士的<一起做RGB-D SLAM>教程,我在 Ubuntu 14.04 安装 g2o.遇到困难,怎奈我眼瞎,找错了方向,浪费时间,没有成功安 ...

  8. GridView Postback后出错Operation is not valid due to the current state of the object.

    一.问题起因 最近项目中有一页面第一次search后正常,但是再次点击其它任何按钮都会报错,亦即postback后页面有问题,经检查是由于页面有一GridView且数据量极大,记录大概有上千条,这儿解 ...

  9. 【算法学习】manacher

    manacher太水了. 这篇blog不能称作算法学习,因为根本没有介绍…… 就贴个模板,太简单了…… #include<cstdio> #include<cstring> # ...

  10. Office DDE漏洞学习笔记

    1.前言 2017年下半年爆发出来的Office漏洞,一直没有空做笔记记录.在病毒分析中也看到有利用这个漏洞的样本,针对Office系列软件发起的钓鱼攻击和APT攻击一直是安全攻防的热点. 2.off ...