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. Is it possible to create @Around Aspect for feign.Client

    https://github.com/spring-cloud/spring-cloud-openfeign/issues/59 看到github 有人问这个问题,做了个示例: import org. ...

  2. 20155230 2016-2017-2《Java程序设计》第六周学习总结

    20155230 2016-2017-2 <Java程序设计>第6周学习总结 教材学习内容总结 Java将输入/输出抽象化为串流,数据有来源及目的地,衔接两者的是串流对象. 从应用程序角度 ...

  3. extern函数声明(转)

    转自:chao_yu extern 函数声明 常常见extern放在函数的前面成为函数声明的一部分,那么,C语言的关键字extern在函数的声明中起什么作用? 答案与分析: 如果函数的声明中带有关键字 ...

  4. 【leetcode 简单】 第八十五题 两个数组的交集 II

    给定两个数组,编写一个函数来计算它们的交集. 示例 1: 输入: nums1 = [1,2,2,1], nums2 = [2,2] 输出: [2,2] 示例 2: 输入: nums1 = [4,9,5 ...

  5. Java编程思想 4th 第2章 一切都是对象

    Java是基于C++的,但Java是一种更纯粹的面向对象程序设计语言,和C++不同的是,Java只支持面向对象编程,因此Java的编程风格也是纯OOP风格的,即一切都是类,所有事情通过类对象协作来完成 ...

  6. js之事件冒泡和事件捕获及其阻止详细介绍

    虽然精通jquery,但对它的原型javascript却不是很了解,最近在学习javascript中遇到了一些困难,比如冒泡和捕获,很多次被提到,但又不知究竟应用在何处.找到了一些好文章解惑,在这里分 ...

  7. 2016.07.15——istringstream测试

    istringstream测试 1.istringstream strcin(str),字符串(str)可以包括多个单词,单词之间使用空格分开 #include "stdafx.h" ...

  8. elasticsearch-head插件安装的一些坑!es6.5.4版本

    折腾了一晚上,总算成功了!,大部分坑都记录了下来,版本升级太快真实个大坑,每个版本都不一样,学的心累!! 这坑太多了!主要就是以下几点最主要的: 因为我这里只使用hear安装,不使用哪个打包工具,所以 ...

  9. jQuery.Validate 验证,以及 remote验证, 多参数传递

    jQuery.Validate 验证: http://www.runoob.com/jquery/jquery-plugin-validate.html 教程网址,很简单, 今天主要在这里记录一下re ...

  10. gcc 编译 + 选项【转】

    转自:http://blog.csdn.net/princess9/article/details/6567678 一般来说要现有项目中的编译选项,设置新的project的编译选项 编译器 就是将“高 ...