二叉树 非递归 层序、前序、中序、后序遍历。

本文介绍了一种使用C#实现二叉树及其遍历的方法,包括层序、前序、中序和后序遍历,并提供了完整的代码示例及运行结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

简单二叉树

    public  class Node<T>
    {
        private T _data;
        private Node<T> _leftChild;
        private Node<T> _rightChild;
        private Node<T> _Parent;
        private bool flag;
        public  Node(){}

        public Node(T data) { this._data = data; flag = false; }

        public override string ToString()
        {
            return _data.ToString();
        }
        public T Data { get { return _data; } set { _data = value; } }
        public  Node<T> LeftChild { get { return _leftChild; } set { _leftChild = value; } }
        public Node<T> RightChild { get { return _rightChild; } set { _rightChild = value; } }
        public Node<T> Parent { get { return _Parent; } set { _Parent = value; } }
        public bool Flag { get { return flag; } set { flag = value; } }
    }
    
    class BinaryTree<T>
    {
        private Node<T> _root;
        public BinaryTree() { }
        public BinaryTree(Node<T> node) { this._root = node; }
        //四种序列
        public Node<T> Root { get { return _root; } set { this._root = value; } }
    }

层序遍历

        public void ByLayerPrint()
        {
            Node<T> temp = new Node<T>();
            Queue<Node<T>> queue = new Queue<Node<T>>();
            queue.Enqueue(_root);
            while (queue.Count > 0)
            {
                temp = queue.Dequeue();
                Console.WriteLine(temp);
                if (temp.LeftChild != null) { queue.Enqueue(temp.LeftChild); }
                if (temp.RightChild != null) { queue.Enqueue(temp.RightChild); }
            }
        }

前序遍历

  public void PreOrderPrint()
        {
            Node<T> temp = new Node<T>();
            Stack<Node<T>> stack = new Stack<Node<T>>();
            temp = _root;
            while (temp != null || stack.Count > 0)
            {
                while (temp != null)
                {
                    Console.WriteLine(temp);
                    stack.Push(temp);
                    temp = temp.LeftChild;
                }

                if (stack.Count > 0)
                {
                    temp = stack.Pop();
                    temp = temp.RightChild;
                }
            }
        }

中序遍历

 public void InOrderPrint()
        {
            Node<T> temp = new Node<T>();
            Stack<Node<T>> stack = new Stack<Node<T>>();
            temp = _root;
            while (temp != null)
            {
                stack.Push(temp);
                temp = temp.LeftChild;
            }
            while (stack.Count > 0)
            {
                temp = stack.Pop();
                Console.WriteLine(temp);
                if (temp.RightChild != null) { stack.Push(temp.RightChild); }
            }
        }

后序遍历

  public void PostOrderPrint()
        {
            Node<T> temp = new Node<T>();
            Stack<Node<T>> stack = new Stack<Node<T>>();
            temp = _root;
            while (temp != null)
            {
                stack.Push(temp);
                temp = temp.LeftChild;
            }

            while (stack.Count > 0)
            {
                temp = stack.Peek();
                if (temp.RightChild == null || temp.RightChild.Flag)
                {
                    stack.Pop();
                    Console.WriteLine(temp);
                    temp.Flag = true;
                }
                else
                {
                    temp = temp.RightChild;
                    while (temp != null)
                    {
                        stack.Push(temp);
                        temp = temp.LeftChild;
                    }
                }
            }
        }

测试代码

        static void Main(string[] args)
        {
            Node<string> node = new Node<string>("aaa");
            Node<string> node2 = new Node<string>("bbb");
            Node<string> node3 = new Node<string>("ccc");
            Node<string> node4 = new Node<string>("ddd");
            Node<string> node5 = new Node<string>("eee");
            Node<string> node6 = new Node<string>("fff");
            Node<string> node7 = new Node<string>("ggg");
            Node<string> node8 = new Node<string>("hhh");

            BinaryTree<string> tree = new BinaryTree<string>(node);
            node.LeftChild = node5;
            node.RightChild = node4;
            node4.RightChild = node3;
            node5.RightChild = node6;
            node5.LeftChild = node7;
            Console.WriteLine("******************************");
            Console.WriteLine("******* 层序遍历  1 **********");
            Console.WriteLine("******* 前序遍历  2 **********");
            Console.WriteLine("******* 中序遍历  3 **********");
            Console.WriteLine("******* 后序遍历  4 **********");
            Console.WriteLine("******* 退出      0 **********");
            Console.WriteLine("******************************");
            while (true)
            {
                ConsoleKeyInfo key = Console.ReadKey();
                switch (key.Key)
                {
                    case ConsoleKey.D0:
                        Environment.Exit(0);
                        break;
                    case ConsoleKey.D1:
                        Console.WriteLine("---------层序遍历----------");
                        tree.ByLayerPrint();
                        break;
                    case ConsoleKey.D2:
                        Console.WriteLine("---------前序遍历----------");
                        tree.PreOrderPrint();
                        break;
                    case ConsoleKey.D3:
                        Console.WriteLine("---------中序遍历----------");
                        tree.InOrderPrint();
                        break;
                    case ConsoleKey.D4:
                        Console.WriteLine("---------后序遍历----------");
                        tree.PostOrderPrint();
                        break;
                    default:
                        Console.WriteLine("输入有误,重新输入");
                        break;
                }

            }
        }

测试结果

172700_VM8g_2403989.jpg

转载于:https://my.oschina.net/hunjixin/blog/511498

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值