zoukankan      html  css  js  c++  java
  • 二叉树算法题

    二叉树类定义

    public class BinaryNode<T>
    {
            public T Element;
            public BinaryNode<T> Left;
            public BinaryNode<T> Right;

            public BinaryNode(T element, BinaryNode<T> left, BinaryNode<T> right)
            {
                this.Element = element;
                this.Left = left;
                this.Right = right;
            }

            public bool IsLeaf()
            {
                if (Left == null && Right == null)
                    return true;
                else
                    return false;
            }

    }

    获取二叉树的深度

    public int GetDepth(BTreeNode node)
    {

            if(node == null)
                return 0;
            else
            {
                int d1=GetDepth(node.LNode);
                int d2=GetDepth(node.RNode);
             }

             return d1>d2?d1++:d2++;

    }

    public int GetDepthNoRecursive(BTreeNode root)

    {

          Queue<int> que = new Queue<int>();

          while(root!=null)
         {

             

          }

    }

    二叉树广度优先遍历

    先序遍历: 先访问节点本身在访问左子节点最后是右子节点. 

     

    二叉树的前序遍历:

    public void PreOrder(BinaryNode<T> root)
    {
               if (root == null)
                   return;
               //visit current node
               Console.WriteLine(root.Element);
               PreOrder(root.Left);
               PreOrder(root.Right);
    }

    二叉树前序遍历 非递归算法:

    public void PreOrderNoRecursive(BinaryNode<T> root)
    {
               if (root == null)
                   return;

               Stack<BinaryNode<T>> stack = new Stack<BinaryNode<T>>();
               while(root !=null || stack.Count > 0)

               {

                     while(root != null){

                              stack.Push(root);

                              Console.WriteLine(root.Element);

                              root = root.Left;

                     }

                     if(stack.Count > 0)

                     {    

                            root = stack.Pop();

                            root = root.Right;

                     }    

               }

    }

    二叉树中序遍历

    InOrder visit

    二叉树中序遍历 递归算法

    public void InOrder(BinaryNode<T> root)

    {

          if(root == null) return;

          InOrder(root.Left);

          Console.WriteLine(root.Element);

          InOrder(root.Right);

    }

    二叉树中序遍历 非递归算法

    public void InOrderNoRecursive(BinaryNode<T> root)

    {

          if(root == null)

                 return;

          Stack<BinaryNode<T>> stack = new Stack<BinaryNode<T>>();


          while(root!=null || stack.Count>0 )
         {

               while (root != null)

               {

                   stack.Push(root);   

                   root = root.Left;

                }

                 if(stack.Count > 0)

                 {

               root = stack.Pop();

               Console.WriteLine(root.Element);

               root = root.Right

                 }

          }

    }

    后序遍历

    public void PostOrder(BinaryNode<T> root){

          if(root == null)

                return;

          PostOrder(root.Left);

          PostOrder(root.Right);

    }

    public void PostOrderNoRecursive(BinaryNode<T> root)

    {

            if(root == null)

                return;

            Stack<BinaryNode<T>> stack = new Stack<BinaryNode<T>>();

            while(root != null || stack.Count>0){

                   while(root!= null)

                   {

                          stack.Push(root);

                          if(root.left != null)

                               root = root.Left;

                          else

                              root = root.right;

                    }     

                    root = stack.Pop();   

                    Console.WriteLine(root.Element); 

                    if(stack.Count>0 && root == stack.Peek().Left)

                   {

                          root = stack.Peek().Right;

                    }

                    else

                    {

                          root = null;

                     }    

           }
     }

  • 相关阅读:
    SharePoint 2010 与RMS集成方案
    VBS基础教程
    c#中csc命令的用法
    文本文件编码格式转换
    “本地连接”属性中“身份验证”选项卡消失的处理方法
    如何组建中小型SharePoint服务器场
    PHP文件上传详解
    Flash AS3 RadioButton的使用方法
    SUSE ssh登录慢解决办法,ssh登录失败,但是strace一下就好了的分析查询 第一次ssh,路由的问题
    AS3组件之ComboBox下拉框
  • 原文地址:https://www.cnblogs.com/stone/p/1877156.html
Copyright © 2011-2022 走看看