2 Tree Traversal ClassificationThe process of systematically visiting all the nodes in a tree and performing some processing at each node in the tree is called a tree traversal.A traversal starts at the root of the tree and visits every node in the tree exactly once.There are two common methods in which to traverse a tree:Breadth-First Traversal (or Level-order Traversal).Depth-First Traversal:Preorder traversalInorder traversal (for binary trees only)Postorder traversal

3 Breadth-First TraversalLet queue be empty;if(tree is not empty)queue.enqueue(tree);while(queue is not empty){tree = queue.dequeue();visit(tree root node);if(tree.leftChild is not empty)enqueue(tree.leftChild);if(tree.rightChild is not empty)enqueue(tree.rightChild);}Note:When a tree is enqueued it is the address of the root node of that tree that is enqueuedvisit means to process the data in the node in some way

7 Depth-First TraversalsCODEfor each Node:Namepublic void preorderTraversal(Visitor v){if(!isEmpty() && ! v.isDone()){v.visit(getKey());getLeft().preorderTraversal(v);getRight().preorderTraversal(v);}Visit the nodeVisit the left subtree, if any.Visit the right subtree, if any.Preorder(N-L-R)public void inorderTraversal(Visitor v){getLeft().inorderTraversal(v);getRight().inorderTraversal(v);Visit the left subtree, if any. Visit the nodeInorder(L-N-R)public void postorderTraversal(Visitor v){getLeft().postorderTraversal(v) ;getRight().postorderTraversal(v);Postorder(L-R-N)

8 Preorder Depth-first TraversalN-L-R“A node is visited when passing on its left in the visit path”K F P M A U S R T

9 Inorder Depth-first TraversalL-N-R“A node is visited when passing below it in the visit path”Note: An inorder traversal can pass through a node without visiting it atthat moment.P F A M K S R U T

10 Postorder Depth-first TraversalL-R-N“A node is visited when passing on its right in the visit path”Note: An postorder traversal can pass through a node without visiting it at that moment.P A M F R S T U K

13 The accept method of the BinaryTree classUsually the accept method of a container is allowed to visit the elements of the container in any order.A depth-first tree traversal visits the nodes in either preoder or postorder and for Binary trees inorder traversal is also possible.The BinaryTree class accept method does a preorder traversal:public void accept(Visitor visitor){preorderTraversal(visitor) ;}

15 Using BinaryTree class IteratorThe iterator() method of the BinaryTree class returns a new instance of the BinaryTreeIterator inner class each time it is called:The following program fragment shows how to use a tree iterator:public Iterator iterator(){return new BinaryTreeIterator();}BinaryTree tree = new BinaryTree();// . . .Initialize treeIterator i = tree.iterator();while(i.hasNext(){Object obj = e.next() ;System.out.print(obj + " ");}

16 Expression TreesAn arithmetic expression or a logic proposition can be represented by a Binary tree:Internal vertices represent operatorsLeaves represent operandsSubtrees are subexpressionsA Binary tree representing an expression is called an expression tree.Build the expression tree bottom-up:Construct smaller subtreesCombine the smaller subtrees to form larger subtrees