zoukankan      html  css  js  c++  java
  • 前序遍历_中序遍历_后序遍历

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

    /**
     *本实例演示二叉树的前序遍历
     * **/
    namespace 前序遍历
    {
        class Program
        {
            static void Main( string[ ] args )
            {
                BinaryTree b = new BinaryTree( "ABCDE#F" );
                b.ProOrder( b.Head );
                Console.WriteLine( );
                b.MidPrder( b.Head );
                Console.WriteLine( );
                b.AfterPrder( b.Head );
                Console.WriteLine( );
            }
        }

        public class Node
        {
            public int NodeValue { get; set; }
            public Node Left { get; set; }
            public Node Right { get; set; }


            public Node( int nodeValue )
            {
                NodeValue = nodeValue;
            }

            public override string ToString( )
            {
                return NodeValue.ToString( );
            }

        }

        public class BinaryTree
        {
            public Node Head { get; set; }
            private string satr;
            public BinaryTree( string currentNode )
            {
                satr = currentNode;
                Head = new Node( satr[ 0 ] );
                Add( Head, 0 );
            }

            private void Add( Node parcnt, int index )
            {

                int leftIndex = 2 * index + 1;
                if ( leftIndex < satr.Length )
                {
                    if ( satr[ leftIndex ] != '#' )
                    {
                        parcnt.Left = new Node( satr[ leftIndex ] );
                        Add( parcnt.Left, leftIndex );
                    }
                }

                int rightIndex = 2 * index + 2;
                if ( rightIndex < satr.Length )
                {
                    if ( satr[ rightIndex ] != '#' )
                    {
                        parcnt.Right = new Node( satr[ rightIndex ] );
                        Add( parcnt.Right, rightIndex );
                    }
                }

            }

            /// <summary>
            /// 先序遍历
            /// </summary>
            /// <param name="node"></param>
            public void ProOrder( Node node )
            {
                if ( node !=null )
                {
                    Console.Write(node.ToString() );
                    ProOrder( node.Left );
                    ProOrder( node.Right );
                }
            }

            /// <summary>
            ///中序遍历
            /// </summary>
            /// <param name="node"></param>
            public void MidPrder( Node node )
            {
                if ( node != null )
                {
                    ProOrder( node.Left );
                    Console.Write(node.ToString() );
                    ProOrder( node.Right );
                }
            }

            /// <summary>
            ///后序遍历
            /// </summary>
            /// <param name="node"></param>
            public void AfterPrder( Node node )
            {
                ProOrder( node.Left );
                ProOrder( node.Right );
                Console.Write(node.ToString() );
            }
        }
    }

  • 相关阅读:
    Docker虚拟机配置手札(centos)
    Nginx配置手札
    登录的顶号功能实现
    苹果登录服务端JWT算法验证-PHP
    mac Read-Only filesystem (转载)
    ssh公私钥登录/git公私钥认证
    crontab 定时访问指定url,定时脚本
    网站通用 敏感词列表
    游戏行业术语一览(2)--游戏运营转化率[转载]
    <转载>为什么VR不可能成功?
  • 原文地址:https://www.cnblogs.com/yuan-2012/p/3440547.html
Copyright © 2011-2022 走看看