This is particularly important in SQL: Nested set model in SQL, as it is an efficient way to transverse trees there, since querrying parents every time would require multiple disk accesses.
As a tree:
  • Root 1
    • Child 1.1
      • Child 1.1.1
      • Child 1.1.2
    • Child 1.2
      • Child 1.2.1
      • Child 1.2.2
As the sets:
 __________________________________________________________________________
|  Root 1                                                                  |
|   ________________________________    ________________________________   |
|  |  Child 1.1                     |  |  Child 1.2                     |  |
|  |   ___________    ___________   |  |   ___________    ___________   |  |
|  |  |  C 1.1.1  |  |  C 1.1.2  |  |  |  |  C 1.2.1  |  |  C 1.2.2  |  |  |
1  2  3___________4  5___________6  7  8  9___________10 11__________12 13 14
|  |________________________________|  |________________________________|  |
|__________________________________________________________________________|
Consider the following nested set:
0, 8, root
  1, 7, mathematics
    2, 3, geometry
      3, 6, calculus
        4, 5, derivative
        5, 6, integral
      6, 7, algebra
  7, 8, physics
When we want to insert one element, e.g. limit, normally under calculus, we have to specify:
  • parent
  • index within parent
so we have a method:
insert(parent, previousSibling)
The summary from www.geeksforgeeks.org/tree-traversals-inorder-preorder-and-postorder/ is a winner:
    1
   / \
  2   3
 / \
4   5
In principle one could talk about tree traversal of unordered trees as a number of possible traversals without a fixed order. But we won't consider that under this section, only deterministic ordered tree traversals.
This is the order in which you would want to transverse to read the chapters of a book.
Like breadth-first search, this also has the property of visiting parents before any children.
This is the easiest one to do iteratively:
  • pop and visit
  • push right to stack
  • push left to stack
This is the order in which a binary search tree should be traversed for ordered output, i.e.:
  • everything to the left is smaller than parent
  • everything to the right is larger than parent
This ordering makes sense for binary trees and not k-ary trees in general because if there are more than two nodes it is not clear what the top node should go in the middle of.
This is unlike pre-order depth-first search and post-order depth-first search which generalize obviously to general trees.
This is a bit harder than iterative pre-order: now we have to check if there is a left or right element or not:
  • (START) push current
  • if there is left:
    • move left
  • else:
    • (ELSE) pop
    • visit
    • if there is right
      • move right
      • GOTO START
    • else:
      • GOTO ELSE
The control flow can be slightly simplified if we allow NULLs: www.geeksforgeeks.org/inorder-tree-traversal-without-recursion/
Has the property of visiting all descendants before the parent.
This is the hardest one to do iteratively.
Bibliography:
www.geeksforgeeks.org/iterative-postorder-traversal/
www.geeksforgeeks.org/iterative-postorder-traversal-using-stack/

Discussion (0)

Sign up or sign in create discussions.

There are no discussions about this article yet.