TQC+ C# 認證教學手冊

404、二元樹

☐易 ☑中 ☐難

DEMO

http://nova.umuc.edu/~jarc/idsv/lesson1.html

https://www.cs.usfca.edu/~galles/visualization/BST.html

參考程式碼

檔案:Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CSD04
{
    class Program
    {
        static void Main(string[] args)
        {
            TreeNode nodeRoot = new TreeNode()
            {
                Value = 2,
                NodeLeft = new TreeNode()
                {
                    Value = 7,
                    NodeLeft = new TreeNode() { Value = 2, NodeLeft = null, NodeRight = null },
                    NodeRight = new TreeNode()
                    {
                        Value = 6,
                        NodeLeft = new TreeNode() { Value = 5, NodeLeft = null, NodeRight = null },
                        NodeRight = new TreeNode() { Value = 11, NodeLeft = null, NodeRight = null }
                    }
                },
                NodeRight = new TreeNode()
                {
                    Value = 5,
                    NodeLeft = null,
                    NodeRight = new TreeNode()
                    {
                        Value = 9,
                        NodeLeft = new TreeNode() { Value = 4, NodeLeft = null, NodeRight = null },
                        NodeRight = null
                    }
                }
            };

            Tree tree = new Tree(nodeRoot);

            string preorder = tree.PreorderTraversal();
            string inorder = tree.InorderTraversal();
            string postorder = tree.PostorderTraversal();

            Console.WriteLine("前序: {0}", preorder);
            Console.WriteLine("中序: {0}", inorder);
            Console.WriteLine("後序: {0}", postorder);

            Console.Read();
        }
    }

    public class TreeNode
    {
        public int Value { get; set; }
        public TreeNode NodeLeft { get; set; }
        public TreeNode NodeRight { get; set; }
    }

    public class Tree
    {
        private TreeNode _root = null;

        public Tree(TreeNode NodeRoot)
        {
            this._root = NodeRoot;
        }

        public string PreorderTraversal()
        {
            return PreorderTraversal(_root);
        }

        private string PreorderTraversal(TreeNode node)
        {
            if (node == null)
            {
                return "";
            }
            return node.Value.ToString() + " " + PreorderTraversal(node.NodeLeft) + PreorderTraversal(node.NodeRight);
        }

        public string InorderTraversal()
        {
            return InorderTraversal(_root);
        }

        private string InorderTraversal(TreeNode node)
        {
            if (node == null)
            {
                return "";
            }
            return InorderTraversal(node.NodeLeft) + node.Value.ToString() + " " + InorderTraversal(node.NodeRight);
        }

        public string PostorderTraversal()
        {
            return PostorderTraversal(_root);
        }

        private string PostorderTraversal(TreeNode node)
        {
            if (node == null)
            {
                return "";
            }
            return PostorderTraversal(node.NodeLeft) + PostorderTraversal(node.NodeRight) + node.Value.ToString() + " ";
        }
    }
}