//节点类
namespace 二叉树
{
class Node
{
private T date;
public T Date { get { return date; } set { date = value; } }
private Node leftNode;
public Node LeftNode
{
get { return leftNode; }
set { leftNode = value; }
}
private Node rightNode;
public Node RightNode
{
get { return rightNode; }
set { rightNode = value; }
}

    public Node()
    {
        leftNode = null;
        rightNode = null;
    }

    public Node (T date)
    {
        this.date = date;
        leftNode = null;
        rightNode = null;
    }

}

}
//二叉树
namespace 二叉树
{
class BinaryTree
{
private Node node;
public Node RootNode
{
get { return node; }
set { node = value; }
}

    public void  ShowNode(Node <T> node )
    {
        Console.Write(node .Date+" ");
    }

    //前序遍历-节点-左子树-右子树:+-*/ABCDE
    public void PerOrder()
    {
        PerOrder(node);
    }
    public void PerOrder(Node <T> root)
    {
        if (root!=null)
        {
            ShowNode(root);
            PerOrder(root.LeftNode);
            PerOrder(root.RightNode);
        }
    }
    //中序遍历-左子树-节点-右子树:A/B*C-D+E
    public void InOrder()
    {
        InOrder(node);

    }
    public void InOrder(Node<T> root)
    {
        if (root !=null)
        {
            InOrder(root.LeftNode);
            ShowNode(root); //显示当前节点
            InOrder(root.RightNode);
        }
    }
    //后序遍历-左子树-右子树-节点:AB/C*D-E+
    public void PostOrder()
    {
        PostOrder(node);
    }
    public void PostOrder(Node <T> root)
    {
        if (root!=null)
        {
            PostOrder(root.LeftNode);
            PostOrder(root.RightNode);
            ShowNode(root);
        }
    }

    
    /// <summary>
    /// 层序遍历-分别显示第一...层
    /// 显示:+
    /// 队列:显示+之前把节点的左子树节点,右子树节点先后放入队列中,然后显示该节点-将其移除队列
    /// </summary>
    public void LevelOrder()
    {
        Queue<Node<T>> q = new Queue<Node<T>>();
        Node<T> node = RootNode;
        while(node!=null)
        {
            ShowNode(node);
            if (node.LeftNode != null) q.Enqueue(node.LeftNode);
            if (node.RightNode != null) q.Enqueue(node.RightNode);
            if (q.Count == 0) return;
            node = q.Dequeue();
        }
    }
}

}
//测试程序
class Program
{
static void Main(string[] args)
{
BinaryTree tree = new BinaryTree(); ;
Node nodeOne ,nodeTwo, nodeTheer, nodeFore,a,b,c,d,e;
nodeOne = new Node('+');
nodeTwo = new Node('-');
nodeTheer = new Node('*');
nodeFore = new Node('/');
a = new Node('A');
b = new Node('B');
c = new Node('C');
d = new Node('D');
e = new Node('E');
tree.RootNode = nodeOne;
nodeOne.LeftNode = nodeTwo;
nodeOne.RightNode = e;
nodeTwo.LeftNode = nodeTheer;
nodeTwo.RightNode = d;
nodeTheer.LeftNode = nodeFore;
nodeTheer.RightNode = c;
nodeFore.LeftNode = a;
nodeFore.RightNode = b;

        /*//二叉树的中序遍历,以左子树-节点-右子树的顺序输出节点;
        tree.InOrder();

        //前序遍历
        tree.PerOrder();

        //后续排序
        tree.PostOrder();*/

        //层序遍历
        tree.LevelOrder();

    }
}
![](https://Wei715547.github.io/post-images/1593592462809.png)