Tutorial 9

Tutorial 9 - Tutorial 9: Iterators &...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Tutorial 9: Iterators & Mathematical Induction CS 134 Tutors Winter 2008 1 Iterators 1.1 Purpose of Iterators As weve seen, it is easy to traverse a binary tree structure recursively by keeping track of the present location within the tree. In general, it is often useful to iterate through all the elements of a data structure. Heres where iterators come in. Iterators are auxiliary classes which let us traverse data struc- tures one element at a time. 1.2 How Do We Use Iterators? Java provides an Iterator interface ( java.util.Iterator ). Here it is: public interface Iterator { public boolean hasNext(); public Object next(); public void remove(); } In order to use the Iterator , we add the following method to BinaryTreeInterface : // post: returns an Iterator that will iterate through the // elements of this binary tree in a post-order manner. public Iterator getPostOrderIterator(); 1 1.3 Exercise Find the sum of all nodes in a tree Assume the BinaryTreeInterface bt only holds objects of type Integer . Using the Iterator , write a method that will sum the values of every node in...
View Full Document

Page1 / 7

Tutorial 9 - Tutorial 9: Iterators &...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online