#include<stdio.h>
#include<stack.c>
#include<stdbool.h>
#include<SquenceNode.c>
#define MaxSize 20
typedef struct TreeNode
*BinTree
;
typedef BinTree Position
;
typedef int ElementType
;
struct TreeNode
{
ElementType Data
;
BinTree Left
;
BinTree Right
;
BinTree IsFirst
;
};
void InorderTraversal( BinTree BT
)
{
BinTree T
= BT
;
Stack S
= CreateStack(MaxSize
);
while( T
|| !isEmpty( S
) )
{
while( T
)
{
T
->IsFirst
= true
;
Push(S
,T
);
T
= T
->Left
;
}
if(!isEmpty(S
))
{
T
= Pop(S
);
printf("%d", T
->Data
);
T
= T
->Right
;
}
}
}
void PreOrderTraversal( BinTree BT
)
{
BinTree T
= BT
;
Stack S
= CreateStack(MaxSize
);
while( T
|| !isEmpty( S
) )
{
while( T
)
{
Push(S
,T
);
printf("%d", T
->Data
);
T
= T
->Left
;
}
if(!isEmpty(S
))
{
T
= Pop(S
);
T
= T
->Right
;
}
}
}
void PostOrderTraversal( BinTree BT
)
{
BinTree T
= BT
;
Stack S
= CreateStack(MaxSize
);
while( T
|| !isEmpty( S
) )
{
while( T
)
{
T
->IsFirst
= true
;
Push(S
,T
);
T
= T
->Left
;
}
if(!isEmpty(S
))
{
T
= Pop(S
);
if(T
->IsFirst
== true
)
{
T
->IsFirst
= false
;
Push(S
,T
);
T
= T
->Right
;
}
else
{
printf("%d", T
->Data
);
T
= NULL;
}
}
}
}
void LevelOrderTraversal( BinTree BT
)
{
Queue Q
;
BinTree T
;
if( !BT
) return;
Q
= CreateQueue( MaxSize
);
Add(Q
, BT
);
while( ! isEmpty(Q
) )
{
T
= Delete(Q
);
printf("%d\n", T
->Data
);
if(T
->Left
)
Add(Q
, T
->Left
);
if(T
->Right
)
Add(Q
, T
->Right
);
}
}
转载请注明原文地址: https://lol.8miu.com/read-7439.html