#include <iostream>
using namespace std;
#define STACK_MAX_SIZE 50
typedef struct node
{
char data;
node * lchild;
node * rchild;
}* BTreeNode;
void CreateBTree(BTreeNode &root,char * a)
{
BTreeNode temp;
BTreeNode Stack[STACK_MAX_SIZE];
int top =-1;
int k;
int i=0;
root = NULL;
while(a[i]!='\0')
{
switch(a[i])
{
case '#':
break;
case '(':
if(top == STACK_MAX_SIZE-1)
{
cout<<"栈溢出!程序自动退出!!"<<endl;
exit(1);
}
else
{
top ++;
Stack[top] = temp;
k=1;
break;
}
case ')':
if(top ==-1)
{
exit(1);
} else
{
top--;
break;
}
case ',':
k = 2;
break;
default:
temp = new node();
temp->data = a[i];
temp->lchild = NULL;
temp->rchild = NULL;
if(root == NULL)
{
root = temp;
}
else
{
if(k==1)
Stack[top]->lchild = temp;
else
{
Stack[top]->rchild = temp;
}
}
}
i++;
}
}
void PreSort(BTreeNode root)
{
if(root == NULL)
{
return ;
}
else
{
cout<<root->data<<" ";
PreSort(root->lchild);
PreSort(root->rchild);
}
}
int main()
{
char * a = "E(B(A,D(C,#)),I(G(F,H),J))";
BTreeNode root;
CreateBTree(root,a);
cout<<"先序序列: ";
PreSort(root);
getchar();
return 0;
}