CSUOJ 1005 Binary Search Tree analog

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
**********************************************************************/

全部评论

相关推荐

不愿透露姓名的神秘牛友
昨天 12:31
以前小时候我最痛恨出轨、偷情的人,无论男女,为什么会出轨?现在我成了自己最讨厌的人,没想到分享的东西在牛客会被这么多人看,大家的评价都很中肯,我也认同,想过一一回复,但我还是收声了,我想我应该说说这件事,这件事一直压在我心里,是个很大的心结,上面说了人为什么出轨,我大概能明白了。我们大一下半年开始恋爱,开始恋爱,我给出了我铭记3年的承诺,我对她好一辈子,我永远不会背叛,我责任心太重,我觉得跟了我,我就要照顾她一辈子,我们在一起3年我都没有碰过她,她说往东我就往东,她说什么我做什么,她要我干什么,我就干什么!在学校很美好,中途也出过一些小插曲,比如男闺蜜、男闺蜜2号等等等。但我都强迫她改掉了,我...
牛客刘北:两个缺爱的人是没有办法好好在一起的,但世界上哪有什么是非对错?你后悔你们在一起了,但是刚刚在一起的美好也是真的呀,因为其他人的出现,你开始想要了最开始的自己,你的确对不起自己,21岁的你望高物远,你完全可以不谈恋爱,去过你想要的生活,你向往自由,在一起之后,你要想的不是一个人,而是两个人,你不是变心了,就像你说的,你受够了,你不想包容了,冷静几天是你最优的选择,爱人先爱己。
社会教会你的第一课
点赞 评论 收藏
分享
零OFFER战士:另一个版本查看图片
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务