Docsity

Binary Search Tree Iterator: Implementing Tree Traversals with an Iterator, Exams of Data Structures and Algorithms

Toggle sidebar
bg1
worksheet 19: Binary Search Tree Iterator Name:
An Active Learning Approach to Data Structures using C
1
Worksheet 19: Binary Search Tree Iterator
To make the BST a useful container we need an iterator. This iterator must
produce the elements in sequence. As we noted previously, this means making
an in-order traversal of the tree. Examine the following tree and see if you can
guess an algorithm that will do this. Remember that, in our implementation,
nodes do not point back up to their parent nodes. How will you remember the
path you have followed down the tree?
As you might have guessed, one way to do this is to have the iterator maintain an
internal stack. This stack will represent the current path that has been traversed
from root to leaf. Notice that the first element that our iterator should produce is
the leftmost child of the root, and that furthermore all the nodes between the root
and this point should be added to the stack. A useful routine for this purpose is
slideLeft:
Using the slideLeft routine, verify that the following
algorithm will produce the desired iterator traversal
of the binary search tree. The remove portion of
the iterator interface is more complex, and we will
not consider it here.
BSTIteratorInit
to initialize, create an empty stack
int BSTIteratorHasNext
void slideLeft (Node n)
while n is not null
stack n
n = left child of n
pf3
pf4

Partial preview of the text

Download Binary Search Tree Iterator: Implementing Tree Traversals with an Iterator and more Exams Data Structures and Algorithms in PDF only on Docsity!

Worksheet 19: Binary Search Tree Iterator

To make the BST a useful container we need an iterator. This iterator must produce the elements in sequence. As we noted previously, this means making an in-order traversal of the tree. Examine the following tree and see if you can guess an algorithm that will do this. Remember that, in our implementation, nodes do not point back up to their parent nodes. How will you remember the path you have followed down the tree? As you might have guessed, one way to do this is to have the iterator maintain an internal stack. This stack will represent the current path that has been traversed from root to leaf. Notice that the first element that our iterator should produce is the leftmost child of the root, and that furthermore all the nodes between the root and this point should be added to the stack. A useful routine for this purpose is slideLeft: Using the slideLeft routine, verify that the following algorithm will produce the desired iterator traversal of the binary search tree. The remove portion of the iterator interface is more complex, and we will not consider it here. BSTIteratorInit to initialize, create an empty stack int BSTIteratorHasNext void slideLeft (Node n) while n is not null stack n n = left child of n

if stack is empty perform slideLeft on root otherwise let n be top of stack. Pop topmost element. slideLeft on right child of n return true if stack is not empty double BSTIteratorNext let n be top of stack. Return value of n Can you characterize what elements are being held on the stack? How large can the stack grow? Assuming that we have already defined a Stack that will hold pointers to nodes, show the implementation of the functions that implement the iterator algorithms described above. struct BSTIterator { struct NodeStack stk; }; void BSTIteratorInit (struct BinarySearchTree *tree, struct BSTIterator *itr) { } int BSTIteratorHasNext (struct BSTIterator * itr) { } EleType BSTITeratorNext (struct BSTIterator *itr) { }

Euler Tours The three common tree-traversal algorithms can be unified into a single algorithm by an approach that visits every node three times. A node will be visited before its left children (if any) are explored, after all left children have been explored but before any right child, and after all right children have been explored. This traversal of a tree is termed an Euler tour. An Euler tour is like a walk around the perimiter of a binary tree. public class EulerTour { public final void traverse (Node n ) { beforeLeft(n); if (n.left != null) traverse (n.left); inBetween (n); if (n.right != null) traverse (n.right); afterRIght(n); } public void beforeLeft (Node n) { … } // will be overridden public void inBetween (Node n) { … } public void afterRight (Node n) { … } } The user constructs an euler tour by subclassing from EulerTour and overriding the methods beforeLeft, inBetweeen and afterRight. To invoke the tour the root node is passed to the method traverse. For example, if a tree represents an arithmetic expression the following could be used to print the representation. class PrintExpression extends EulerTOur { public void beforeLeft (Node n) { System.out.print(“(“); } public void inBetween (Node n) { System.out.print(n.value); } public void afterRight (Node n) { System.out.print(“)”); } }