Bag Implementations that Use Arrays Chapter 2 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions.

Slides:



Advertisements
Similar presentations
List Implementations That Use Arrays
Advertisements

Lists Chapter 4 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X.
The Efficiency of Algorithms Chapter 4 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.
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.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 14 – Student Grades Application: Introducing.
List Implementations That Use Arrays Chapter 5 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Announcements.
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.
Queue, Deque, and Priority Queue Implementations Chapter 24 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
© 2006 Pearson Addison-Wesley. All rights reserved5 A-1 Chapter 5 Linked Lists.
Stack Implementations Chapter 22 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Chapter 3 Array-Based Implementations CS Data Structures Mehmet H Gunes Modified from authors’ slides.
Bag Implementations that Use Arrays Chapter 2 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
A Binary Search Tree Implementation Chapter 27 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Bags Chapter 1 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.
Data Structures and Abstractions with Java, 4e Frank Carrano
Java Classes Appendix C © 2015 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Data Structures and Abstractions with Java, 4e Frank Carrano
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
An Introduction to Sorting Chapter 8 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with.
Documentation and Programming Style Appendix A © 2015 Pearson Education, Inc., Hoboken, NJ. 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.
Searching Chapter 18 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank.
Designing Classes Prelude © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank.
Lists Chapter 4 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Chapter 5 Array-Based Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Linked Bag Chapter 3.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
© 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.
Creating Classes from Other Classes Appendix D © 2015 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Chapter 5 Array-Based Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Tree Implementations Chapter 24 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java,
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
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.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Stacks Chapter 5 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank Carrano.
A Binary Search Tree Implementation Chapter 25 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions.
Link-Based Implementations
Bag Implementations that Use Arrays
A Bag Implementation that Links Data
A Bag Implementation that Links Data
Chapter 4 Link Based Implementations
Chapter 3 Array-Based Implementations
List Implementations Chapter 9
Adapted from Pearson Education, Inc.
Array-Based Implementations
List Implementations that Use Arrays
A List Implementation That Links Data
Stack Implementations
Array-Based Implementations
A Heap Implementation Chapter 26 Adapted from Pearson Education, Inc.
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 List Implementation That Links Data
List Implementations that Use Arrays
Stack Implementations
A List Implementation that Uses An Array
A List Implementation that Links Data
Queue, Deque, and Priority Queue Implementations
Presentation transcript:

Bag Implementations that Use Arrays Chapter 2 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank Carrano

Fixed-Size Array to Implement the ADT Bag FIGURE 2-1 A classroom that contains desks in fixed positions © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Fixed-Size Array FIGURE 2-2 UML notation for the class ArrayBag, including the class’s data fields © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Fixed-Size Array LISTING 2-1 An outline of the class ArrayBag © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Fixed-Size Array LISTING 2-1 An outline of the class ArrayBag © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Fixed-Size Array LISTING 2-1 An outline of the class ArrayBag © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Fixed-Size Array FIGURE 2-3 Adding entries to an array that represents a bag, whose capacity is six, until it becomes full © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Fixed-Size Array FIGURE 2-3 Adding entries to an array that represents a bag, whose capacity is six, until it becomes full © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Fixed-Size Array Method add © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Fixed-Size Array Method isFull © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Fixed-Size Array Method toArray © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Making the Implementation Secure Practice fail-safe programming by including checks for anticipated errors Validate input data and arguments to a method refine incomplete implementation of ArrayBag to make code more secure by adding the following two data fields © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Making the Implementation Secure Revised constructor © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Making the Implementation Secure Method to check initialization © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Making the Implementation Secure Revise the method add © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Testing the Core Methods Stubs for remove and clear © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Testing the Core Methods LISTING 2-2 A program that tests core methods of the class ArrayBag © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Testing the Core Methods LISTING 2-2 A program that tests core methods of the class ArrayBag © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Testing the Core Methods LISTING 2-2 A program that tests core methods of the class ArrayBag © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Testing the Core Methods LISTING 2-2 A program that tests core methods of the class ArrayBag © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Implementing More Methods Methods isEmpty and getCurrentSize © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Implementing More Methods Method getFrequencyOf © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Implementing More Methods Method contains © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Methods That Remove Entries The method clear © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Methods That Remove Entries The method remove © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Methods That Remove Entries FIGURE 2-4 The array bag after a successful search for the string “Nancy" © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Methods That Remove Entries FIGURE 2-5 (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 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Methods That Remove Entries FIGURE 2-6 Avoiding a gap in the array while removing an entry © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Methods That Remove Entries New definition of remove © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Methods That Remove Entries The second remove method © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Methods That Remove Entries The removeEntry method © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Methods That Remove Entries Definition for the method getIndexOf © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Methods That Remove Entries Revised definition for the method contains © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Using Array Resizing FIGURE 2-7 Resizing an array copies its contents to a larger second array © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Using Array Resizing FIGURE 2-8 (a) An array; (b) two references to the same array; (c) the original array variable now references a new, larger array; © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Using Array Resizing FIGURE 2-8 (d) the entries in the original array are copied to the new array; (e) the original array is discarded © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Using Array Resizing FIGURE 2-9 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 © 2015 Pearson Education, Inc., Upper Sa ddle River, NJ. All rights reserved.

New Implementation of a Bag Previous definition of method add © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

New Implementation of a Bag Revision of method doubleCapacity © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Pros and Cons of Using an Array Adding an entry to the bag is fast Removing an unspecified entry is fast Removing a particular entry requires time to locate the entry Increasing the size of the array requires time to copy its entries © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

End Chapter 2 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.