代码改变世界

二叉树非递归遍历方法(C#)

2010-09-10 17:58  Kevin Pan  阅读(1529)  评论(3编辑  收藏  举报

前段时间写了二叉树的遍历算法,递归方法很简单几行代码就能搞定,但是非递归算法还是有点伤脑细胞。代码如下,可能有问题,希望博友指正,谢谢。

public class TreeNode

{

    public TreeNode LChild

    {

        get;

        set;

    }

 

    public TreeNode RChild

    {

        get;

        set;

    }

    public string Data

    {

        get;

        set;

    }

 

    //构造一颗简单的二叉树

    public static TreeNode CreateTree()

    {

        TreeNode root = new TreeNode() { Data = "-" };

        root.LChild = new TreeNode() { Data = "+" };

        root.RChild = new TreeNode() { Data = "/" };

        root.LChild.LChild = new TreeNode() { Data = "a" };

        root.LChild.RChild = new TreeNode() { Data = "*" };

        root.LChild.RChild.LChild = new TreeNode() { Data = "b" };

        root.LChild.RChild.RChild = new TreeNode() { Data = "-" };

        root.LChild.RChild.RChild.LChild = new TreeNode() { Data = "c" };

        root.LChild.RChild.RChild.RChild = new TreeNode() { Data = "d" };

        root.RChild.LChild = new TreeNode() { Data = "e" };

        root.RChild.RChild = new TreeNode() { Data = "f" };

 

        return root;

    }

 

    //递归先序遍历

    public static void PreOrderTranverse(TreeNode root)

    {

        if (root != null)

        {

            PrintNode(root);

            PreOrderTranverse(root.LChild);

            PreOrderTranverse(root.RChild);

        }

    }

 

    //递归中序遍历

    public static void InOrderTransverse(TreeNode root)

    {

        if (root != null)

        {

            InOrderTransverse(root.LChild);

            PrintNode(root);

            InOrderTransverse(root.RChild);

        }

    }

 

    //递归后续遍历

    public static void PostOrderTransverse(TreeNode root)

    {

        if (root != null)

        {

            PostOrderTransverse(root.LChild);

            PostOrderTransverse(root.RChild);

            PrintNode(root);

        }

    }

 

    //非递归中序遍历

    public static void NonrecursiveInOrderTraverse(TreeNode root)

    {

        Stack<TreeNode> nodeStack = new Stack<TreeNode>();

 

        if (root == null)

        {

            return;

        }

 

        var node = root;

        while (node != null || nodeStack.Count != 0)

        {

            while (node != null)

            {

                nodeStack.Push(node);

                node = node.LChild;

            }

            node = nodeStack.Pop();

 

            PrintNode(node);

            node = node.RChild;

        }

    }

 

    //非递归后续遍历

    public static void NonrecursivePostOrderTraverse(TreeNode root)

    {

        Stack<TreeNode> nodeStack = new Stack<TreeNode>();

 

        if (root == null)

        {

            return;

        }

 

        var node = root;

        while (node != null || nodeStack.Count != 0)

        {

            while (node != null)

            {

                nodeStack.Push(node);

                if (node.LChild == null)

                {

                    node = node.RChild;

                }

                else

                {

                    node = node.LChild;

                }

            }

 

            node = nodeStack.Pop();

 

            PrintNode(node);

            if (nodeStack.Count != 0 && node == nodeStack.Peek().LChild)

            {

                node = nodeStack.Peek().RChild;

            }

            else

            {

                node = null;

            }

        }

    }

 

    //打印树节点

    public static void PrintNode(TreeNode node)

    {

        System.Console.Write(node.Data);

    }

}