c语言返回二叉树的大小,先序中序返回二叉树 什么错误啊?

#include

#include

#include

#include

typedef struct BiTNode{

char data;

struct BiTNode *lchild,*rchild;

}BiTNode,*BiTree;

typedef struct QNode{

BiTree data;

struct QNode *next;

}QNode,*QueuePtr;

typedef struct{

QueuePtr front;

QueuePtr rear;

}LinkQueue;

BiNode * restore(char *prepos,char *inpos,int n){

char *rpos;

BiTree ptr;

int k;

if(n<=0) return NULL;

ptr->data=*prepos;

for(rpos=inpos;rpos

if(*rpos==*prepos) break;

k=rpos-inpos;

ptr->lchild=restore(prepos+1,inpos,k);

ptr->rchild=restore(prepos+1+k,rpos+1,n-1-k);

return ptr;

}

int hiberarchy(BiTree T)//按层输出二叉树

{

LinkQueue* Q;

if (!T) return 1;

InitQueue(Q);

visit(T->data);

EnQueue(Q,T); //结点入队

while (!QueueEmpty(Q)){ //队不空继续循环

DeQueue(Q,p); //出队

if (p->lchild){ //处理左孩子

visit(p->lchild->data);

EnQueue(Q,p->lchild);

}

if (p->rchild){ //处理右孩子

visit(p->rchild->data);

EnQueue(Q,p->rchild);

}

}

return 1;

}

void visit(char ch){

printf("%c\t",ch);

}

void Exit(){

char c;

printf("\nAre you sure to exit?(Y/N)\n");

c=getch();

if(c=='y'||c=='Y') exit(0);

else return;

}

char Menu(){

int i;

clrscr();

for(i=0;i<77;i++) printf("*");

printf("\n1.Create a Binary Tree according to your preorder and inorder traverse,then print by level:");

printf("\n2.Accordint to PreOrder and InOrder,InitBiTree,then print the BiTree Forest:");

printf("\n0.Exit.");

for(i=0;i<77;i++) printf("*");

printf("\nPlease enter the number(0 to 9) to select a function.\n");

return getch();

}

int CreateBiTree(BiTree *BT){

char e;

e=getchar();

if(e==' '){ *BT=NULL; return 1;}

else{

*BT=(BiTNode *)malloc(sizeof(BiTNode));

if(!(*T)) return 0;

(*BT)->data=e;

if(!CreateBiTree(&((*T)->lchild))) return 0;

if(!CreateBiTree(&((*T)->rchild))) return 0;

}

return 1;

}

int BiTreeDepth(BiTree T){

int depth,h;

BiTree p;

if(!T) return 0;

else{

for(depth=1,p=T->lchild;p;p=p->rchild){

h=BiTreeDepth(p);

if(h>depth) depth=h;

}

return depth+1;

}

}

int DestroyBiTree(BiTree *T){

if(*T){

if(DestroyBiTree(&((*T)->lchild))){

if(DestroyBiTree(&((*T)->rchild))){

free(*T);

if(!*T) return 1;

}

}

else return 0;

}

return 1;

}

int InitQueue(LinkQueue *Q){

Q->front=Q->rear=(QueuePtr)malloc(sizeof(QNode));

if(!Q->front) return 0;

Q->front->next=NULL;

return 1;

}

int EnQueue(LinkQueue *Q,BiTree e){

QueuePtr p;

p=(QNode *)malloc(sizeof(QNode));

if(!p) return 0;

p->data=e; p->next=NULL; Q->rear->next=p; Q->rear=p;

return 1;

}

BiTree DeQueue(LinkQueue *Q,BiTree e){

QueuePtr p;

if(Q->front==Q->rear) return NULL;

p=Q->front->next;

e=p->data;

Q->front->next=p->next;

if(Q->rear==p) Q->rear=Q->front;

free(p);

}

int QueueEmpty(LinkQueue Q){

if(Q.front==Q.rear) return 1;

else return 0;

}

int QueueLength(LinkQueue Q){

int i=0;

QueuePtr q;

for(q=Q.front;q!=Q.rear;q=q->next,i++);

return i;

}

int CreatePreInBiTree(BiTree *T,char *pre,char *in,int preLow,int preHigh,int inLow,int inHigh){

int pos;

if(preLow>preHigh){ *T=NULL; return 1;}

*T=(BiTNode *)malloc(sizeof(BiTNode));

(*T)->data=pre[preLow];

for(pos=inLow;pos<=inHigh;pos++)

if(pre[preLow]==in[pos]) break;

CreatePreInBiTree(&((*T)->lchild),pre,in,preLow+1,preLow+pos-inLow,inLow,pos-1);

CreatePreInBiTree(&((*T)->rchild),pre,in,preLow+pos-inLow+1,preHigh,pos+1,inHigh);

return 1;

}

int PrintBiTreeForest(BiTree T){

int i;

LinkQueue Q;

BiTree p;

p=T;

if (!T) return 1;

InitQueue(&Q);

while(p){ EnQueue(&Q,p); p=p->rchild;}

i=QueueLength(Q);

while(!QueueEmpty(Q)){

p=DeQueue(&Q);

printf("%c ",p->data);

if(p->lchild){

p=p->lchild;

while(p){ EnQueue(&Q,p); p=p->rchild;}

}

i--;

if(!i){ printf("\n"); i=QueueLength(Q);}

}

return 1;

}

void main(){

int depth,i;

char c;

char element,replace;

char pre[8]={'a','b','c','d','e','g','f','h'},in[8]={'c','b','e','g','d','f','a','h'};

BiTree T=NULL,TA=NULL,p=NULL;

while(1){

c=Menu();

switch(c){

case '1':printf("\nNow you will creat a bitree and print all the nodes by level.\nYour preorder is: ");

for(i=0;i<8;i++) printf("%c\t",pre[i]);

printf("\nYour inorder is: ");

for(i=0;i<8;i++) printf("%c\t",in[i]);

T=restore(pre,in,8);

printf("\n");

break;

case '2':

CreatePreInBiTree(&TA,pre,in,0,7,0,7);

LevelOrderTraverse(TA);

PrintBiTreeForest(TA);

printf("\n");

break;

case '0':printf("Press any key to exit...");

DestroyBiTree(&T);

DestroyBiTree(&TA);

getchar(); getchar();

Exit();

break;

default:break;

}

printf("\nPress anykey to main menu.\n");

getch();

}

}

//

6f83fddf9cb9ff6843fffd45e1807199.gif

年轻有年轻的冲动,成熟有成熟的魅力。莫让时间冲淡一切,要让一切充实时间

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值