Download presentation
Presentation is loading. Please wait.
Published byGerald Henderson Modified over 9 years ago
1
Dictionary Implementations Chapter 18 Slides by Steve Armstrong LeTourneau University Longview, TX 2007, Prentice Hall
2
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
3
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
4
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 …
5
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
6
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 ArrayDictionarysource code
7
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()
8
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()
9
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)
10
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.
11
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 SortedArrayDictionary Note methods add add locateIndex locateIndex
12
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)
13
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 SortedVectorDictionary Note private inner class KeyIteratorKeyIterator
14
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; …
15
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 …
16
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
17
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)
18
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.
19
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 SortedLinkedDictionary Note private inner class KeyIterator KeyIterator
20
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)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.