Upload
gerald-henderson
View
218
Download
0
Tags:
Embed Size (px)
Citation preview
Dictionary Implementations
Chapter 18
Slides by Steve ArmstrongLeTourneau University
Longview, TX2007,Prentice Hall
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Chapter Contents
• Array-Based Implementations An Unsorted Array-Based Dictionary A Sorted Array-Based Dictionary
• Vector-Based Implementation
• Linked Implementations An Unsorted Linked Dictionary A Sorted Linked Dictionary
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Array Based Implementations 1
• Each entry consists of two parts A search key A value
• Strategies Encapsulate the two parts into an object Use two parallel arrays
Text focuses on first approach
Text focuses on first approach
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Array-Based Implementations
Fig. 18-1 Two possible ways to use arrays to represent dictionary
entries: (a) an array of objects that encapsulates
each search key and corresponding value …
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Array-Based Implementations
Fig. 18-1 Two possible ways to use arrays to represent dictionary entries: (b) parallel
arrays of search keys and values
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Unsorted Array-Based Dictionary
• Our implementation uses one array Each element in dictionary (the array) is an
instance of class Entry Entry will be private and internal to the
dictionary class
• Outer class stated in terms of type parameters K and V Data types of the search keys and the values
• View source code of ArrayDictionary
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Unsorted Array-Based Dictionary 5
Fig. 18-2 Adding a new entry to an unsorted array-based dictionary.
Click to see method add()
Click to see method add()
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Unsorted Array-Based Dictionary 6
Fig 18-3 Removing an entry from an unsorted array-based dictionary
Click to see method
remove()
Click to see method
remove()
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Unsorted Array-Based Implementations 8
• Unsorted worst-case efficiencies Addition O(1) Removal O(n) Retrieval O(n) Traversal O(n)
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Sorted Array-Based Dictionary 9
Fig. 18-3 Adding an entry to a sorted
array-based dictionary: (a) search;
(b) make room; (c) insert.
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Sorted Array-Based Dictionary 11
• Some of implementation same as unsorted dictionary
• Differences Search key must be from class that
implements Comparable
• View class SortedArrayDictionary• Note methods
add locateIndex
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Sorted Array-Based Implementations 15
• Sorted worst-case efficiencies Addition O(n) Removal O(n) Retrieval O(log n) Traversal O(n)
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Vector-Based Implementations
• Similar in spirit to the array-based version
• With vector no need for … makeRoom doubleArray isArrayFull
Counting entries, vector does so for you
• View class SortedVectorDictionary Note private inner class KeyIterator
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Linked Implementations 22
Fig. 18-6 Three possible ways to use linked nodes to represent the entries of a dictionary: (a) a chain of nodes
that each reference an entry object; …
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Linked Implementations
Fig. 18-6 Three possible ways to use linked nodes to represent the entries of a dictionary: (b) parallel chains of
search keys and values …
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Linked Implementations
Fig. 18-6 Three possible ways to use linked nodes to represent the entries of a dictionary: (c) a chain of
nodes that each reference a search key and a value
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Unsorted Linked Implementations 23
• Unsorted worst-case efficiencies Addition O(1) Removal O(n) Retrieval O(n) Traversal O(n)
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Linked Implementations
Fig. 18-7 Adding to an unsorted linked dictionary.
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
A Sorted Linked Dictionary 25
• When adding nodes Requires sequential search from beginning Need only search until desiredKey ≥ nodeKey
• View class SortedLinkedDictionary• Note private inner class KeyIterator
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Sorted Linked Implementations 27
• Sorted worst-case efficiencies Addition O(n) Removal O(n) Retrieval O(n) Traversal O(n)