Binary Tree Traversals Binary Tree Traversal classification Breadth

Binary Tree Traversals • Binary Tree Traversal classification • Breadth. First traversal • Depth. First traversal • Accept method of Binary. Tree class • Binary Tree Iterator 1

Tree Traversal (Definition) • The process of systematically visiting all the nodes in a tree and performing some computation at each node in the tree is called a tree traversal. • There are two methods in which to traverse a tree: 1. Breadth-First Traversal. 2. Depth-First Traversal: • Preorder traversal • Inorder traversal (for binary trees only) • Postorder traversal 2

Breadth-First Traversal • The Binary. Tree class breadth. First. Traversal method: public void breadth. First. Traversal(Visitor visitor){ Queue. As. Linked. List queueaslinkedlist = new Queue. As. Linked. List(); if(!is. Empty()) queueaslinkedlist. enqueue(this); while(!queueaslinkedlist. is. Empty() && !visitor. is. Done()){ Binary. Tree tree = (Binary. Tree)queueaslinkedlist. dequeue(); visitor. visit(tree. get. Key()); if (!tree. get. Left(). is. Empty()) queueaslinkedlist. enqueue(tree. get. Left()); if (!tree. get. Right(). is. Empty()) queueaslinkedlist. enqueue(tree. get. Right()); } } 3

Breadth-First Traversal H D L F B A H E C D L B F I G J N A C K E G M I K O M O 4

Depth-First Traversals Name for each Node: • Visit the Preorder • Visit the (N-L-R) • Visit the Inorder (L-N-R) CODE public void preorder. Traversal(Visitor v){ if(!is. Empty() && ! v. is. Done()){ node v. visit(get. Key()); get. Left(). preorder. Traversal(v); left subtree, if any. get. Right(). preorder. Traversal(v); right subtree, if any. } } public void inorder. Traversal(Visitor v){ if(!is. Empty() && ! v. is. Done()){ • Visit the left subtree, if any. get. Left(). inorder. Traversal(v); v. visit(get. Key()); Visit the node get. Right(). inorder. Traversal(v); • Visit the right subtree, if any. } } • Visit the Postorder • Visit the (L-R-N) • Visit the public void postorder. Traversal(Visitor v){ if(!is. Empty() && ! v. is. Done()){ left subtree, if any. get. Left(). postorder. Traversal(v) ; get. Right(). postorder. Traversal(v); right subtree, if any. v. visit(get. Key()); node } 5 }

Depth-first Preorder Traversal N-L-R H D L F B A H E C D B A C I G F N J E G L K J I M K N O M O 6

Depth-first Inorder Traversal L-N-R H D L F B A A E C B C D E I G F N J G H I K J K M L M O Note: An inorder traversal of a BST visits the keys sorted in increasing order. 7

Depth-first Postorder Traversal L-R-N H D L F B A A E C C B E G I G F N J D I K K J M M O N O L H 8

Traversals • The following code illustrates how to display the contents of a Binary tree using each traversal method. Visitor v = new Printing. Visitor() ; Binary. Tree t = new Binary. Tree() ; //. . . t. breadth. First. Traversal(v) ; t. postorder. Traversal(v) ; t. inorder. Traversal(v) ; t. postorder. Traversal(v) ; 9

The accept method of the Binary. Tree class • Usually 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 Binary. Tree class accept method does a preorder traversal: public void accept(Visitor visitor) { preorder. Traversal(visitor) ; } • What is the time complexity of the accept method of traversal algorithms? 10

Binary Tree Iterator • The Binary. Tree class provides a tree iterator that does a preorder traversal. The iterator is implemented as an inner class: private class Binary. Tree. Iterator implements Iterator{ Stack stack; public Binary. Tree. Iterator(){ stack = new Stack. As. Linked. List(); if(!is. Empty())stack. push(Binary. Tree. this); } public boolean has. Next(){return !stack. is. Empty(); } public Object next(){ if(stack. is. Empty())throw new No. Such. Element. Exception(); Binary. Tree tree = (Binary. Tree)stack. pop(); if (!tree. get. Right(). is. Empty()) stack. push(tree. get. Right()); if (!tree. get. Left(). is. Empty()) stack. push(tree. get. Left()); return tree. get. Key(); } } 11

Using a Binary Tree Iterator • The iterator() method of the Binary. Tree class returns a new instance of the Binary. Tree. Iterator inner class each time it is called: public Iterator iterator(){ return new Binary. Tree. Iterator(); } • The following program fragment shows how to use a tree iterator: Binary. Tree tree = new Binary. Tree() ; //. . . Iterator i = tree. iterator() ; while(i. has. Next(){ Object obj = e. next() ; System. out. print(obj + " ") ; } 12
- Slides: 12