#include <stdio.h>#include <malloc.h>#include<stdlib.h>#include<string.h>#define MaxSize 50typedef int ElemType; typedef struct node{ElemType data;struct node *lchild;struct node *rchild;}BTNode,*Bitree;void InitBiTree(BTNode *&T);{T=NULL;}void CreateBiTree(BTNode *&T,char *str){ BTNode *St[MaxSize],*p=NULL; int top=-1,tag,j=0; char ch; T=NULL; ch=str[j]; while(ch!="\0") { switch(ch) { case'(':top++;St[top]=p;tag=1;break; case')':top--;break; case',':tag=2;break; default:p=(BTNode *)malloc(sizeof(BTNode)); p->data=ch;p->lchild=p->rchild=NULL; if(T==NULL) T=p; else { switch(tag) { case 1:St[top]->lchild=p;break; case 2:St[top]->rchild=p;break; } } } j++; ch=str[j]; }}BTNode *FindNode(BTNode *T,ElemType x){ BTNode *p; if(T==NULL)return NULL; else if(T->data==x)return T; else { p=FindNode(T->lchild,x); if(p!=NULL)return p; else return FindNode(T->rchild,x); }}BTNode *LchildNode(BTNode *p){ if(p->lchild) return p->lchild; else return NULL;}BTNode *RchildNode(BTNode *p){ if(p->rchild) return p->rchild; else return NULL;}int BiTreeDepth(BTNode *T){ int ldep,rdep; if(T==NULL) return 0; if(T->lchild)ldep=BiTreeDepth(T->lchild); else ldep=0; if(T->rchild)rdep=BiTreeDepth(T->rchild); else rdep=0; return(ldep>rdep)?(ldep+1):(rdep+1);}void visite(BTNode *T){ if(T==NULL) printf("The node does not exist"); else printf("%c",T->data);}void PrintBiTree1(BTNode *T){ if(T!=NULL) { visite(T); if(T->lchild!=NULL||T->rchild!=NULL) { printf("("); PrintBiTree1(T->lchild); if(T->rchild!=NULL)printf(","); PrintBiTree1(T->rchild); printf(")"); } }}void PrintBiTree2(BTNode *T,int level){ if(T!=NULL) { PrintBiTree2(T->rchild,level+1); if(level!=0) { for(int i=0;i<4*(level-1);i++) {printf("%s"," ");} printf("%s","---"); } visite(T); printf("\n"); PrintBiTree2(T->lchild,level+1); }}void DestoryBiTree(BTNode *&T){ if(T!=NULL) { DestoryBiTree(T->lchild); DestoryBiTree(T->rchild); free(T); }}void main(){int a[]={1,2,3,4,5,6};Bitree T;InitBiTree(T);}
添加回答
举报
0/150
提交
取消