» Access elements of a container sequentially without exposing the underlying representation

Similar documents
Pattern Examples Behavioural

Think of drawing/diagramming editors. ECE450 Software Engineering II. The problem. The Composite pattern

a graph is a data structure made up of nodes in graph theory the links are normally called edges

CS 206 Introduction to Computer Science II

Last Lecture. Lecture 17: Design Patterns (part 2) Kenneth M. Anderson Object-Oriented Analysis and Design CSCI 4448/ Spring Semester, 2005

CSCI 253. Overview. The Elements of a Design Pattern. George Blankenship 1. Object Oriented Design: Iterator Pattern George Blankenship

Topic 14. The BinaryTree ADT

tree nonlinear Examples

Chapter 20: Binary Trees

Last Lecture. Lecture 26: Design Patterns (part 2) State. Goals of Lecture. Design Patterns

Design Patterns. On Design Patterns. Becoming a Master Designer. Patterns & Frameworks

CS 206 Introduction to Computer Science II

CSCI-401 Examlet #5. Name: Class: Date: True/False Indicate whether the sentence or statement is true or false.

Title Description Participants Textbook

Trees. Tree Structure Binary Tree Tree Traversals

Binary Trees

Principles of Computer Science

Programming II (CS300)

The tree data structure. Trees COL 106. Amit Kumar Shweta Agrawal. Acknowledgement :Many slides are courtesy Douglas Harder, UWaterloo

CS61B Lecture #20: Trees. Last modified: Mon Oct 8 21:21: CS61B: Lecture #20 1

SDC Design patterns GoF

CSC148 Week 6. Larry Zhang

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

CS 206 Introduction to Computer Science II

CS61B Lecture #20: Trees. Last modified: Wed Oct 12 12:49: CS61B: Lecture #20 1

Data Structure Lecture#10: Binary Trees (Chapter 5) U Kang Seoul National University

Chapter 11.!!!!Trees! 2011 Pearson Addison-Wesley. All rights reserved 11 A-1

Chapter 11.!!!!Trees! 2011 Pearson Addison-Wesley. All rights reserved 11 A-1

Object-Oriented Oriented Programming

Abstract Data Structures IB Computer Science. Content developed by Dartford Grammar School Computer Science Department

Section 5.5. Left subtree The left subtree of a vertex V on a binary tree is the graph formed by the left child L of V, the descendents

from inheritance onwards but no GUI programming expect to see an inheritance question, recursion questions, data structure questions

Binary Trees, Binary Search Trees

If you took your exam home last time, I will still regrade it if you want.

Tree Structures. A hierarchical data structure whose point of entry is the root node

Title Description Participants Textbook

Tree Travsersals and BST Iterators

CSI33 Data Structures

Design Pattern: Composite

STUDENT LESSON AB30 Binary Search Trees

CE 221 Data Structures and Algorithms

TREES. Trees - Introduction

Computer Science 210 Data Structures Siena College Fall Topic Notes: Trees

Tree. A path is a connected sequence of edges. A tree topology is acyclic there is no loop.

CSE143 Summer 2008 Final Exam Part B KEY August 22, 2008

Trees. (Trees) Data Structures and Programming Spring / 28

CS 127 Fall In our last episode. Yet more fun with binary trees. Tree traversals. Breadth-first traversal

Cpt S 122 Data Structures. Data Structures Trees

Binary Search Trees. See Section 11.1 of the text.

CS61B Lecture #20. Today: Trees. Readings for Today: Data Structures, Chapter 5. Readings for Next Topic: Data Structures, Chapter 6

Why Use Binary Trees? Data Structures - Binary Trees 1. Trees (Contd.) Trees

Iterator Design Pattern

Trees : Part 1. Section 4.1. Theory and Terminology. A Tree? A Tree? Theory and Terminology. Theory and Terminology

CSCD01 Engineering Large Software Systems. Design Patterns. Joe Bettridge. Winter With thanks to Anya Tafliovich

Name CPTR246 Spring '17 (100 total points) Exam 3

Topics in Object-Oriented Design Patterns

Data Structures - Binary Trees 1

Behavioral Design Patterns Used in Data Structures Implementation

Trees. Dr. Ronaldo Menezes Hugo Serrano Ronaldo Menezes, Florida Tech

Design Patterns Revisited

INF2220: algorithms and data structures Series 1

CS350: Data Structures Tree Traversal

Introduction to Trees. D. Thiebaut CSC212 Fall 2014

Computational Optimization ISE 407. Lecture 16. Dr. Ted Ralphs

Algorithms in Systems Engineering ISE 172. Lecture 16. Dr. Ted Ralphs

Cpt S 122 Data Structures. Course Review FINAL. Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University

L18.1 Introduction... 2

CIS 121. Binary Search Trees

CS61B Lecture #21. A Recursive Structure. Fundamental Operation: Traversal. Preorder Traversal and Prefix Expressions. (- (- (* x (+ y 3))) z)

Chapter 5. Binary Trees

Lecture 37 Section 9.4. Wed, Apr 22, 2009

Abstract Data Structures IB Computer Science. Content developed by Dartford Grammar School Computer Science Department

Garbage Collection: recycling unused memory

March 20/2003 Jayakanth Srinivasan,

Sorted Arrays. Operation Access Search Selection Predecessor Successor Output (print) Insert Delete Extract-Min

Trees. Introduction & Terminology. February 05, 2018 Cinda Heeren / Geoffrey Tien 1

Computer Science E-119 Fall Problem Set 4. Due prior to lecture on Wednesday, November 28

Trees. Q: Why study trees? A: Many advance ADTs are implemented using tree-based data structures.

Trees 11/15/16. Chapter 11. Terminology. Terminology. Terminology. Terminology. Terminology

Binary Trees Fall 2018 Margaret Reid-Miller

Trees. CSE 373 Data Structures

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

Comp Intermediate Programming EXAM #2 April 02, 2003 Rice University - Instructors: Cox & Nguyen

Design Pattern and Software Architecture: IV. Design Pattern

Lecture 26. Introduction to Trees. Trees

Data Structures and Algorithms

Associate Professor Dr. Raed Ibraheem Hamed

Successor/Predecessor Rules in Binary Trees

CS350: Data Structures Binary Search Trees

EXERCISES SOFTWARE DEVELOPMENT I. 10 Recursion, Binary (Search) Trees Towers of Hanoi // Tree Traversal 2018W

Data Structures and Algorithms for Engineers

Data Structures. Trees. By Dr. Mohammad Ali H. Eljinini. M.A. Eljinini, PhD

Terminology. The ADT Binary Tree. The ADT Binary Search Tree

CSC148H Week 8. Sadia Sharmin. July 12, /29

Outline. An Application: A Binary Search Tree. 1 Chapter 7: Trees. favicon. CSI33 Data Structures

Binary Trees. Examples:

Binary Trees. Reading: Lewis & Chase 12.1, 12.3 Eck Programming Course CL I

CSC148-Section:L0301

A Case Study of Gang of Four (GoF) Patterns : Part 7

Course Review for. Cpt S 223 Fall Cpt S 223. School of EECS, WSU

Transcription:

Iterator Pattern Behavioural Intent» Access elements of a container sequentially without exposing the underlying representation Iterator-1

Motivation Be able to process all the elements in a container Different iterators can give different sequential ordering» Binary tree > preorder, inorder, postorder» Do not need to extend container interface Iterator-2

Example Architecture BINARY_TREE size insertleft ( NODE, ELEMENT ) remove ( PARENT, NODE )... object ITERATOR * next * alldone * item * TREE_ITERATOR + Iterator knows the internal structure export all relevant features to it make_inorder make_preorder make_postorder next + alldone + item + Iterator-3

Example Client tree_items : TREE_ITERATOR... from create tree_items.make_inorder ( a_tree ) until tree_items.alldone loop item := tree_items.item process ( item ) tree_items.next end Iterator-4

Abstract Architecture CONTAINER + size add remove object ITERATOR * next * alldone * item * CONCRETE_ITERATOR + make_1 make_2 next + alldone + item + Iterator-5

Participants Iterator Defines interface for accessing and traversing a container s contents Concrete iterator» Implements the iterator interface» Keeps track of the current position in the traversal» Determines next object in a sequence of the container s objects Container Could provide a method to create an instance of an iterator Done in Java due to the poor export control Iterator-6

Applicability Access a container s contents without knowing about or using its internal representation Provide uniform interface for traversing a container s contents Support polymorphic iteration Iterator-7

Consequences Supports variations in the traversal of a container» Complex containers can be traversed in different ways Trees and graphs» Easy to change traversal order Replace iterator instance with a different one Iterators simplify the container interface Do not need iterator interface in container interface Multiple simultaneous traversals Each iterator keeps track of its own state Iterator-8

Implementation Can implement null iterators alldone is always True» Useful in traversing tree structures > At each level use iterator over children > At leaf level automatically get a null iterator > No exceptions at the boundary Iterator-9

Inorder Traversal Binary Tree public Enumeration inorderlrtraversal() { return new Enumeration() { Declare variables needed by the enumeration { } Initialization program for the enumerator } public boolean hasmoreelements() { Provide the definition } public Object nextelement() { Provide the definition } Iterator-10

Inorder Traversal Binary Tree 2 // Declare variables needed by the enumeration private Stack btstack = new Stack(); { // Initialization program for the enumerator // Simulate recursion by programming our own // stack. Need to get to the leftmost node as it // is first in the enumeration } Node node = tree; while (node!= null) { btstack.add(node); node = node.left; } top of stack root Iterator-11

Inorder Traversal Binary Tree 3 public boolean hasmoreelements() { return! btstack.isempty(); } Iterator-12

Inorder Traversal Binary Tree 4 top of stack initially root 3 5 2 4 1 InitStack 3 2 1 after call 1 6 5 4 2 1 after call 2 5 4 2 1 An enumerator is always 1 element ahead of the user 6 top of stack after first call to nextelement top of stack after second call to nextelement Iterator-13

Inorder Traversal Binary Tree 5 public Object nextelement() { if (btstack.isempty()) throw new NoSuchElementException(); } Node node = (Node) btstack.remove(); Object result = node.datum; // next data to return if (node.right!= null) { node = node.right; } do { btstack.add(node); node = node.left; } while (node!= null); return result; // Find next sequence node // Get leftmost node in // right subtree Notice that an enumerator is always 1 element ahead Iterator-14

What iterators do not do Create a copy, modify it and iterate over the copy» Sometimes efficiency may dictate a compromise Iterators do not modify the original» Leave a "bookmark"» Fold the page of a book Iterator-15

Should iterate aggregate contents? If aggregation is complex (binary tree traversal) and multiple iterations are needed, execution can be more efficient» Aggregate contains pointers to original Can be expensive in storage space for large collections If an object has multiple roles (occurs multiple times in the collection, e.g. Leila is the CEO and an engineer), then could lose a role with aggregation Iterator-16

Related Patterns Iterators are frequently applied to Composites Polymorphic iterators rely on factory methods to instantiate the appropriate Iterator subclass Memento is often used in conjunction with the Iterator pattern. An iterator can use a memento to capture the state of an iteration. The iterator stores the memento internally. Iterator-17

Iterator in Java API The example binary tree iterator in previous slides shows that the Java class Enumerator is an instantiation of the Iterator pattern Java also has the class Iterator with the following methods» next(), hasnext() and remove() Iterator-18