1 / 16

Iterators and Tree Traversals

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.

magnar
Download Presentation

Iterators and Tree Traversals

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Iterators and Tree Traversals CS 225

  2. 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

  3. 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

  4. 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

  5. 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

  6. 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

  7. 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

  8. 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

  9. Collections API CS 225

  10. 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

  11. 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

  12. 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

  13. 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

  14. 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

  15. 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

  16. 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

More Related