Fall 2007ACS - 3913 Ron McFadyen1 Composite Pattern (see pages 353-367) A composite is a group of objects in which some objects contain others; one object.

Slides:



Advertisements
Similar presentations
CS Fall 2012, Lab 08 Haohan Zhu. Boston University Slideshow Title Goes Here CS Fall 2012, Lab /17/2015 Tree - Data Structure  Basic.
Advertisements

Binary Trees CSC 220. Your Observations (so far data structures) Array –Unordered Add, delete, search –Ordered Linked List –??
Winter 2007ACS-3913 Ron McFadyen1 Duck Example Consider the text example (up to page 6). Each type of duck is a subclass of Duck Most subclasses implement.
SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group Binary Search Tree A running demonstration of binary search.
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary trees Reading: L&C 9.1 – 9.7.
March Ron McFadyen1 Composite Used to compose objects into tree structures to represent part-whole hierarchies Composite lets clients treat.
Fall 2009ACS-3913 Ron McFadyen Composite Pattern Problem: How do we treat a composition structure of objects the same way as a non-composite object? Arises.
Fall 2009ACS-3913 Ron McFadyen1 Decorator Pattern The Decorator pattern allows us to enclose an object inside another object. The enclosing object is called.
DictionaryADT and Trees. Overview What is the DictionaryADT? What are trees? Implementing DictionaryADT with binary trees Balanced trees DictionaryADT.
Feb Ron McFadyen1 Iterator Pattern Recall Generic UML class diagram The iterator is used to access the elements of some aggregate. The aggregate.
Fall 2007CS 2251 Trees Chapter 8. Fall 2007CS 2252 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information.
Binary Trees Terminology A graph G = is a collection of nodes and edges. An edge (v 1,v 2 ) is a pair of vertices that are directly connected. A path,
Iterators T.J. Niglio Computer & Systems Engineering Fall 2003 Software Design & Documentation Object Behavioral.
Sept 2004Ron McFadyen Decorator Pattern The decorator pattern allows us to enclose an object inside another object. The enclosing object is called.
Fall 2007CS 2251 Iterators and Tree Traversals. Fall 2007CS 2252 Binary Trees In a binary tree, each node has at most two subtrees A set of nodes T is.
Spring 2010ACS-3913 Ron McFadyen1 Command The command pattern encapsulates a request or unit of work into an object. An invoker will ask a concrete command.
Feb Ron McFadyen1 Iterator Pattern Generic UML class diagram The iterator is used to access the elements of some aggregate. The aggregate interface.
Winter 2015ACS Ron McFadyen1 Composite Pattern A composite is a group of objects in which some objects contain others; one object may represent.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 19 Binary.
The Composite Pattern.. Composite Pattern Intent –Compose objects into tree structures to represent part-whole hierarchies. –Composite lets clients treat.
Trees Chapter 23 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
CHAPTER 12 Trees. 2 Tree Definition A tree is a non-linear structure, consisting of nodes and links Links: The links are represented by ordered pairs.
CS21, Tia Newhall Binary Search Trees (BST) 1.Hierarchical data structure with a single pointer to root node 2.Each node has at most two child nodes (a.
Winter 2011ACS-3913 Ron McFadyen1 Decorator Sometimes we need a way to add responsibilities to an object dynamically and transparently. The Decorator pattern.
Spring 2010ACS-3913 Ron McFadyen1 Duck Example Consider the text example (up to page 6). Each type of duck is a subclass of Duck Most subclasses implement.
Chapter 18 - basic definitions - binary trees - tree traversals Intro. to Trees 1CSCI 3333 Data Structures.
By : Budi Arifitama Pertemuan ke Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation.
Composite Design Pattern. Motivation – Dynamic Structure.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Trees & Graphs Chapter 25 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X.
Thought for the Day “To become truly great, one has to stand with people, not above them.” – Charles de Montesquieu.
CS Data Structures Chapter 15 Trees Mehmet H Gunes
Topic 14 The BinaryTree ADT Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms.
Trees Chapter 8. Chapter 8: Trees2 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information To learn how.
Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.
CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists.
Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.
CMSC 341 Introduction to Trees. 8/3/2007 UMBC CMSC 341 TreeIntro 2 Tree ADT Tree definition  A tree is a set of nodes which may be empty  If not empty,
Chapter 9: The Iterator Pattern
程式語言結構 Final Project. Goal The student can gain the knowledge of the object operation for the Java programming language. –Class –Interface.
Jan Ron McFadyen1 Decorator Sometimes we need a way to add responsibilities to an object dynamically and transparently. The Decorator pattern.
程式語言結構 Final Project 老師: Gwan-Hwan Hwang. Goal The students can gain the knowledge of the object operation for the Java programming language. –Class –Interface.
Topic 15 The Binary Search Tree ADT Binary Search Tree A binary search tree (BST) is a binary tree with an ordering property of its elements, such.
Binary Search Tree Traversal Methods. How are they different from Binary Trees?  In computer science, a binary tree is a tree data structure in which.
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
November Ron McFadyen1 Composite Pattern A composite is a group of objects in which some objects contain others; one object may represent groups,
Topics Definition and Application of Binary Trees Binary Search Tree Operations.
Trees : Part 1 Section 4.1 (1) Theory and Terminology (2) Preorder, Postorder and Levelorder Traversals.
Computer Science 112 Fundamentals of Programming II Introduction to Trees.
1 Chapter 7 Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation of the binary search tree.
CMSC 341 Introduction to Trees. 2/21/20062 Tree ADT Tree definition –A tree is a set of nodes which may be empty –If not empty, then there is a distinguished.
CS 210 Iterator Pattern October 31 st, Example to motivate discussion We have two lists (of menu items) one implemented using ArrayList and another.
Example to motivate discussion We have two lists (of menu items) one implemented using ArrayList and another using Arrays. How does one work with these.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
Trees Ellen Walker CPSC 201 Data Structures Hiram College.
Chapter 10 Trees © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
1 CMSC 341 Introduction to Trees Textbook sections:
1 Iterator Pattern (A Behavioral Pattern) Prepared by: Neha Tomar.
(c) University of Washington20-1 CSC 143 Java Trees.
Trees Chapter 15.
Recursive Objects (Part 4)
Section 8.1 Trees.
Iterator and Composite Design Patterns
Chapter 20: Binary Trees.
Chapter 21: Binary Trees.
Jim Fawcett CSE776 – Design Patterns Summer 2003
Abstract Data Structures
Menu item at a restaurant
Chapter 20: Binary Trees.
Binary Tree Iterators Tree Traversals: preorder, inorder, postorder
Presentation transcript:

Fall 2007ACS Ron McFadyen1 Composite Pattern (see pages ) A composite is a group of objects in which some objects contain others; one object may represent groups, and another may represent an individual item, a leaf. The composite class diagram enables a tree structure – a hierarchy. The operations are appropriate for processing and traversing trees.

Fall 2007ACS Ron McFadyen2 Composite Pattern Generic UML class diagram Component operation() Leaf operation() Composite operation() other() * Client Each node of the Component structure can respond to some common operation(s). I.e. the client can send the common operation to Component and the structure responds “appropriately”.

Fall 2007ACS Ron McFadyen3 Composite Pattern Consider the Head First example What is the class diagram? Who is the client? What operations are defined for the component, the composite, and the leaf? How are they carried out? How are the associations implemented in the code?

Fall 2007ACS Ron McFadyen4 Composite Pattern MenuComponent add() remove() getChild() print()… MenuItem print() Menu add() remove() getChild() print()… * Waitress What does it mean to ask a menu, or a menu item, to print itself?

Fall 2007ACS Ron McFadyen5 Composite Pattern Menu menuComponents: ArrayList add() remove() getChild() print()… Note the text implements the association from Menu to MenuComponents with an array list data type. What is the implementation of print() in Menu and in MenuItem

Fall 2007ACS Ron McFadyen6 Composite Pattern In the Menu class we have a print() that is appropriate for an internal node. public void print() { System.out.print("\n" + getName()); System.out.println(", " + getDescription()); System.out.println(" "); Iterator iterator = menuComponents.iterator(); while (iterator.hasNext()) { MenuComponent menuComponent = (MenuComponent)iterator.next(); menuComponent.print(); }

Fall 2007ACS Ron McFadyen7 Composite Pattern In the MenuItem class we have a print() that is appropriate for a leaf: public void print() { System.out.print(" " + getName()); if (isVegetarian()) { System.out.print("(v)"); } System.out.println(", " + getPrice()); System.out.println(" -- " + getDescription()); }

Fall 2007ACS Ron McFadyen8 Composite Pattern We have just viewed the Head First implementation of print() methods appropriate for various nodes. Suppose you wanted nodes/methods to collaborate to count the total number of menu items. Say the waitress asks AllMenus “how many items are there?” How could that be done?

Fall 2007ACS Ron McFadyen9 Composite Pattern A BCD RSTV WZ The tree on the left can be traversed in many ways: Pre-order traversal Visit a node and then its children: ABCRSWZTVD Post-order traversal Visit the children and then the parent: BRWZSTVCDA Level-order traversal Visit nodes on one level and then the next: ABCDRSTVWZ Which traversal is utilized in the Head First example? What tree does the Head First example instantiate?