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 Input1
5
3 6 9 5 1
Sample Output3 1 6 5 9
1 3 5 6 9
1 5 9 6 3
根据题意建立二叉树,编写insert函数递归将数据插入二叉树中,接下来基本的BST
#include
using namespace std;
struct tree
{
int v;
struct tree *left;
struct tree *right;
}*root,*t;
int pre,mid,lat,ro;
void pre_order(tree *p)
{
if (p!=NULL)
{
if (pre==0)
cout<v;
else cout<<" "<v;
pre++;
pre_order(p->left);
pre_order(p->right);
}
}
void mid_order(tree *p)
{
if (p!=NULL)
{
mid_order(p->left);
if (mid==0)
cout<v;
else cout<<" "<v;
mid++;
mid_order(p->right);
}
}
void lat_order(tree *p)
{
if (p!=NULL)
{
lat_order(p->left);
lat_order(p->right);
if (lat==0)
cout<v;
else cout<<" "<v;
lat++;
}
}
void insert(int k,tree *p)
{
if (ro==0)
{
p->v=k;
p->left=NULL;
p->right=NULL;
ro++;
}
else if(kv)
{
if (p->left==NULL)
{
t=new tree;
t->left=NULL;
t->right=NULL;
t->v=k;
p->left=t;
}
else insert(k,p->left);
}
else if(k>=p->v)
{
if (p->right==NULL)
{
t=new tree;
t->left=NULL;
t->right=NULL;
t->v=k;
p->right=t;
}
else insert(k,p->right);
}
}
void delet(tree *p)
{
int i;
if (p->left!=NULL) delet(p->left);
if (p->right!=NULL) delet(p->right);
delete p;
}
int main()
{
int T,n,i,a;
cin>>T;
while (T--)
{
pre=0;
mid=0;
lat=0;
ro=0;
cin>>n;
root=new tree;
for (i=0;i>a;
insert(a,root);
}
pre_order(root);
cout<