ICOM 4035 – Data Structures Lecture 3 – Bag ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez ©Manuel.

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

Singly linked lists Doubly linked lists
DATA STRUCTURES USING C++ Chapter 5
Lists Chapter 4 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X.
Data Structures A data structure is a collection of data organized in some fashion that permits access to individual elements stored in the structure This.
ICOM 4035 – Data Structures Lecture 15 – Graph ADT
ICOM 6005 – Database Management Systems Design Dr. Manuel Rodríguez-Martínez Electrical and Computer Engineering Department Lecture 11 – Hash-based Indexing.
ICOM 4035 – Data Structures Lecture 12 – Hashtables & Map ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico,
Stacks, Queues, and Deques. 2 A stack is a last in, first out (LIFO) data structure Items are removed from a stack in the reverse order from the way they.
CS Data Structures II Review COSC 2006 April 14, 2017
ICOM 4035 – Data Structures Lecture 8 – Doubly Linked Lists Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez.
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
© 2004 Goodrich, Tamassia Hash Tables1  
CPSC 388 – Compiler Design and Construction
An Array-Based Implementation of the ADT List public class ListArrayBased implements ListInterface { private static final int MAX_LIST = 50; private Object.
CMPT 225 ADT List using Dynamic arrays A dynamic data structure is one that changes size, as needed, as items are inserted or removed The Java ArrayList.
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 17: Linked Lists.
Stacks, Queues, and Deques
Data Structures Using C++ 2E
16-Aug-15 Air Force Institute of Technology Electrical and Computer Engineering Object-Oriented Programming in Java Topic : Interfaces, Copying/Cloning,
Maps A map is an object that maps keys to values Each key can map to at most one value, and a map cannot contain duplicate keys KeyValue Map Examples Dictionaries:
ICOM 4035 – Data Structures Lecture 2 – ADT and Interfaces Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez.
Chapter 7 More Lists. Chapter 7: More Lists 7.1 – Circular Linked Lists 7.2 – Doubly Linked Lists 7.3 – Linked Lists with Headers and Trailers 7.4 – A.
Implementing Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
ICOM 4035 – Data Structures Lecture 9 – Stack ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez ©Manuel.
ICOM 4035 – Data Structures Lecture 5 – List ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez ©Manuel.
ICOM 4035 – Data Structures Lecture 6 – Big-O Notation Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Marcus Frean.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 6 – September 6 th, 2001.
ICOM 4035 – Data Structures Lecture 13 – Trees ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez ©Manuel.
PRESENTED BY: RAJKRISHNADEEPAK.VUYYURU SWAMYCHANDAN.DONDAPATI VINESHKUMARREDDY.LANKA RAJSEKHARTIRUMALA KANDURI ALAN.
ICOM 4035 – Data Structures Lecture 14 – Binary Search Tree ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico,
ICOM 4035 – Data Structures Lecture 11 – Map ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez ©Manuel.
ICOM 4035 – Data Structures Lecture 1 - Introduction Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez.
Hash Tables1   © 2010 Goodrich, Tamassia.
1 Heaps and Priority Queues Starring: Min Heap Co-Starring: Max Heap.
(c) University of Washington15-1 CSC 143 Java List Implementation via Arrays Reading: 13.
ICOM 4035 – Data Structures Lecture 7 – Linked Lists Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez.
ICOM 4035 – Data Structures Lecture 10 – Queue ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez ©Manuel.
1 Heaps and Priority Queues v2 Starring: Min Heap Co-Starring: Max Heap.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department.
(c) University of Washington16-1 CSC 143 Java Linked Lists Reading: Ch. 20.
(c) University of Washington16-1 CSC 143 Java Lists via Links Reading: Ch. 23.
CSS446 Spring 2014 Nan Wang.  To understand the implementation of linked lists and array lists  To analyze the efficiency of fundamental operations.
COSC 1P03 Data Structures and Abstraction 5.1 Linear Linked Structures A bank is a place where they lend you an umbrella in fair weather and ask for it.
ICOM 4035 – Data Structures Lecture 4 – Set ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez ©Manuel.
CS 261 Fall 2009 Dynamic Array Introduction (aka Vector, ArrayList)
M1G Introduction to Programming 2 3. Creating Classes: Room and Item.
HEAPS. Review: what are the requirements of the abstract data type: priority queue? Quick removal of item with highest priority (highest or lowest key.
IMPLEMENTING ARRAYLIST COMP 103. RECAP  Comparator and Comparable  Brief look at Exceptions TODAY  Abstract Classes - but note that the details are.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 5 – September 4 th, 2001.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Marcus Frean.
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
CSE 501N Fall ‘09 10: Introduction to Collections and Linked Lists 29 September 2009 Nick Leidenfrost.
M180: Data Structures & Algorithms in Java Stacks Arab Open University 1.
1 Chapter 4 Unordered List. 2 Learning Objectives ● Describe the properties of an unordered list. ● Study sequential search and analyze its worst- case.
Amortized Analysis and Heaps Intro David Kauchak cs302 Spring 2013.
Course: Programming II - Abstract Data Types HeapsSlide Number 1 The ADT Heap So far we have seen the following sorting types : 1) Linked List sort by.
CSC 243 – Java Programming, Spring, 2014 Week 4, Interfaces, Derived Classes, and Abstract Classes.
Arrays, Link Lists, and Recursion Chapter 3. Sorting Arrays: Insertion Sort Insertion Sort: Insertion sort is an elementary sorting algorithm that sorts.
LINKED LISTS.
CSC 243 – Java Programming, Fall, 2008 Tuesday, September 30, end of week 5, Interfaces, Derived Classes, and Abstract Classes.
CSC 212 – Data Structures Lecture 28: More Hash and Dictionaries.
Building Java Programs Generics, hashing reading: 18.1.
An Array-Based Implementation of the ADT List
Arrays and Linked Lists
Data Structures & Algorithms
Lecture 3 – Data collection List ADT
Presentation transcript:

ICOM 4035 – Data Structures Lecture 3 – Bag ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez ©Manuel Rodriguez – All rights reserved

Lecture Organization Part I – The role of collection classes and Bags Part II – Design and implementation the Bag ADT Part III – Iterating over a Bag Part IV - Implementing a Dynamic Bag M. Rodriguez-MartinezICOM

Objectives Understand the concept of collections – ADTs used to store data items Understand the design and implementation of the Bag ADT – Unordered collection of items Provide motivating examples ICOM 4035M. Rodriguez-Martinez 3

Companion videos Lecture3 videos – Contains the coding process associated with this lecture – Shows how to build the interfaces, concrete classes, and factory classes mentioned here M. Rodriguez-MartinezICOM

Part I The role of collection classes and Bags M. Rodriguez-MartinezICOM

Collections A Collection is an ADT whose sole purpose is to collect (i.e., store) data items for us Your programs should use collections to keep your object instances around Example: – Program needs to create 100 vectors – Options: Create an array with 100 vectors Use a collection to store 100 vectors M. Rodriguez-MartinezICOM

Benefits of Collections Variable size – you can grow as needed – If I need 101 vectors, I need to recompile  Implementation can be changed – Why use arrays? I will used linked lists … Information hiding – Collection controls access to your items. No need to expose details. Reuse (Do not repeat yourself! DNRY) – You can use the same pattern in other apps M. Rodriguez-MartinezICOM

1 st Collection: Bag ADT Bag: – unordered collection of things – repetitions are allowed – Mathematical term: Multi-set M. Rodriguez-MartinezICOM Bag Bag with candy canes Bag with name tags Joe Ned Amy

Data in Bag Bag can have – fixed number of items (static-size Bag) Fixed at construction time – Variable number of items (dynamic-size Bag) Size can be increased at running time M. Rodriguez-MartinezICOM Static Bag Dynamic Bag

Static vs. Dynamic Bag How to store values in Bag? – We need an array of Object! Store any type of value – Static: Fixed sized array that never grows – Dynamic: Array can grow as needed – These are details for the concrete classes! M. Rodriguez-MartinezICOM Fields elements currentSize Methods Dynamic Bag Fields elements Methods Static Bag currentSize 5 maxSize

Part II Design and implementation the Bag ADT M. Rodriguez-MartinezICOM

Design of Bag ADT M. Rodriguez-MartinezICOM StaticBagDynamicBag > Bag We will do static bag first, then implement dynamic Bag

Operations on the Bag Supported – Add new element – Remove a copy of an element – Remove all copies of an element – Clear Bag – Count copies of element – Test for element membership – Get Bag size – Test if empty – Iterate over all stored values M. Rodriguez-MartinezICOM

Moe Add new element Adding Moe to bag B For static bag, need to check if you have space Dynamic bag grows as needed M. Rodriguez-MartinezICOM Bag with name tags Joe Ned Amy Bag with name tags Joe Ned Amy B.add(“Moe”);

Add new element (2) Key idea is to keep array packed – In use section All elements in range [0,currentSize-1] – Unused (free) section Current size tells us – Current size – Next slot available! You add to next slot and increment currentSize M. Rodriguez-MartinezICOM JoeNedAmy unusedin use currentSize: 3 JoeNedAmyMoe unusedin use currentSize: 4 add Moe

Remove element Remove Ned from bag B – Return true if erased or false if not found Works the same for both static and dynamic Bags Search and replace with last element M. Rodriguez-MartinezICOM Bag with name tags Joe Ned Amy Bag with name tags Joe Amy B.erase(“Ned”);

Remove element (2) First find it! Key idea – overwrite target with last element – decrement currentSize Last element is still there but inaccessible – Valid range: [0, currenSize-1] – Best to make it null Prevent memory leaks M. Rodriguez-MartinezICOM JoeNedAmy unusedin use currentSize: 3 JoeAmy unusedin use currentSize: 2 remove Ned

Ned Remove all copies of element Remove all copies of Ned from bag B Works the same for both static and dynamic Bags Simply loop calling erase until no more copies are left M. Rodriguez-MartinezICOM Bag with name tags Joe Ned Amy Bag with name tags Joe Amy B.eraseAll(“Ned”);

Ned Count element copies Count all copies of Ned in bag B Works the same for both static and dynamic Bags Simply loop accumulating number of times value Ned is seen M. Rodriguez-MartinezICOM Bag with name tags Joe Ned Amy B.count(“Ned”); 3 There are three copies

Count element copies(2) Set up an accumulator value Loop from 0 to currentSize-1 Increment accumulator each time you see target values – Ned in this case M. Rodriguez-MartinezICOM JoeNedAmyNed unused in use currentSize: 5 accumulator = 3

Ned Clear Bag Remove all copies from bag Works the same for both static and dynamic Bags Simply loop thru array nulling everything and then set currentSize to 0 M. Rodriguez-MartinezICOM Bag with name tags Joe Ned Amy Bag with name tags B.clear();

Clear Bag (2) Key idea – Loop setting all values to null This prevent memory leaks – Set current size to zero Memory Leak – Only setting currentSize to zero leaves a bunch of references active – Garbage collector will not be able to reclaim them M. Rodriguez-MartinezICOM JoeNedAmyNed unused in use currentSize: unused currentSize: 0 clear

Easy Bag operations Bag size – just return the value of currentSize Empty Bag test – just determine if bag size is equal to zero Element membership in bag – check is number of copies is greater than 0 Always a good practice to write operations in terms of previous ones – Software reuse (Do Not Repeat Yourself (DNRY)!) M. Rodriguez-MartinezICOM

Part III Iterating over Bag elements M. Rodriguez-MartinezICOM

Iterating over Bag elements We want to iterate over elements inside bag – To use them for whatever reason we need them Can be done by making Bag iterable – Bag interface extends Java Iterable interface – Enables bag to return an iterator How do we do this? – Need to have an inner class that helps you iterate over Bag elements BagIterator class that implements Java Iterator interface M. Rodriguez-MartinezICOM

Bag Iterator BagIterator is inner class to static or dynamic bag Instances of BagIterator have permission to see private fields of Bag – Used to iterate over the array BagIterator just needs an integer as private field – Keeps track of current value being inspected Current Position M. Rodriguez-MartinezICOM Fields elements currentSize Methods currentPosition BagIterator inner class

Part IV Implementing Dynamic Bag M. Rodriguez-MartinezICOM

Apu Tom Jil Dynamic Bag Keeps accepting new elements as long as there is memory. Idea: Grow the array of elements as needed and copy values from old array to new one Only method to change is add() M. Rodriguez-MartinezICOM Ned Bag with name tags Joe Ned Amy Bag with name tags Ned Joe Ned Amy Multiple inserts

Add new element to full Bag Key idea is to crate new array with twice the size Process: – First create new array – Copy all elements from old to new array – Erase old array – Insert new element Now, you have room to grow in the future M. Rodriguez-MartinezICOM JoeNedAmy in use currentSize: 3 JoeNedAmyMoe unusedin use currentSize: 4 add Moe maxSize: 3 maxSize: 6

How to implement Dynamic Bag Option 1: – Copy all methods from static bag, but change add() method Ugly! Option 2: – Use inheritance to make static bag a parent class for dynamic bag and change add() method Better, but you need to make array of elements protected Option 3: – Use object composition and make a static bag instance be a private member of dynamic bag – Map all methods to static bag except for add() which reallocates static bag Best option! M. Rodriguez-MartinezICOM

Summary Collection classes – Provides objects whose role is to store other objects Bag – unordered collection of items – Static bag – sized cannot be changed – Dynamic bag – size can be changed as needed Double the size of previous array Better done via object composition M. Rodriguez-MartinezICOM

Questions? – ICOM 4035M. Rodriguez-Martinez 32