主题:[原创]data structure
帮我看看哪里错了
#include "iostream"
#define MaxSize 100
typedef char ElemType;
typedef struct node
{
ElemType data;
struct node *left,*right;
}BTree;
void preorder(BTree *BT)
{
if(BT!=NULL)
{
printf("%s",BT->data);
preorder(BT->left);
preorder(BT->right);
}
}
void inorder(BTree *BT)
{
if(BT!=NULL)
{
inorder(BT->left);
printf("%s",BT->data);
inorder(BT->right);
}
}
void postorder(BTree *BT)
{
if(BT!=NULL)
{
postorder(BT->left);
postorder(BT->right);
printf("%s",BT->data);
}
}
void creatree(BTree **BT,char *str)
{
BTree *stack[MaxSize],*p;
int top=-1,k,j=0;
char ch;
ch=str[j];
while(ch!='\0')
{
switch(ch)
{
case '(':top++;stack[top]=p;k=1;
break;
case ')':top--;
break;
case ',':k=2;
break;
default:p=(BTree *) malloc(sizeof(BTree));
p->data=ch;
p->left=p->right=NULL;
if(*BT==NULL)
*BT=p;
else
{
switch(k)
{
case 1:stack[top]->left=p;
break;
case 2:stack[top]->right=p;
}
}
}
j++;
ch=str[j];
}
}
void main()
{
BTree *B;
char *s="A(B(D,E(H,I)),C(G))";
creatree(&B,s);
printf("/n preorder:");
preorder(B);
printf("/n inorder:");
inorder(B);
printf("/n postorder:");
postorder(B);
}
#include "iostream"
#define MaxSize 100
typedef char ElemType;
typedef struct node
{
ElemType data;
struct node *left,*right;
}BTree;
void preorder(BTree *BT)
{
if(BT!=NULL)
{
printf("%s",BT->data);
preorder(BT->left);
preorder(BT->right);
}
}
void inorder(BTree *BT)
{
if(BT!=NULL)
{
inorder(BT->left);
printf("%s",BT->data);
inorder(BT->right);
}
}
void postorder(BTree *BT)
{
if(BT!=NULL)
{
postorder(BT->left);
postorder(BT->right);
printf("%s",BT->data);
}
}
void creatree(BTree **BT,char *str)
{
BTree *stack[MaxSize],*p;
int top=-1,k,j=0;
char ch;
ch=str[j];
while(ch!='\0')
{
switch(ch)
{
case '(':top++;stack[top]=p;k=1;
break;
case ')':top--;
break;
case ',':k=2;
break;
default:p=(BTree *) malloc(sizeof(BTree));
p->data=ch;
p->left=p->right=NULL;
if(*BT==NULL)
*BT=p;
else
{
switch(k)
{
case 1:stack[top]->left=p;
break;
case 2:stack[top]->right=p;
}
}
}
j++;
ch=str[j];
}
}
void main()
{
BTree *B;
char *s="A(B(D,E(H,I)),C(G))";
creatree(&B,s);
printf("/n preorder:");
preorder(B);
printf("/n inorder:");
inorder(B);
printf("/n postorder:");
postorder(B);
}