题目1201:二叉排序树

题目1201:二叉排序树

时间限制:1 秒

内存限制:32 兆

特殊判题:

提交:6160

解决:2594

题目描述:

    输入一系列整数,建立二叉排序数,并进行前序,中序,后序遍历。

输入:

    输入第一行包括一个整数n(1<=n<=100)。
    接下来的一行包括n个整数。

输出:

    可能有多组测试数据,对于每组数据,将题目所给数据建立一个二叉排序树,并对二叉排序树进行前序、中序和后序遍历。
    每种遍历结果输出一行。每行最后一个数据之后有一个空格。

样例输入:
5
1 6 5 9 8
样例输出:
1 6 5 9 8 
1 5 6 8 9 
5 8 9 6 1 
提示:

输入中可能有重复元素,但是输出的二叉树遍历序列中重复元素不用输出。

#include <iostream>
#include<stdio.h>
#include<string.h>
using namespace std;

struct Node
{
    Node *lchild;
    Node *rchild;
    int c;
} Tree[110];

int loc;
Node *create()
{
    Tree[loc].lchild=Tree[loc].rchild=NULL;
    return &Tree[loc++];
}

void postOrder(Node *T)//后序遍历
{
    if(T->lchild!=NULL)
        postOrder(T->lchild);
    if(T->rchild!=NULL)
        postOrder(T->rchild);
    printf("%d ",T->c);
}

void inOrder(Node *T)//中序遍历
{
    if(T->lchild!=NULL)
        inOrder(T->lchild);
    printf("%d ",T->c);
    if(T->rchild!=NULL)
        inOrder(T->rchild);

}
void preOrder(Node *T)//前序遍历
{
    printf("%d ",T->c);
    if(T->lchild!=NULL)
        preOrder(T->lchild);
    if(T->rchild!=NULL)
        preOrder(T->rchild);
}
Node *Insert(Node *T,int x)
{
    if(T==NULL)
    {
        T=create();
        T->c=x;
        return T;
    }
    else if(x<T->c)
    {
        T->lchild=Insert(T->lchild,x);
    }
    else if(x>T->c)
    {
        T->rchild=Insert(T->rchild,x);
    }
    return T;
}

int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        loc=0;
        Node *T=NULL;
        for(int i=0; i<n; i++)
        {
            int x;
            scanf("%d",&x);
            T=Insert(T,x);
        }
        preOrder(T);//前序遍历
        printf("\n");
        inOrder(T);//中序遍历
        printf("\n");
        postOrder(T);//后序遍历
        printf("\n");
    }
    return 0;
}

 

posted @ 2016-07-26 08:34  多思考&&多动手  阅读(608)  评论(0编辑  收藏  举报