Tutorial 9

# Tutorial 9 - Tutorial 9 Iterators& Mathematical...

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

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

View Full Document
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 we’ve 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. Here’s 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

{[ snackBarMessage ]}

### Page1 / 7

Tutorial 9 - Tutorial 9 Iterators& Mathematical...

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

View Full Document
Ask a homework question - tutors are online