C#實現泛型二叉樹

構造如下圖所示的二叉樹:
在這裏插入圖片描述

前序遍歷

前序遍歷遵循的是中——>左——>右的遍歷順序。以上圖爲例,其過程如下:
(1)輸出A,獲取其左子節點B;
(2)輸出B,獲取其左子節點D;
(3)輸出D,D的左右子節點均爲空,此時B的左子樹全部遍歷完成,獲取其右子節點E;
(4)輸出E,E的左右子節點均爲空,此時B的左右子樹全部遍歷完成,A的左子樹全部遍歷完成,獲取其右子節點C;
(5)輸出C,C的左子節點爲空,獲取其右子節點F;
(6)輸出F,此時遍歷全部完成。

中序遍歷

中序遍歷遵循的是左——>中——>右的遍歷順序。以上圖爲例,其過程如下:
(1)A->B->D,D的左右子節點均爲空,輸出D,獲取其根節點B;
(2)輸出B,獲取其右子節點E;
(3)E的左右子節點均爲空,輸出E,此時A的左子樹全部遍歷完成,獲取根節點A;
(4)輸出A,獲取其右子節點C;
(5)C的左子節點爲空,輸出C,獲取其右子節點F;
(6)輸出F,此時遍歷全部完成。

後序遍歷

後序遍歷遵循的是左——>右——>中的遍歷順序。以上圖爲例,其過程如下:
(1)A->B->D,D的左右子節點均爲空,輸出D,獲取其兄弟節點E;
(2)輸出E,E的左右子節點均爲空,獲取其根節點B;
(3)輸出B,此時A的左子樹全部遍歷完成,獲取其右子節點C,C的左子節點爲空,獲取其右子節點F;
(4)輸出F,此時C的左右子樹全部遍歷完成,獲取C;
(5)輸出C,此時A的左右子樹全部遍歷完成,獲取A;
(6)輸出A,此時遍歷全部完成。

程序代碼

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // 第三層節點
            BinaryTree<string> d = new BinaryTree<string>() { Value = "D", Left = null, Right = null };
            BinaryTree<string> e = new BinaryTree<string>() { Value = "E", Left = null, Right = null };
            BinaryTree<string> f = new BinaryTree<string>() { Value = "F", Left = null, Right = null };

            // 第二層節點
            BinaryTree<string> b = new BinaryTree<string>() { Value = "B", Left = d, Right = e };
            BinaryTree<string> c = new BinaryTree<string>() { Value = "C", Left = null, Right = f };

            // 第一層節點
            BinaryTree<string> a = new BinaryTree<string>() { Value = "A", Left = b, Right = c };

            // 前序遍歷
            Console.WriteLine("—————————————前序遍歷———————————————");
            PreorderTraversal(a);

            // 中序遍歷
            Console.WriteLine("—————————————中序遍歷———————————————");
            SequentialTraversal(a);

            // 後序遍歷
            Console.WriteLine("—————————————後序遍歷———————————————");
            PostOrderTraversal(a);

            Console.ReadKey();
        }

        /// <summary>
        /// 前序遍歷
        /// </summary>
        /// <param name="tree"></param>
        static void PreorderTraversal(BinaryTree<string> tree)
        {
            if (tree == null)
            {
                return;
            }
            Console.WriteLine(tree.Value);
            PreorderTraversal(tree.Left);
            PreorderTraversal(tree.Right);
        }

        /// <summary>
        /// 中序遍歷
        /// </summary>
        /// <param name="tree"></param>
        static void SequentialTraversal(BinaryTree<string> tree)
        {
            if (tree == null)
            {
                return;
            }
            SequentialTraversal(tree.Left);
            Console.WriteLine(tree.Value);
            SequentialTraversal(tree.Right);
        }

        /// <summary>
        /// 後序遍歷
        /// </summary>
        /// <param name="tree"></param>
        static void PostOrderTraversal(BinaryTree<string> tree)
        {
            if (tree == null)
            {
                return;
            }
            PostOrderTraversal(tree.Left);
            PostOrderTraversal(tree.Right);
            Console.WriteLine(tree.Value);
        }
    }

    public class BinaryTree<T>
    {
        /// <summary>
        /// 節點值
        /// </summary>
        public T Value { get; set; }

        /// <summary>
        /// 左子節點
        /// </summary>
        public BinaryTree<T> Left { get; set; }

        /// <summary>
        /// 右子節點
        /// </summary>
        public BinaryTree<T> Right { get; set; }
    }
}

程序運行結果如下:

—————————————前序遍歷———————————————
A
B
D
E
C
F
—————————————中序遍歷———————————————
D
B
E
A
C
F
—————————————後序遍歷———————————————
D
E
B
F
C
A

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章