Presentation is loading. Please wait.

Presentation is loading. Please wait.

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X Announcements.

Similar presentations


Presentation on theme: "Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X Announcements."— Presentation transcript:

1 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X Announcements No Tutorial Today

2 Sorted Lists Chapter 13 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

3 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X Chapter Contents Specifications for the ADT Sorted List  Using the ADT Sorted List A Linked Implementation  The Method add  The Efficiency of the Linked Implementation An Implementation that Uses the ADT List  Efficiency Issues

4 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X Specifications for the ADT Sorted List Data  A collection of objects in sorted order, same data type  The number of objects in the collection Operations  Add a new entry  Remove an entry  Get the position of the entry  Check if a certain value is contained in the list  Clear the list  Return the length of the list  Check if list is empty or full  Display the list Note: a sorted list will not let you add or replace an entry by position

5 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X Specifications for the ADT Sorted List View SortedListInterfaceSortedListInterface Note methods  add  remove  getPosition  Other methods from Segment 4.10methods from Segment 4.10

6 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X A Linked Implementation View linked implementation of ADT sorted listlinked implementation Note  add method add If list is in ascending order, insert new entry just before first entry not smaller than new entry  Private getNodeBefore getNodeBefore Need two nodes – node before and node after where new node goes

7 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X The Method add Fig. 13-1 Insertion points of names into a sorted chain of linked nodes.

8 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X The Method add Fig. 13-2 Recursively adding Luke to a sorted chain of names

9 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X The Method add Fig. 13-3 Recursively adding a node at the beginning of the chain … continued →

10 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X The Method add Fig. 13-3 (ctd) Recursively adding a node at the beginning of the chain.

11 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X The Method add Fig. 13-4 Recursively adding a node between existing nodes in a chain … continued →

12 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X The Method add Fig. 13-4 (ctd) Recursively adding a node between existing nodes in a chain.

13 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X Efficiency of the Linked Implementation ADT Sorted List Operation Array Linked add(newEntry) remove(anEntry) getPosition(anEntry) getEntry(givenPosition) contains(anEntry) remove(givenPosition) display() clear(), getLength(), isEmpty(), isFull() O(n) O(1) O(n) O(1) O(n) O(1) Fig. 13-5 The worst-case efficiencies of the operations on the ADT sorted list for two implementations

14 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X An Implementation That Uses the ADT List View class SortedListclass SortedList Note  Use of generic type T  Implements interface SortedListInterface  Assume class LList from chapter 6 & 7 is implementation of interface ListInterface

15 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X An Implementation That Uses the ADT List Fig. 13-6 An instance of a sorted list that contains a list of its entries.

16 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X An Implementation That Uses the ADT List Fig. 13-7 A sorted list in which Jamie belongs after Carlos but before Sarah.

17 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X Efficiency Issues ADT List Operation Array Linked getEntry(givenPosition) add(newPosition, newEntry) remove(givenPosition) contains(anEntry) display() clear(),getLength(),isEmpty(), isFull() O(1) O(n) O(1) O(n) O(1) Fig. 13-8 The worst-case efficiencies of selected ADT list operations for array-based and linked implementations

18 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X Efficiency Issues ADT Sorted List Operation Array Linked add(newEntry) remove(anEntry) getPosition(anEntry) getEntry(givenPosition) contains(anEntry) remove(givenPosition) display() clear(), getLength(), isEmpty(),isFull() O(n) O(1) O(n) O(1) O(n 2 ) O(n) O(1) Fig. 13-9 The worst-case efficiencies of the ADT sorted list operations when implemented using an instance of the ADT LIST

19 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X Efficiency Issues When you use an instance of an ADT list to represent entries in ADT sorted list  Must use the list's operations to access sorted lists entries  Do not access them directly Direct access leads to inefficient implementation of sorted list  Underlying list uses a chain of linked nodes to store entries


Download ppt "Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X Announcements."

Similar presentations


Ads by Google