Download Ch13v2.0

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Announcements
• No Tutorial Today
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Sorted Lists
Chapter 13
Slides by Steve Armstrong
LeTourneau University
Longview, TX
2007, Prentice Hall
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
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








Note: a sorted list will not
Add a new entry
let you add or replace an
Remove an entry
entry by position
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
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 SortedListInterface
• Note methods
 add
 remove
 getPosition
 Other methods from Segment 4.10
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
list
• Note
 add method
• If list is in ascending order, insert new entry just
before first entry not smaller than new entry
 Private getNodeBefore
• Need two nodes – node before and node after
where new node goes
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.
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
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 →
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.
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 →
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.
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
add(newEntry)
remove(anEntry)
getPosition(anEntry)
getEntry(givenPosition)
contains(anEntry)
remove(givenPosition)
display()
clear(), getLength(),
isEmpty(), isFull()
Array
Linked
O(n)
O(n)
O(n)
O(1)
O(n)
O(n)
O(n)
O(1)
O(n)
O(n)
O(n)
O(n)
O(n)
O(n)
O(n)
O(1)
Fig. 13-5 The worst-case efficiencies of the operations
on the ADT sorted list for two implementations
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 SortedList
• Note
 Use of generic type
T
 Implements interface
SortedListInterface
 Assume class LList from chapter 6 & 7 is
implementation of interface ListInterface
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.
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.
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
getEntry(givenPosition)
add(newPosition, newEntry)
remove(givenPosition)
contains(anEntry)
display()
clear(),getLength(),isEmpty(),
isFull()
Array
Linked
O(1)
O(n)
O(n)
O(n)
O(n)
O(1)
O(n)
O(n)
O(n)
O(n)
O(n)
O(1)
Fig. 13-8 The worst-case efficiencies of selected ADT list
operations for array-based and linked implementations
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
add(newEntry)
remove(anEntry)
getPosition(anEntry)
getEntry(givenPosition)
contains(anEntry)
remove(givenPosition)
display()
clear(), getLength(),
isEmpty(),isFull()
Array
Linked
O(n)
O(n)
O(n)
O(1)
O(n)
O(n)
O(n)
O(1)
O(n2)
O(n2)
O(n2)
O(n)
O(n)
O(n)
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
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
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Related documents