二叉树先序遍历的非递归算法(用栈实现)

来源:百度知道 编辑:UC知道 时间:2024/06/17 03:56:10
RT

typedef char DataType;
typedef struct node{
DataType data;
struct node *lchild,*rchild;
}BinTNode;

typedef BinTNode *BinTree;

int count;
void CreateBinTree(BinTree *T);
void PreorderN(BinTree T);

#define StackSize 10 /*假定预分配的栈空间最多为10*/
typedef BinTree SDataType; /*栈的元素类型设为整型*/
#define Error printf
typedef struct{
SDataType data[StackSize];
int top;
}SeqStack;

void InitStack(SeqStack *S) /*初始栈*/
{
S->top=-1;
}

int StackEmpty(SeqStack *S) /*判栈空*/
{
return S->top==-1;
}

int StackFull(SeqStack *S) /*判栈满*/
{
return S->top==StackSize-1;
}

void Push(SeqStack *S, SDataType x) /*进栈*/
{
if(StackFull(S))
Error("栈已满\n"); /*上溢退出*/
else S->data[++S->top]=x; /*栈顶指针加1后将x进栈*/
}

SDataType Pop(SeqSta