Download presentation
Presentation is loading. Please wait.
1
Fall 2007CS 2251 Iterators and Tree Traversals
2
Fall 2007CS 2252 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
3
Fall 2007CS 2253 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
4
Fall 2007CS 2254 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
5
Fall 2007CS 2255 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
6
Fall 2007CS 2256 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
7
Fall 2007CS 2257 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
8
Fall 2007CS 2258 Collection Interface add( E element) addAll( Collection coll) clear() contains( Object e) containsAll( Collection coll) isEmpty() Iterator iterator() remove( Object e) removeAll( Collection coll) retainAll( Collection coll) size() toArray()
9
Fall 2007CS 2259 Collections API Implementation Hash Table Resizable Array Balanced Tree Linked List Hash Table + Linked List SetHashSetTreeSetLinkedHashSet ListArrayListLinkedList DequeArrayDequeLinkedList MapHashMapTreeMapLinkedHashMap
10
Fall 2007CS 22510 Iterable Interface Contains the method –Iterator iterator() The Collection interface extends the Iterable interface, so all classes that implement the Collection interface must provide an iterator method
11
Fall 2007CS 22511 The Iterator Interface The interface Iterator is defined as part of API package java.util Iterator methods –boolean hasNext() –E next() –void remove()
12
Fall 2007CS 22512 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
13
Fall 2007CS 22513 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
14
Fall 2007CS 22514 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
15
Fall 2007CS 22515 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
16
Fall 2007CS 22516 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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.