#include
#include
#include
typedef struct node *tree_pointer;
typedef struct node{
char data;
tree_pointer left_c;
tree_pointer right_c;
}node; tree_pointer crt_bt()
{
tree_pointer ptr;
char data;
printf("Input Node Data:=>");
data=getche();
if (data=='0')
return (NULL);
ptr=(tree_pointer)malloc(sizeof(node));
ptr->data=data;
printf("\nNode Data:%c=>Left\n",ptr->data);
ptr->left_c=crt_bt();
printf("\nNode Data:%c=>Right\n",ptr->data);
ptr->right_c=crt_bt();
return(ptr);
} #define MAX_ITEM 100
typedef struct {
int key;
}element;
tree_pointer stack[MAX_ITEM];
int top=-1; void push(int *top,tree_pointer item)
{
if(*top == MAX_ITEM-1)
{
printf("堆疊滿了\n");
exit(1);
}
stack[ *top]=item;
}
tree_pointer pop(int *top)
{
if(*top == -1)
{
printf("堆疊空了\n");
exit(1);
}
(stack[*top--]);
return;
} void iter_preorder(tree_pointer node)
{
int top=-1;
tree_pointer stack[MAX_ITEM];
push(&top,node);
for(node=pop(&top);node;node=pop(&top))
{ //node=pop(&top);
if(!node)break;
for(;node;)
{
printf("%c",node->data);
if(node->right_c)
push(&top,node->right_c);
node=node->left_c;
}
}
} void main()
{
tree_pointer tree;
tree=crt_bt();
printf("\n此樹的前序式排列為:");
iter_preorder(tree);
getch();
} 程式是可以執行了...可是印出來的排序會怪怪的..可以請問問題出在哪嗎??