Bag Implementations that Use Arrays

Slides:



Advertisements
Similar presentations
Lists Chapter 4 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X.
Advertisements

Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
A Bag Implementation that Links Data Chapter 3 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
A List Implementation That Links Data Chapter 6 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Completing the Linked Implementation of a List Chapter 7 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
List Implementations That Use Arrays Chapter 5 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
List Implementations That Use Arrays Chapter 5. 2 Chapter Contents Using a Fixed-Size Array to Implement the ADT List An Analogy The Java Implementation.
CHAPTER 6 Stacks Array Implementation. 2 Stacks A stack is a linear collection whose elements are added and removed from one end The last element to be.
© 2006 Pearson Addison-Wesley. All rights reserved5 A-1 Chapter 5 Linked Lists.
Chapter 3 Array-Based Implementations CS Data Structures Mehmet H Gunes Modified from authors’ slides.
A Binary Search Tree Implementation Chapter 25 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Bag Implementations that Use Arrays Chapter 2 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 4: Linked Lists Data Abstraction & Problem Solving with.
Bags Chapter 1 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Lab 7 Queue ADT. OVERVIEW The queue is one example of a constrained linear data structure. The elements in a queue are ordered from least recently added.
Bag Implementations that Use Arrays Chapter 2 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions.
Stack Implementations Chapter 6 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
© 2006 Pearson Addison-Wesley. All rights reserved5 B-1 Chapter 5 (continued) Linked Lists.
A Heap Implementation Chapter 26 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Array-Based Implementations Chapter 3 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
A Bag Implementation that Links Data Chapter 3 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 18: Stacks and Queues.
An Array-Based Implementation of the ADT List
Link-Based Implementations
18 Chapter Stacks and Queues
Chapter 5 Linked Lists © 2006 Pearson Addison-Wesley. All rights reserved.
Linked Lists Chapter 5 (continued)
Linked Lists Chapter 6 Section 6.4 – 6.6
Chapter 5 Linked Lists © 2011 Pearson Addison-Wesley. All rights reserved.
Pointers, Polymorphism, and Memory Allocation
Chapter 4 Linked Lists.
A Bag Implementation that Links Data
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Queue, Deque, and Priority Queue Implementations
A Bag Implementation that Links Data
Array Lists Chapter 6 Section 6.1 to 6.3
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Chapter 4 Link Based Implementations
Chapter 3 Introduction to Classes, Objects Methods and Strings
Chapter 12 Sorted Lists and their Implementations
Chapter 3 Array-Based Implementations
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Hashing as a Dictionary Implementation
List Implementations Chapter 9
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Adapted from Pearson Education, Inc.
Chapter 5 Linked Lists © 2006 Pearson Addison-Wesley. All rights reserved.
Dynamic Data Structures and Generics
Array-Based Implementations
List Implementations that Use Arrays
A List Implementation That Links Data
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Array-Based Implementations
A Heap Implementation Chapter 26 Adapted from Pearson Education, Inc.
Linked Lists Chapter 5 (continued)
Chapter 5 Linked Lists © 2006 Pearson Addison-Wesley. All rights reserved.
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Chapter 5 Linked Lists © 2011 Pearson Addison-Wesley. All rights reserved.
Bag Implementations that Use Arrays
A Binary Search Tree Implementation
Dynamic Array: Implementation of Stack
Linked Lists Chapter 5 (continued)
A List Implementation That Links Data
Copyright ©2012 by Pearson Education, Inc. All rights reserved
List Implementations that Use Arrays
A List Implementation that Uses An Array
A List Implementation that Links Data
Presentation transcript:

Bag Implementations that Use Arrays Chapter 2 Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Contents Using a Fixed-Size Array to Implement the ADT Bag An Analogy A Group of Core Methods Implementing the Core Methods Testing the Core Methods Implementing More Methods Methods That Remove Entries Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Contents Using Array Resizing to Implement the ADT Bag Resizing an Array A New Implementation of a Bag The Pros and Cons of Using an Array to Implement the ADT Bag Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Objectives Implement ADT bag using a fixed-size array or an array that expanded dynamically Discuss advantages and disadvantages of implementations presented Copyright ©2012 by Pearson Education, Inc. All rights reserved

Implement with Fixed Size Array Define methods specified by previous interface BagInterface Consider use of fixed size array Not unlike a classroom with exactly 40 desks Numbered from 0 to 39 Copyright ©2012 by Pearson Education, Inc. All rights reserved

Adding, Removing Students Arbitrarily specify consecutively numbered desks be occupied When desk #39 occupied, room is full Removing What to do when someone in middle of sequence is removed? Move last person there or shift everyone? Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 2-1 A classroom that contains desks in fixed positions Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Figure 2-2 UML notation for the class ArrayBag, including the class’s data fields Copyright ©2012 by Pearson Education, Inc. All rights reserved

Group of Core Methods Do not attempt to define entire class, then test Instead, identify group of core methods Define Test Then finish rest of class Note outline, Listing 2-1 Note: Code listing files must be in same folder as PowerPoint files for links to work Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Design Decisions When array bag is partially full Which array elements should contain entries? Options Start at element 0 or element 1 ? Require elements to be sequential? Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Figure 2-3 Adding entries to an array that represents a bag, whose capacity is six, until it becomes full Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Add Method Note: entries in no order Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 2-4 An array of objects contains references to those objects Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Method isFull Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Method toArray Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Design Decisions Should toArray return the array bag or a copy? Best to return a copy … think about why. Temporarily make stub methods for testing at this stage View test program, Listing 2-2 Output Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved More Methods Methods isEmpty and getCurrentSize Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved More Methods Method getFrequencyOf Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved More Methods Method contains Copyright ©2012 by Pearson Education, Inc. All rights reserved

Methods That Remove Entries Method clear Remove all entries Remove last entry in bag How could this be more efficient? Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Figure 2-5 The array bag after a successful search for the string "Alice" Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Figure 2-6 (a) A gap in the array bag after setting the entry in bag[index] to null; (b) the array after shifting subsequent entries to avoid a gap Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 2-7 Avoiding a gap in the array while removing an entry Copyright ©2012 by Pearson Education, Inc. All rights reserved

Methods That Remove Entries Method to remove a specified entry Assumes presence of private getIndexOf method Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Resizing Array Need to accommodate more elements than originally specified for bag Figure 2-8 Resizing an array copies its contents to a larger second array Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 2-9 (a) An array; (b) two references to the same array; Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Figure 2-9 (a) An array; (b) two references to the same array; (c) the original array variable now references a new, larger array; Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Figure 2-9 (d) the entries in the original array are copied to the new array; (e) the original array is discarded Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved FIGURE 2-10 The effect of the statement myArray = Arrays.copyOf (myArray, 2 * myArray.length); (a) The argument array; (b) the parameter that references the argument array; (c) a new, larger array that gets the contents of the argument array; (d) the return value that references the new array; (e) the argument variable is assigned the return value Copyright ©2012 by Pearson Education, Inc. All rights reserved

A New Implementation of a Bag Change name of class to ResizableArrayBag, distinguish between implementations. Remove modifier final from declaration of array bag to enable resizing. Change the name of constant DEFAULT_CAPACITY to DEFAULT_INITIAL_CAPACITY. Copyright ©2012 by Pearson Education, Inc. All rights reserved

A New Implementation of a Bag Although unnecessary, change clarifies new purpose of constant, Bag’s capacity will increase as necessary. Make same change in default constructor Change names of constructors to match new class name. Copyright ©2012 by Pearson Education, Inc. All rights reserved

A New Implementation of a Bag Revise definition of method add to always accommodate new entry. Method will never return false. Revise definition of method isFull to always return false. A bag will never become full. Copyright ©2012 by Pearson Education, Inc. All rights reserved

A New Implementation of a Bag New add method Assumes method ensureCapacity Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved End Chapter 2 Copyright ©2012 by Pearson Education, Inc. All rights reserved