160 likes | 310 Views
Iterators and Tree Traversals. Binary Trees. In a binary tree, each node has at most two subtrees A set of nodes T is a binary tree if either of the following is true T is empty Its root node has two subtrees, TL and TR, such that TL and TR are binary trees. Tree Traversals.
E N D
Binary Trees • In a binary tree, each node has at most two subtrees • A set of nodes T is a binary tree if either of the following is true • T is empty • Its root node has two subtrees, TL and TR, such that TL and TR are binary trees CS 225
Tree Traversals • Often we want to visit all the nodes of a tree and do something with each • A tree traversal is a path that visits every node in the tree • Two ways to traverse a tree • Depth first - Inorder, Preorder and Postorder traversals • Breadth-first - Level order traversal CS 225
Depth-First Tree Traversals • Preorder Tree Traversal • Visit root node, traverse TL, traverse TR • Inorder Tree Traversal • Traverse TL, visit root node, traverse TR • Postorder Tree Traversal • Traverse TL, Traverse TR, visit root node CS 225
Level-Order Traversal • Visit all nodes at one level of the tree before going on to the next level • In the tree below, a level-order traversal gives a-b-c-d-e-f-g-h-i-j-k CS 225
Implementing Traversals • How do we write methods to traverse a tree? • For depth-first traversals, we can use recursive methods • But what if we want an iterator which gives only one element at a time? • For level-order traversals, algorithm isn't so obvious CS 225
Common Features of Collections • Collection interface specifies a set of common methods • Fundamental features include: • Collections grow as needed • Collections hold references to objects • Collections have at least two constructors CS 225
add( E element) addAll( Collection<E> coll) clear() contains( Object e) containsAll( Collection<E> coll) isEmpty() Iterator iterator() remove( Object e) removeAll( Collection<E> coll) retainAll( Collection<E> coll) size() toArray() Collection Interface CS 225
Collections API CS 225
Iterable<E> Interface • Contains the method • Iterator<E> iterator() • The Collection interface extends the Iterable interface, so all classes that implement the Collection interface must provide an iterator method CS 225
The Iterator<E> Interface • The interface Iterator is defined as part of API package java.util • Iterator methods • boolean hasNext() • E next() • void remove() CS 225
Implementing Iterators • For linear collections, implementing an iterator just requires keeping track of where you are in the collection with a reference variable or an index. • For trees, the problem is a little more complicated • You can't interrupt a recursive method CS 225
Depth-first searches • Use a stack to keep track of where you are in the recursive process • Setting up the iterator push the root node on the stack • Algorithm for next pre-order pop the top node process the node push right child push left child CS 225
Depth first iterators • For the other traversals, each node needs to be pushed multiple times • need to keep track of how many times it has been pushed • For in-order searches the node gets pushed twice and processed on the second pop • For post-order traversals the node gets pushed three times CS 225
Level-Order traversals • In this case, we can use a queue • Initializing the iterator enqueue the root • Algorithm for next dequeue the first element enqueue the left child enqueue the right child process the removed node CS 225
Visitor Design Pattern • This is a common way of handling the process of performing an action on each element of a collection • The Visitor interface has a single method • visit( E element) • The collection class can have traversal methods that take a Visitor as a parameter CS 225