二叉树的前序遍历,中序遍历和后序遍历(c#)

该程序很简单的实现了一个node类和一个Bintree类,没有多余的功能。

如果还要加功能,直接加即可。

 

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

namespace BinaryTree
{
    class Program
    {
        static void Main(string[] args)
        {
            Node<string> root = new Node<string>("0");
            Node<string> node1 = new Node<string>("1");
            Node<string> node2 = new Node<string>("2");
            Node<string> node3 = new Node<string>("3");
            Node<string> node4 = new Node<string>("4");
            Node<string> node5 = new Node<string>("5");
            Node<string> node6 = new Node<string>("6");
            Node<string> node7 = new Node<string>("7");
            root.Lchild = node1;
            root.Rchild = node2;
            node1.Lchild = node3;
            node1.Rchild = node4;
            node2.Lchild = node5;
            node2.Rchild = node6;
            node3.Lchild = node7;
            BinTree<string> tree = new BinTree<string>(root);
            tree.PreOrder();
            tree.InOrder();
            tree.PostOrder();
        }

    }
    public class Node<T>
    {
        // private filed
        private T num;
        private Node<T> lchild;
        private Node<T> rchild;

        //property
        public T Num
        {
            get
            {
                return num;
            }
            set
            {
                num = value;
            }
        }
        public Node<T> Lchild
        {
            get
            {
                return lchild;
            }
            set
            {
                lchild = value;
            }
        }
        public Node<T> Rchild
        {
            get
            {
                return rchild;
            }
            set
            {
                rchild = value;
            }
        }

        //constructor
        public Node(T val)
        {
            num = val;
            rchild = null;
            lchild = null;
        }

    }

    public class BinTree<T>
    {
        //filed
        private Node<T> head;

        //property
        public Node<T> Head
        {
            get
            {
                return head;
            }
            set
            {
                head = value;
            }
        }

        //constructor
        public BinTree()
        {
            head = null;
        }
        public BinTree(Node<T> p)
        {
            head = p;
        }

        //method
        public Node<T> GetHead(Node<T> p)
        {
            return head;
        }
        public Node<T> GetLchild(Node<T> p)
        {
            return p.Lchild;
        }
        public Node<T> GetRchild(Node<T> p)
        {
            return p.Rchild;
        }

        //前序遍历
        public void PreOrder()
        {
            Console.WriteLine("PreOrder:----------------");
            PreOrder(this.head);
        }
        public void PreOrder(Node<T> p)
        {
            if (p == null) return;
            Console.WriteLine(p.Num);
            PreOrder(p.Lchild);
            PreOrder(p.Rchild);
        }

        //中序遍历
        public void InOrder()
        {
            Console.WriteLine("InOrder:----------------");
            InOrder(this.head);
        }
        public void InOrder(Node<T> p)
        {
            if (p == null) return;
            InOrder(p.Lchild);
            Console.WriteLine(p.Num);
            InOrder(p.Rchild);
        }

        //后序遍历
        public void PostOrder()
        {
            Console.WriteLine("PostOrder:----------------");
            PostOrder(this.head);
        }
        public void PostOrder(Node<T> p)
        {
            if (p == null) return;
            PostOrder(p.Lchild);
            PostOrder(p.Rchild);
            Console.WriteLine(p.Num);
        }
    }
}

你可能感兴趣的:(二叉树的前序遍历,中序遍历和后序遍历(c#))