二叉树面试(三)

/*
面试(三)
开发环境:Visual Studio 2008
开发语言:C语言
要 求:
请根据下列程序中的CreateTree函数绘制程序流程图(或N-S图)。
请根据下列程序中的OutputByLayer函数绘制程序流程图(或N-S图)。
请根据下列程序中的PreOrder函数绘制程序流程图(或N-S图)。
请根据下列程序中的InOrder函数绘制程序流程图(或N-S图)。
请根据下列程序中的LastOrder函数绘制程序流程图(或N-S图)。
时 间:20-30分钟

得分标准:
1.使用括弧表示法非递推建立二叉树(10分)
2.按层输出二叉树,并统计二叉树的结点个数,及该二叉树的深度(高度)(20分)
3.非递归先序遍历二叉树(10分)
4.非递归中序遍历二叉树(10分)
5.非递归后序遍历二叉树(20分)
6.口述算法(30分)
*7.请指出该程序的算法缺陷,并给出解决办法。

考核标准:
1.程序流程图(或N-S图)结构不清楚者,此题直接(0分)
2.口述算法思路不清晰者(0分)

考核小组:张一涛

批注评语:CreateTree函数算法缺陷在于,当使用括弧表示法非递归建立二叉树时,所输出的字符串长度超过100,无法建立二叉树。当时二叉树的结点超过100时,OutputByLayer函数、PreOrder函数、InOrder函数和LastOrder函数将出现错误。解决办法:使用链栈、链队、循环队列。

*/

#include <stdio.h>
#include <stdlib.h>

typedef struct BTree
{
    char data;
    struct BTree *Lchild,*Rchild;
}Tree;

Tree *CreateTree(Tree *Root,char *str)
{
    Tree *s[100],*p;
    int i=0,k=0,top=0;
    Root=NULL;
    p=NULL;
    for(i=0;str[i]!='\0';i++)
    {
        if(str[i]=='(')
        {   s[top++]=p;k=1;}
        else
        if(str[i]==')')
            top--;
        else
        if(str[i]==',')
            k=2;
        else
        {
            p=(Tree *)malloc(sizeof(Tree));
            p->data=str[i];
            p->Lchild=NULL;
            p->Rchild=NULL;
            if(Root==NULL)
                Root=p;
            else
            {
                if(k==1)
                    s[top-1]->Lchild=p;
                else
                    s[top-1]->Rchild=p;
            }
        }
    }
    return Root;
}
void PreRecursion(Tree *r){  
    if(r){  
        printf("%c ",r->data);  
        PreRecursion(r->Lchild); 
        PreRecursion(r->Rchild);  
    }  
}
void InRecursion(Tree *r){  
    if(r){  
        InRecursion(r->Lchild); 
        printf("%c ",r->data);
        InRecursion(r->Rchild);  
    }  
}
void LastRecursion(Tree *r){  
    if(r){  
        LastRecursion(r->Lchild); 
        LastRecursion(r->Rchild);  
        printf("%c ",r->data);
    }  
}
void LastOrder(Tree *r)
{
    Tree *stack[100];
    Tree *cur;
    Tree *pre=NULL;
    int i=-1;
    stack[++i]=r;
    while(i!=-1)
    {
        cur=stack[i];
        if((cur->Lchild==NULL&&cur->Rchild==NULL)||(pre!=NULL&&(pre==cur->Lchild||pre==cur->Rchild)))
        {
            printf("%c ",cur->data);  
            i--;
            pre=cur; 
        }
        else
        {
            if(cur->Rchild!=NULL)
                stack[++i]=cur->Rchild;
            if(cur->Lchild!=NULL)
                stack[++i]=cur->Lchild;
        }
    }
}
void PreOrder(Tree *r)
{
    Tree *s[100],*p=r;
    int top=-1;
    while(p!=NULL||top!=-1)
    {
        while(p!=NULL)
        {
            printf("%c ",p->data);
            s[++top]=p;
            p=p->Lchild;
        }
        if(top!=-1)
        {
            p=s[top--];         
            p=p->Rchild;
        }
    }
}

void InOrder(Tree *r)
{
    Tree *s[100],*p=r;
    int top=-1;
    while(p!=NULL||top!=-1)
    {
        while(p!=NULL)
        {
            s[++top]=p;
            p=p->Lchild;
        }
        if(top!=-1)
        {
            p=s[top];
            printf("%c ",p->data);
            top--;
            p=p->Rchild;
        }
    }
}

void FreeTree(Tree *R)
{
    if(R!=NULL)
    {
        FreeTree(R->Lchild);
        FreeTree(R->Rchild);
        free(R);
    }
}

void OutputByLayer(Tree *R)
{
    Tree *Stack[100];
    int cur=0,last=0,tmp;
    int high=0;
    if(R==NULL)
        return ;
    Stack[last++]=R;
    while(cur<last)
    {
        tmp=last;
        while(cur < tmp)
        {
            printf("%c ",Stack[cur]->data);
            if(Stack[cur]->Lchild!=NULL)
                Stack[last++]=Stack[cur]->Lchild;
            if(Stack[cur]->Rchild!=NULL)
                Stack[last++]=Stack[cur]->Rchild;
            ++cur;
        }
        printf("\n");
        high++;
    }
    printf("The Tree is Node:%d\n",last);
    printf("The Tree is high:%d\n",high);
}
void main()
{
    Tree *Root=NULL;
    Root=CreateTree(Root,"a(b(c,d(1,2)),e(,f(3)))");

    printf("Output By Layer:\n");
    OutputByLayer(Root);
    printf("\n");

    printf("\n**********Non-Recursion**********\n");
    printf("PreOrder:\t");
    PreOrder(Root);
    printf("\n");

    printf("InOrder:\t");
    InOrder(Root);
    printf("\n");

    printf("LastOrder:\t");
    LastOrder(Root);
    printf("\n");

    printf("\n**********Recursion**********\n");
    printf("PreOrder:\t");
    PreRecursion(Root);
    printf("\n");

    printf("InOrder:\t");
    InRecursion(Root);
    printf("\n");

    printf("LastOrder:\t");
    LastRecursion(Root);
    printf("\n");

    printf("\nFreeTree!\n");
    FreeTree(Root);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值