二叉查找树的简单创建和3种遍历

创建一棵二叉查找树并分别用先序遍历中序遍历后序遍历输出

其实根据二叉搜索树的特点,中序遍历的结果就是从小到大的顺序输出,但为了练习树我还是用递归输出的

题目:

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

l  To traverse a non-empty binary tree in preorder, perform the following operations recursively at each node, starting with the root node:

l  Visit the root.

l  Traverse the left subtree.

l  Traverse the right subtree.

 

l  To traverse a non-empty binary tree in inorder (symmetric), perform the following operations recursively at each node:

l  Traverse the left subtree.

l  Visit the root.

l  Traverse the right subtree.

 

l  To traverse a non-empty binary tree in postorder, perform the following operations recursively at each node:

l  Traverse the left subtree.

l  Traverse the right subtree.

l  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 figure:

-

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<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
/*
    题意:给出 N 个数,按照顺序依次插入到二叉搜索树,
	然后按照前序,中序,后序依次输出 
	If the inserted value is larger than 
	or equal to the value of the root's value, 
	insert it to the right subtree.
	╮(╯▽╰)╭
	二叉查找树: 左子树<根<右子树 
	先序遍历:根-左-右
	中序遍历:左-根-右
	后序遍历:左-右-根 
*/
typedef long long LL;
struct Node
{
	int val;
	Node *l,*r;
} ;
int a[1005];
int n;
void Insert(Node * &p,int x)//往树里面插入一个元素x
{
	if (p == NULL)//空树 
	{
		p = new Node;
		p->val = x;
		p->l = p->r = NULL;
		return; 
	} 
	if (x < p->val)//如果插入的值小于根的值,插入到左子树
	{
		Insert(p->l,x);
	}
	else//否则插入右子树 PS:   =-= 这个题说了等于根的时候插入右子树 
	{
		Insert(p->r,x);
	}
} 
void buildBST(Node * &T)
{
	T = NULL;
	for (int i = 0;i < n;++i) Insert(T,a[i]);
}
bool first;
void PreOder(Node * T)
{
	if (T)
	{
		if (first)
		{
			cout<<T->val;
			first = 0;
		}
		else 
		{
			cout<<" "<<T->val;
		}
		PreOder(T->l);
		PreOder(T->r);
	}
}
void InOder(Node * T)//中序遍历二叉查找树的话就是顺序输出,这样的话......
{
	if (T)
	{
		InOder(T->l);
		if (first)
		{
			cout<<T->val;
			first = 0;
		}
		else cout<<" "<<T->val;
		InOder(T->r);
	}
}
void AfterOder(Node * T)
{
	if (T)
	{
		AfterOder(T->l);
		AfterOder(T->r);
		if (first)
		{
			cout<<T->val;
			first = 0;
		}
		else cout<<" "<<T->val;
	}
} 
int main()
{
	int T_T;
	cin>>T_T;
	while (T_T--)
	{
		cin>>n;
		for (int i = 0;i < n;++i) scanf("%d",a+i);
		Node * T;
		buildBST(T);
		
		first = 1;
		PreOder(T);
		cout<<endl;
		
		first = 1;
		InOder(T);
		cout<<endl;
		
		first = 1;
		AfterOder(T);
		cout<<endl;
		
		cout<<endl;
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值