/// <summary>
/// 作者:GENGWEI
/// </summary>
/// <summary>
/// 非递归实现后序遍历
/// </summary>
/// <param name="t"></param>
static void NonRecurPostOrder(TNode t)
{
SeqStack st = new SeqStack();
st.top = -1;
TNode p = t;
while (p != null || st.top != -1)
{
while (p != null)
{
st.top++;
st.data[st.top] = p;
st.tag[st.top] = 0;
p = p.left;
}
while ((st.top) > -1 && (st.tag[st.top] == 1))
{
p = st.data[st.top];
Console.Write(p.Data + ",");
st.top--;
}
if (st.top > -1)
{
p = st.data[st.top];
st.tag[st.top] = 1;
p = p.right;
}
else
{
p = null;
}
}
}
/// <summary>
/// 作者:GENGWEI
/// </summary>
/// <summary>
/// 自定义的顺序栈
/// </summary>
class SeqStack
{
public TNode[] data;
//为栈中元素作的标记,用于后序遍历
public int[] tag;
//栈顶
public int top;
public SeqStack()
{
data = new TNode[8];
tag=new int[8];
}
}