CS 3343/3341
 Analysis of Algorithms 
  Complete   
   Binary Tree Traversals  


Introduction. Students in this course are usually familiar with traversals of binary search trees. There are three kinds: preorder, inorder, and postorder. Of these, the inorder traversal presents the elements of a binary search in the order used for the tree, so it is usually the most important. However, one common task is to visit each node of the tree in turn for a variety of purposes.

For example, in the syntax tree often used by compilers, one sometimes uses a complete traversal, illustrated below for the simple search tree made up of days of the week. Here we start at the root and go all the way around the tree, visiting each node three times. The three kinds of traversals are included: preorder in blue, inorder in green, and postorder in orange.


Complete Tree Traversal (click picture or days.pdf).
  
CompleteTraversal(root);

CompleteTraversal(node x) {
     if (x != null) {
          blue visit (preorder);
          CompleteTraversal(x.left);
          green visit (inorder);
          CompleteTraversal(x.right);
          orange visit (postorder);
     }
}

  
Monday
Friday
Friday
Friday
Monday
Tuesday
Thursday
Saturday
Saturday
Sunday
Sunday
Sunday
Saturday
Thursday
Thursday
Tuesday
Wednesday
Wednesday
Wednesday
Tuesday
Monday

Compilers use this kind of traversal during semantic analysis, where information can be passed down the tree (inherited) and passed up the tree (synthesized). It is also possible to leave important information at a node during the first or second visit, and pick it up or use it during a later visit.


Revision date: 2012-11-08. (Please use ISO 8601, the International Standard Date and Time Notation.)