parent
e495793712
commit
86be2a8da1
@ -0,0 +1,151 @@
|
||||
#include<stdio.h>
|
||||
#include<malloc.h>
|
||||
#define MaxSize 30
|
||||
typedef char ElemType;
|
||||
typedef struct node
|
||||
{
|
||||
ElemType data;
|
||||
struct node *lchild;
|
||||
struct node *rchild;
|
||||
}BTNode;
|
||||
typedef struct{
|
||||
BTNode* data[MaxSize];
|
||||
int top;
|
||||
}SqStack;
|
||||
|
||||
void InitStack(SqStack *&s){
|
||||
s = (SqStack*)malloc(sizeof(SqStack));
|
||||
s->top = -1;
|
||||
}
|
||||
bool StackEmpty(SqStack *s){
|
||||
return(s->top == -1);
|
||||
}
|
||||
bool Push(SqStack *&s,BTNode *e){
|
||||
if(s->top == MaxSize-1)
|
||||
return false;
|
||||
s->top++;
|
||||
s->data[s->top] = e;
|
||||
return true;
|
||||
}
|
||||
bool Pop(SqStack *&s,BTNode *&e){
|
||||
if(s->top == -1)
|
||||
return false;
|
||||
e = s->data[s->top];
|
||||
s->top--;
|
||||
return true;
|
||||
}
|
||||
int CreateBiTree(BTNode *&T,char *str){
|
||||
BTNode *St[MaxSize],*p;
|
||||
int top = -1,k,j = 0;
|
||||
char ch;
|
||||
T = NULL;
|
||||
ch = str[j];
|
||||
while(ch!='\0')
|
||||
{
|
||||
switch(ch)
|
||||
{
|
||||
case '(':top++;St[top]=p;k=1;break;
|
||||
case ')':top--;break;
|
||||
case ',':k=2;break;
|
||||
default:p=(BTNode *)malloc(sizeof(BTNode));
|
||||
p->data=ch;p->lchild=p->rchild=NULL;
|
||||
if(T==NULL)
|
||||
T=p;
|
||||
else
|
||||
{
|
||||
switch(k)
|
||||
{
|
||||
case 1:St[top]->lchild=p;break;
|
||||
case 2:St[top]->rchild=p;break;
|
||||
}
|
||||
}
|
||||
}
|
||||
j++;
|
||||
ch=str[j];
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
int preOrder(BTNode *T){
|
||||
if(T != NULL){
|
||||
printf("%c",T->data);
|
||||
preOrder(T->lchild);
|
||||
preOrder(T->rchild);
|
||||
}
|
||||
}
|
||||
int inOrder(BTNode *T){
|
||||
if(T != NULL){
|
||||
inOrder(T->lchild);
|
||||
printf("%c",T->data);
|
||||
inOrder(T->rchild);
|
||||
}
|
||||
}
|
||||
int postOrder(BTNode *T){
|
||||
if(T != NULL){
|
||||
postOrder(T->lchild);
|
||||
postOrder(T->rchild);
|
||||
printf("%c",T->data);
|
||||
}
|
||||
}
|
||||
|
||||
void ldr(BTNode *T){
|
||||
BTNode *p;
|
||||
SqStack *st;
|
||||
InitStack(st);
|
||||
p = T;
|
||||
while(!StackEmpty(st) || p != NULL){
|
||||
while(p != NULL){
|
||||
Push(st,p);
|
||||
p = p->lchild;
|
||||
}
|
||||
if(!StackEmpty(st)){
|
||||
Pop(st,p);
|
||||
printf("%c",p->data);
|
||||
p = p->rchild;
|
||||
}
|
||||
}
|
||||
}
|
||||
int main(){
|
||||
BTNode *T = NULL;
|
||||
char str[30];
|
||||
int option;
|
||||
|
||||
do{
|
||||
printf("Options:\n");
|
||||
printf("%-25s%-25s%-25s\n%-25s%-25s%-25s\n","1.CreateBTree","2.PreOrder Result","3.InOrder Result","4.PostOrder Result","5.ldr Result","6.Quit");
|
||||
scanf("%d",&option);
|
||||
switch(option){
|
||||
case 1:
|
||||
printf("Input string:\n");
|
||||
scanf("%s",str);
|
||||
CreateBiTree(T,str);
|
||||
break;
|
||||
case 2:
|
||||
if(T == NULL) break;
|
||||
printf("PreOrder:\n");
|
||||
preOrder(T);
|
||||
printf("\n");
|
||||
break;
|
||||
case 3:
|
||||
if(T == NULL) break;
|
||||
printf("InOrder:\n");
|
||||
inOrder(T);
|
||||
printf("\n");
|
||||
break;
|
||||
case 4:
|
||||
if(T == NULL) break;
|
||||
printf("PostOrder\n");
|
||||
postOrder(T);
|
||||
printf("\n");
|
||||
break;
|
||||
case 5:
|
||||
if(T == NULL) break;
|
||||
printf("INORDER:\n");
|
||||
ldr(T);
|
||||
printf("\n");
|
||||
break;
|
||||
case 6:
|
||||
return 0;
|
||||
}
|
||||
printf("\n");
|
||||
}while(true);
|
||||
}
|
||||
Loading…
Reference in new issue