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.

  • 相关阅读:
    HDU 2188.悼念512汶川大地震遇难同胞——选拔志愿者-巴什博奕
    hdu 4217 Data Structure? 树状数组求第K小
    hdu 5137 How Many Maos Does the Guanxi Worth 最短路 spfa
    Codeforces Round #375 (Div. 2) C. Polycarp at the Radio 贪心
    Codeforces Round #375 (Div. 2) D. Lakes in Berland dfs
    hiho 1325 : 平衡树·Treap
    bzoj 2656 [Zjoi2012]数列(sequence) 递推+高精度
    Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) C. Destroying Array
    Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) D. Generating Sets 贪心+优先队列
    Codeforces Round #374 (Div. 2) A , B , C 水,水,拓扑dp
  • 原文地址:https://www.cnblogs.com/lexus/p/2821105.html
Copyright © 2011-2022 走看看