二叉树遍历

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

namespace 前序遍历_中序遍历_后序遍历
{
    class Program
    {
        static void Main( string[ ] args )
        {
            BinaryTree bin = new BinaryTree( );
            bin.Insert( 23 );
            bin.Insert( 34 );
            bin.Insert( 45 );
            bin.PreOrder( bin.Head );
        }
    }

    public class Node
    {
        public int data { get; set; }
        public Node LeftNode { get; set; }
        public Node RightNode { get; set; }

        public void DisplayNode( )
        {
            Console.WriteLine( data + " " );
        }
    }

    public class BinaryTree
    {
        public Node Head;
        public BinaryTree( )
        {
            Head = null;
        }

        public void Insert( int i )
        {
            Node newNode = new Node( );
            newNode.data = i;
            if ( Head == null )
            {
                Head = newNode;
            }
            else
            {
                Node current = Head;
                Node parent;
                while ( true )
                {
                    parent = current;

                  if(i < current.data)

                 {
                    if ( parent == null )
                    {
                        parent.LeftNode = newNode;
                        break;
                    }

                  }

                  else

                  {

                       current = current.RightNode;

                          if ( parent == null )
                         {
                            parent.RightNode= newNode;
                            break;
                         }

                  }
                }
            }
        }

        /// <summary>
        /// 中序遍历
        /// </summary>
        /// <param name="theRoot"></param>
        public void InOrder( Node theRoot )
        {
            if ( !( theRoot == null ) )
            {
                InOrder( theRoot.LeftNode );
                theRoot.DisplayNode( );
                InOrder( theRoot.RightNode );
            }
        }

       
        /// <summary>
        /// 前序遍历
        /// </summary>
        /// <param name="theRoot"></param>
        public void PreOrder( Node theRoot )
        {
            if ( !( theRoot == null ) )
            {
                theRoot.DisplayNode( );
                PreOrder( theRoot.LeftNode );
                PreOrder( theRoot.RightNode );
            }
        }

       
        /// <summary>
        ///  后序遍历
        /// </summary>
        /// <param name="theRoot"></param>
        public void PosOrder( Node theRoot )
        {
            if ( !( theRoot == null ) )
            {
                PreOrder( theRoot.LeftNode );
                PreOrder( theRoot.RightNode );
                theRoot.DisplayNode( );
            }
        }
    }
}

转载于:https://www.cnblogs.com/yuan-2012/archive/2012/08/21/2648444.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值