zoukankan      html  css  js  c++  java
  • JavaScript Lab Articles Nonrecursive Preorder Traversal Part 4

    JavaScript Lab - Articles - Non-recursive Preorder Traversal - Part 4

    Non-recursive Preorder Traversal - Part 4

    Published on 18th of December 2009. Copyright Tavs Dokkedahl. Displayed 6416 time(s)

    Different kind of traversals

    Preorder is just one way of traversing a tree. Postorder and levelorder is two different approaches. There also exist inorder traversal but that is only well defined for binary trees and is thus not very usefull for DOM trees.

    Non-recursive postorder traversal

    In a postorder traversal the nodes are visited in the following order

    Tree data structure

    Again the red numbers indicates when the node is visited but not processed. A non-recursive algorithm for postorder traversal is

     1 function postorderTraversal(root) {
     2   var n = root;
     3   while(n) {
     4     // Find first left most leaf on a path which have not
     5     // yet been visited
     6     if (n.firstChild && !n.v) {
     7       // Mark node as visited
     8       n.v = true;
     9       n = n.firstChild;
    10     }
    11     else {
    12       //
    13       // Do something with node
    14       //
    15       // If n is root node, traversal is complete so break loop
    16       if (n == root)
    17         break;
    18       // Find next node
    19       if (n.nextSibling)
    20         n = n.nextSibling;
    21       else
    22         n = n.parentNode;
    23     }
    24   }
    25 }
    

    As you can see it is not much different from the preorder algorithm although slightly more simple.

    Levelorder traversal

    The final form of traversal is levelorder traversal. Levelorder traversal visits the nodes as they are ordered according to the level in the tree.

    Tree data structure

    The easiest way to implement levelorder without recursion is to use a queue.

     1 // Non-recursive levelorder traversal of DOM tree
     2 function levelorderTraversal(root) {
     3   // Initialize queue to contain root element
     4   var q1 = [root];
     5   // While there are elements in the queue
     6   while(q1.length) {
     7     var q2 = [];
     8     // For each element in queue
     9     for(var i=0; i<q1.length; i++) {
    10       //
    11       // Do something with node q[i]
    12       //
    13       // Create new queue with childnodes of elements in queue
    14       for(var j=0; j<q1[i].childNodes.length; j++)
    15         q2.push(q1[i].childNodes[j]);
    16     }
    17     q1 = q2;
    18   }
    19 }
    

    I never really had the need for anything besides preorder traversal but if you can think of some common places where one could utilize postorder or levelorder concerning DOM trees please leave a comment.

  • 相关阅读:
    bladex从blade-dev.yaml 读取配置信息
    怎么判断map不为空
    根据来源编号对明细进行分组 跟库存做对比 用到的技术 list根据某个字段分组 Double Long 比较大小
    hibernate根据包名获取该包下实体类,数据库中不存在的库表名称
    hibernate NonUniqueObjectException 一个session中存有两个识别码(id)相同的对象
    正则匹配不包含某些字符 update(?!tdyt)[A-z(]*(qlgz)
    idea常用插件
    Rainbow Roads
    Jumping Haybales (dp)
    01背包
  • 原文地址:https://www.cnblogs.com/lexus/p/2821105.html
Copyright © 2011-2022 走看看