CSU 1005: Binary Search Tree analog

1005: Binary Search Tree analog

     Time Limit: 1 Sec     Memory Limit: 128 Mb     Submitted: 825     Solved: 173    

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


解题思路:题目要求按照给出的数据构建一个二叉树,这个二叉树要满足,右子结点大于父节点,左子节点小于父节点。最后按照前、中、后三种遍历的方法输出。。。

前序遍历:遍历顺序是先根节点,再左子树,最后右子树

中序遍历:遍历顺序是先左子树,再根节点,最后右子树

后序遍历:遍历顺序是先左子树,再右子树,最后根节点


代码如下:

#include 
#include 
#include 
using namespace std;
typedef struct node* tree;
typedef struct node* tree_lr;
struct node{
    int k;
    tree_lr l,r;
};
int in[1001],pre[1001],post[1001];
int pre_i,in_i,post_i;
tree bulid_tree(tree t,int x)
{
    if(t==NULL) {
        t=(tree)malloc(sizeof(struct node));///将申请的空间的地址强制转化为 ree 指针类型,并赋值
        t->k=x;
        t->l=t->r=NULL;
    }else if(x >= t->k) t->r = bulid_tree(t->r,x);
    else t->l=bulid_tree(t->l,x);
    return t;
}
void pre_tree(tree t)///前序遍历
{
    if(t==NULL) return;
    pre[pre_i++]=t->k;
    pre_tree(t->l);
    pre_tree(t->r);
}
void in_tree(tree t)///中序遍历
{
    if(t==NULL) return;
    in_tree(t->l);
    in[in_i++]=t->k;
    in_tree(t->r);
}
void post_tree(tree t)///后序遍历
{
    if(t==NULL) return;
    post_tree(t->l);
    post_tree(t->r);
    post[post_i++]=t->k;
}
void print(int s[],int n)///输出函数
{
    for(int i=0;i


你可能感兴趣的:(树,OJ)