zoukankan      html  css  js  c++  java
  • CSharp Algorithm

    /*

    Author: Jiangong SUN

    */


    Here I will introduce the breadth first traversal of binary tree.


    The principe is that you traverse the binary tree level by level. 


    This traversal is quite different than depth first traversal. In depth first traversal you can use recursive method to traverse.


    Here is one solution using a queue.


            /// <summary>
            /// breadth-first traversal 宽度遍历树
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="node"></param>
            public void Traversal<T>(Node<T> node)
            {
                //create a Node<T> queue
                var treeQueue = new Queue<Node<T>>();
                //initialize queue with tree root node
                treeQueue.Enqueue(node);
    
                //if queue has elements
                while (treeQueue.Count > 0)
                {
                    //dequeue the queue's first node 
                    Node<T> current = treeQueue.Dequeue();
    
                    //print the node name
                    PrintNodeName(current);
                    
                    //if node has Left leaf, enqueue it
                    if (current.LNode != null)
                        treeQueue.Enqueue(current.LNode);
                    
                    //if node has right leaf, enqueue it
                    if (current.RNode != null)
                        treeQueue.Enqueue(current.RNode);
                }
            }



    references;

    http://www.cs.bu.edu/teaching/c/tree/breadth-first/

    http://hectorea.com/blog/programming-interview-31-binarytree-traversal-by-levels-breadth-first/#

  • 相关阅读:
    [require-js]向下滑动ajax加载的javascript实现
    Date的ToString方法
    GMAT语法总结
    流程控制语句:if、if else、if else if、嵌套if
    Random类
    Scanner类
    java运算符
    java数据类型转换
    mysql查询语句
    mysql常用语句
  • 原文地址:https://www.cnblogs.com/aukle/p/3235642.html
Copyright © 2011-2022 走看看