/*
面試(三)
開發環境: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);
}