/*sy
#include
#include
typedef char DataType;
typedef struct node{
DataType data;
struct node *lchild
}BinTNode;
typedef BinTNode *BinTree;
int count;
void CreateBinTree(BinTree *T);
void PreorderN(BinTree T);
#define StackSize
typedef BinTree SDataType; /*棧的元素類型設為整型*/
#define Error printf
typedef struct{
SDataType data[StackSize];
int top;
}SeqStack;
void InitStack(SeqStack *S) /*初始棧*/
{ S
}
int StackEmpty(SeqStack *S) /*判棧空*/
{return S
}
int StackFull(SeqStack *S) /*判棧滿*/
{return S
}
void Push(SeqStack *S
{if(StackFull(S))
Error(
else S
}
SDataType Pop(SeqStack *S) /*出棧*/
{if (StackEmpty(S))
Error(
else return S
}
SDataType StackTop(SeqStack *S) /*取棧頂元素*/
{if (StackEmpty(S))
Error(
return S
}
main()
{BinTree T;
char ch
printf(
ch
while(ch
{printf(
printf(
printf(
scanf(
switch(ch
{case
case
CreateBinTree(&T);
printf(
case
case
PreorderN(T);break;
case
case
default:ch
}
}
}
void CreateBinTree(BinTree *T)
{char ch;
scanf(
if (ch==
else {*T=(BinTNode*)malloc(sizeof(BinTNode));
(*T)
CreateBinTree(&(*T)
CreateBinTree(&(*T)
}
}
void PreorderN(BinTree T)
{/*先序遍歷二叉樹T的非遞歸算法*/
SeqStack *S;
BinTree p;
InitStack(S);Push(S
while(!StackEmpty(S))
{while(p=StackTop(S))
{ printf(
Push(S
}
p=Pop(S); /*空指針退棧*/
if (!StackEmpty(S)) /*輸出結點
{p=Pop(S);
/* printf(
Push(S
}
}
}/*PreorderN */
From:http://tw.wingwit.com/Article/program/sjjg/201311/23547.html