当前位置: 代码迷 >> C语言 >> 先序中序返回二叉树 什么错误啊?
  详细解决方案

先序中序返回二叉树 什么错误啊?

热度:342   发布时间:2007-06-10 10:50:12.0
先序中序返回二叉树 什么错误啊?


程序是TC 3.0 写的~
搜索更多相关的解决方案: 二叉树  

----------------解决方案--------------------------------------------------------
干嘛不写下来呢,还要下,
----------------解决方案--------------------------------------------------------
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include<math.h>
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<inpos+n;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();
}
}
//
----------------解决方案--------------------------------------------------------
回复:(se7en_enter)#include#inclu...

厉害~!!!!!!
谢谢~


----------------解决方案--------------------------------------------------------
  相关解决方案