求数据二叉树非递归遍历的算法(C++)

来源:百度知道 编辑:UC知道 时间:2024/05/24 05:14:26
如题,不要有模板的,最好能看懂的,谢谢···

#include<stdio.h>
#include<stdlib.h>
#define MAX_STACK_SIZE 100
typedef struct node *tree_pointer;
struct node{
char ch;
tree_pointer left_child,right_child;
};
tree_pointer root=NULL;
tree_pointer stack[MAX_STACK_SIZE];
int top=-1;
void push(int *top,tree_pointer item)
{
if(*top>=MAX_STACK_SIZE-1)
printf("The stack is full\n");
else
stack[++*top]=item;

}
tree_pointer pop(int *top)
{
if(*top==-1)
return NULL;
return stack[(*top)--];
}
tree_pointer create(tree_pointer ptr)
{
char ch;
scanf("%c",&ch);
if(ch==' ')
ptr=NULL;
else{
ptr=(tree_pointer)malloc(sizeof(node));
ptr->ch=ch;
ptr->left_child=create(ptr->left_child);
ptr->right_child=create(ptr->right_child);
}
return ptr;
}
void iter_preor