Dictionary Implementations Chapter 18 Chapter Contents ArrayBased Implementations
Dictionary Implementations Chapter 18
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 2
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 3
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 4
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 search. Key, Object data. Value) { key = search. Key; value = data. Value; } // end constructor private Object get. Key() { return key; } // end get. Key private Object get. Value()} { return value; } // end get. Value private void set. Value(Object data. Value) { value = data. Value; } // end set. Value } // end Entry 5
Unsorted Array-Based Dictionary Fig. 18 -2 Unsorted, array-based dictionary: (a) adding an entry; (b) removing an entry. 6
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) 7
Sorted Array-Based Dictionary Fig. 18 -3 Adding an entry to a sorted array-based dictionary: (a) search; (b) make room; (c) insert. 8
Sorted Array-Based Dictionary Beginning of the class public class Sorted. Array. Dictionary implements Dictionary. Interface, java. io. Serializable { private Entry [] entries; // array of sorted entries private int current. Size = 0; // number of entries private final static int DEFAULT_MAX_SIZE = 25; public Sorted. Array. Dictionary() { entries = new Entry[DEFAULT_MAX_SIZE]; current. Size = 0; } // end default constructor public Sorted. Array. Dictionary(int max. Size) { entries = new Entry[max. Size]; current. Size = 0; } // end constructor. . . 9
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 Downside • Requires some involved casts 10
Vector-Based Implementations Beginning of the class public class Sorted. Vector. Dictionary implements Dictionary. Interface, java. io. Serializable { private Vector entries; public Sorted. Vector. Dictionary() { entries = new Vector(); // as needed, vector doubles its size } // end default constructor public Sorted. Vector. Dictionary(int max. Size) { entries = new Vector(max. Size); } // end constructor. . . 11
Linked Implementations Fig. 18 -4 a) Could use a chain of nodes that each reference an entry object 12
Linked Implementations figure 18 -4 a & b Fig. 18 -4 b) Use parallel chains of search keys and values 13
Linked Implementations figure 18 -4 c Fig. 18 -4 c) Use a chain of nodes that each reference a search key and value 14
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) 15
Linked Implementations Fig. 18 -5 Adding to an unsorted linked dictionary. 16
- Slides: 16