Dictionary Implementations Chapter 18 Slides by Steve Armstrong
Dictionary Implementations Chapter 18 Slides by Steve Armstrong Le. Tourneau University Longview, TX ã 2007, Prentice Hall
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 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 Array. Dictionary 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 arraybased dictionary. 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 Click to see method remove() Fig 18 -3 Removing an entry from an unsorted array-based dictionary 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 Removal Retrieval Traversal O(1) 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 Sorted. Array. Dictionary • Note methods § add § locate. Index 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 Removal Retrieval Traversal O(n) O(log n) 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 … § make. Room § double. Array § is. Array. Full § Counting entries, vector does so for you • View class Sorted. Vector. Dictionary § Note private inner class Key. Iterator 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 desired. Key ≥ node. Key • View class Sorted. Linked. Dictionary • Note private inner class Key. Iterator 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 Removal Retrieval Traversal O(n) Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0 -13 -237045 -X
- Slides: 20