## 二叉树的非递归遍历

2010-12-23 21:59  MichaelYin  阅读(5259)  评论(1编辑  收藏  举报

        /// <summary>
/// 非递归中序遍历二叉树
/// </summary>
/// <param name="root"></param>
static void InOrderTraverse(BinaryTreeNode root)
{
BinaryTreeNode temp = root;
Stack<BinaryTreeNode> stack = new Stack<BinaryTreeNode>();
stack.Push(root);
while (stack.Count > 0)
{
while (temp != null)
{
temp = temp.left;
stack.Push(temp);
}
stack.Pop();
//如果为0证明这时右边节点为null
if (stack.Count > 0)
{
temp = stack.Pop();
Console.WriteLine(temp.data);
temp = temp.right;
stack.Push(temp);
}
}
}

        /// <summary>
/// 非递归中序遍历二叉树
/// </summary>
/// <param name="root"></param>
static void InOrderTraverse2(BinaryTreeNode root)
{
BinaryTreeNode temp = root.left;
Stack<BinaryTreeNode> stack = new Stack<BinaryTreeNode>();
stack.Push(root);
while (stack.Count > 0 || temp != null)
{
while (temp != null)
{
stack.Push(temp);
temp = temp.left;
}
temp = stack.Pop();
Console.WriteLine(temp.data);
temp = temp.right;
}
}

while循环中的while循环的条件是temp是否为null,所以，我可以用一个if/else来换一下

        static void InOrderTraverse3(BinaryTreeNode root)
{
BinaryTreeNode temp = root.left;
Stack<BinaryTreeNode> stack = new Stack<BinaryTreeNode>();
stack.Push(root);
while (stack.Count > 0 || temp != null)
{
if (temp != null)
{
stack.Push(temp);
temp = temp.left;
}
else
{
temp = stack.Pop();
Console.WriteLine(temp.data);
temp = temp.right;
}
}
}

        /// <summary>
/// 非递归先序遍历二叉树
/// </summary>
/// <param name="root"></param>
static void PreOrderTraverse(BinaryTreeNode root)
{
BinaryTreeNode temp = root.left;
Stack<BinaryTreeNode> stack = new Stack<BinaryTreeNode>();
Console.WriteLine(root.data);
stack.Push(root);
while (stack.Count > 0 || temp != null)
{
while (temp != null)
{
Console.WriteLine(temp.data);
stack.Push(temp);
temp = temp.left;
}
temp = stack.Pop();
temp = temp.right;
}
}

        static void PostOrderTraversa1(BinaryTreeNode root)
{
Stack<BinaryTreeNode> stack = new Stack<BinaryTreeNode>();
stack.Push(root);
BinaryTreeNode prev = null;
BinaryTreeNode curr = null;
while (stack.Count > 0)
{
curr = stack.Peek();
if (prev == null || prev.left == curr || prev.right == curr)
{
if (curr.left != null)
{
stack.Push(curr.left);
}
else if (curr.right != null)
{
stack.Push(curr.right);
}
else
{
Console.WriteLine(curr.data);
stack.Pop();
}
}
else if (curr.left == prev)
{
if (curr.right != null)
{
stack.Push(curr.right);
}
else
{
Console.WriteLine(curr.data);
stack.Pop();
}
}
else if (curr.right == prev)
{
Console.WriteLine(curr.data);
stack.Pop();
}
prev = curr;
}
}
这个方法我继续折腾，可以简化成这样
        static void PostOrderTraversa2(BinaryTreeNode root)
{
Stack<BinaryTreeNode> stack = new Stack<BinaryTreeNode>();
stack.Push(root);
BinaryTreeNode prev = null;
BinaryTreeNode curr = null;
while (stack.Count > 0)
{
curr = stack.Peek();
if (prev == null || prev.left == curr || prev.right == curr)
{
if (curr.left != null)
stack.Push(curr.left);
else if (curr.right != null)
stack.Push(curr.right);
}
else if (curr.left == prev)
{
if (curr.right != null)
stack.Push(curr.right);
}
else
{
Console.WriteLine(curr.data);
stack.Pop();
}
prev = curr;
}
}
恩恩，有意思~
好了，最后来一个压轴的吧。老实说我开始想过这么搞，但是没有想清楚就否定了，后来在网上看到别人这么写才看懂。
使用双栈来完成后序遍历，看好了，当当当当~
        /// <summary>
/// 使用双栈
/// </summary>
/// <param name="root"></param>
static void PostOrderTraversa3(BinaryTreeNode root)
{
Stack<BinaryTreeNode> stack = new Stack<BinaryTreeNode>();
Stack<BinaryTreeNode> output = new Stack<BinaryTreeNode>();
stack.Push(root);
BinaryTreeNode curr = null;
while (stack.Count > 0)
{
curr = stack.Pop();
output.Push(curr);
if (curr.left != null)
stack.Push(curr.left);
if (curr.right != null)
stack.Push(curr.right);
}
while (output.Count > 0)
{
Console.WriteLine(output.Peek().data);
output.Pop();
}
}