Presentation is loading. Please wait.

Presentation is loading. Please wait.

Dictionary Implementations Chapter 18. 2 Chapter Contents Array-Based Implementations The Entries An Unsorted Array-Based Dictionary A Sorted Array-Based.

Similar presentations


Presentation on theme: "Dictionary Implementations Chapter 18. 2 Chapter Contents Array-Based Implementations The Entries An Unsorted Array-Based Dictionary A Sorted Array-Based."— Presentation transcript:

1 Dictionary Implementations Chapter 18

2 2 Chapter Contents Array-Based Implementations The Entries An Unsorted Array-Based Dictionary A Sorted Array-Based Dictionary Vector-Based Implementation Linked Implementations The Entries An Unsorted Linked Dictionary A Sorted Linked Dictionary

3 3 Array Based Implementations Each entry consists of two parts A search key A value Strategies Encapsulate the two parts into an object Use two parallel arrays Use a two dimensional array Text focuses on first approach

4 4 Array-Based Implementations Fig. 18-1 3 ways to use arrays to represent dictionary entries: (a) an array of entry objects; (b) parallel arrays of search keys and values; (c) 2-dimensional array of search keys and values

5 5 The Entries A class to represent the two-part entries private class Entry implements java.io.Serializable {private Object key;private Object value; private Entry(Object searchKey, Object dataValue) {key = searchKey; value = dataValue; } // end constructor private Object getKey() {return key;} // end getKey private Object getValue()} {return value;} // end getValue private void setValue(Object dataValue) {value = dataValue;} // end setValue } // end Entry

6 6 Unsorted Array-Based Dictionary Fig. 18-2 Unsorted, array-based dictionary: (a) adding an entry; (b) removing an entry.

7 7 Array-Based Implementations Unsorted worst-case efficiencies Addition O(1) Removal O(n) Retrieval O(n) Traversal O(n) Sorted worst-case efficiencies Addition O(n) Removal O(n) Retrieval O(log n) Traversal O(n)

8 8 Sorted Array-Based Dictionary Fig. 18-3 Adding an entry to a sorted array-based dictionary: (a) search; (b) make room; (c) insert.

9 9 Sorted Array-Based Dictionary Beginning of the class public class SortedArrayDictionary implements DictionaryInterface, java.io.Serializable {private Entry [] entries;// array of sorted entries private int currentSize = 0; // number of entries private final static int DEFAULT_MAX_SIZE = 25; public SortedArrayDictionary() {entries = new Entry[DEFAULT_MAX_SIZE]; currentSize = 0; } // end default constructor public SortedArrayDictionary(int maxSize) {entries = new Entry[maxSize]; currentSize = 0; } // end constructor...

10 10 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 Downside Requires some involved casts

11 11 Vector-Based Implementations Beginning of the class public class SortedVectorDictionary implements DictionaryInterface, java.io.Serializable {private Vector entries; public SortedVectorDictionary() {entries = new Vector();// as needed, vector doubles its size } // end default constructor public SortedVectorDictionary(int maxSize) {entries = new Vector(maxSize); } // end constructor...

12 12 Linked Implementations Fig. 18-4 a) Could use a chain of nodes that each reference an entry object

13 13 Linked Implementations Fig. 18-4 b) Use parallel chains of search keys and values figure 18-4 a & b

14 14 Linked Implementations Fig. 18-4 c) Use a chain of nodes that each reference a search key and value figure 18-4 c

15 15 Linked Implementations Unsorted worst-case efficiencies Addition O(1) Removal O(n) Retrieval O(n) Traversal O(n) Sorted worst-case efficiencies Addition O(n) Removal O(n) Retrieval O(n) Traversal O(n)

16 16 Linked Implementations Fig. 18-5 Adding to an unsorted linked dictionary.


Download ppt "Dictionary Implementations Chapter 18. 2 Chapter Contents Array-Based Implementations The Entries An Unsorted Array-Based Dictionary A Sorted Array-Based."

Similar presentations


Ads by Google