zoukankan      html  css  js  c++  java
  • 二叉树遍历:先序、中序、后序和层次遍历

    public class BinaryNode
        
    {
            
    public int Data;
            
    public BinaryNode Left;
            
    public BinaryNode Right;
        }



    public class BinaryTree
        
    {
            
    public void PreOrder(BinaryNode node)
            
    {
                
    if (node != null)
                
    {
                    Console.WriteLine(node.Data);

                    PreOrder(node.Left);
                    PreOrder(node.Right);
                }

            }


            
    public void MidOrder(BinaryNode node)
            
    {
                
    if (node != null)
                
    {
                    MidOrder(node.Left);
                    Console.WriteLine(node.Data);
                    MidOrder(node.Right);
                }

            }


            
    public void AfterOrder(BinaryNode node)
            
    {
                
    if (node != null)
                
    {
                    AfterOrder(node.Left);
                    AfterOrder(node.Right);
                    Console.WriteLine(node.Data);
                }

            }


            
    public void LayerOrder(BinaryNode node)
            
    {
                
    if (node != null)
                
    {
                    CurrentLayer(node.Left);
                    CurrentLayer(node.Right);

                    LayerOrder(node.Left);
                    LayerOrder(node.Right);
                }

            }


            
    private void CurrentLayer(BinaryNode node)
            
    {
                
    if (node != null)
                
    {
                    Console.WriteLine(node.Data);
                }

            }


        }


    测试:
                BinaryNode n1 = new BinaryNode();
                n1.Data 
    = 27;

                BinaryNode n2 
    = new BinaryNode();
                n2.Data 
    = 83;

                BinaryNode n3 
    = new BinaryNode();
                n3.Data 
    = 123;

                BinaryNode n4 
    = new BinaryNode();
                n4.Data 
    = 98;

                BinaryNode n5 
    = new BinaryNode();
                n5.Data 
    = 94;

                BinaryNode n6 
    = new BinaryNode();
                n6.Data 
    = 48;

                BinaryNode n7 
    = new BinaryNode();
                n7.Data 
    = 61;

                n1.Left 
    = n2;
                n1.Right 
    = n3;
                n1.Left.Left 
    = n4;
                n1.Left.Right 
    = n5;
                n1.Right.Right 
    = n6;
                n1.Right.Right.Left 
    = n7;

                BinaryTree bTree 
    = new BinaryTree();

                Console.WriteLine(
    "Pre Order Binary Tree:");
                bTree.PreOrder(n1);

                Console.WriteLine(
    "Midd Order Binary Tree:");
                bTree.MidOrder(n1);

                Console.WriteLine(
    "After Order Binary Tree:");
                bTree.AfterOrder(n1);

                Console.WriteLine(
    "Layer Order Binary Tree:");
                bTree.LayerOrder(n1);

                Console.ReadLine();

  • 相关阅读:
    MySQL语法
    Linux常用命令大全
    触发器使用UTL_SMTP包发送邮件
    MySQL——触发器的创建和使用总结
    MySQL数据库备份
    Nginx配置文件(nginx.conf)配置详解
    JS弹出框,打开文件,保存文件,另存为。。。。
    java excel两种操作方式
    Zookeeper的优缺点
    activemq linux安装
  • 原文地址:https://www.cnblogs.com/silva/p/1091806.html
Copyright © 2011-2022 走看看