Dictionaries
This presentation is the property of its rightful owner.
Sponsored Links
1 / 24

Dictionaries PowerPoint PPT Presentation


  • 85 Views
  • Uploaded on
  • Presentation posted in: General

Dictionaries. Chapter 12. Chapter Contents. Specifications for the ADT Dictionary Entries and methods Using the ADT Dictionary English Dictionary Telephone Directory Address Book Java Class Library: the Interface Map. Specifications for the ADT Dictionary.

Download Presentation

Dictionaries

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Dictionaries

Dictionaries

Chapter 12


Chapter contents

Chapter Contents

  • Specifications for the ADT Dictionary

    • Entries and methods

  • Using the ADT Dictionary

    • English Dictionary

    • Telephone Directory

    • Address Book

  • Java Class Library: the Interface Map


Specifications for the adt dictionary

Specifications for the ADT Dictionary

  • Contains entries that each have two parts

    • A key word or search key

    • A value associated with the key

Dictionary organizes and identifies its entries by their search keys, rather than by position like List.

Key word enables you to locate the value.

Dictionary is also called map, table.

An English dictionary.


Specifications for the adt dictionary1

Specifications for the ADT Dictionary

  • Data

    • Pairs of objects (key, value)

    • Number of pairs in the collection

  • Operations

  • add

  • remove

  • getValue

  • contains

  • getKeyIterator

  • isEmpty

  • isFull

  • getSize

  • clear


Using the adt dictionary

Using the ADT Dictionary

  • A directory of telephone numbers

  • What is search key and corresponding value?

A class diagram for a telephone directory.


Java class library the interface map

Java Class Library: The Interface Map

  • A list of method signatures in Map

    • Note similarity to methods developed in this chapter

public Object put(Object key, Object value);public Object remove(Object key);public Object get(Object key);public boolean containsKey(Object key);public boolean containsValue(Object value);public Set keySet();public Collection values();public boolean isEmpty();public int size();public void clear();


Dictionary implementations

Dictionary Implementations


Chapter contents1

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


Array based implementations

Text focuses on first approach

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


Array based implementations1

Array-Based Implementations

  • 3 ways to use arrays to represent dictionary entries:

  • an array of entry objects;

  • parallel arrays of search keys and values;

  • 2-dimensional array of search keys and values


The entries

The Entries

  • A private 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 constructorprivate Object getKey(){return key;} // end getKeyprivate Object getValue()}{return value;} // end getValueprivate void setValue(Object dataValue){value = dataValue;} // end setValue} // end Entry


Unsorted array based dictionary

Unsorted Array-Based Dictionary

  • Unsorted, array-based dictionary:

  • adding an entry;

  • removing an entry.

Shifting of elements

Q: Alternative way for removing without shifting?


Sorted array based dictionary

Sorted Array-Based Dictionary

Adding an entry to a sorted array-based dictionary: (a) search; (b) make room; (c) insert.


Sorted array based dictionary1

Sorted Array-Based Dictionary

  • Beginning of the class

public class SortedArrayDictionary implements DictionaryInterface, java.io.Serializable{private Entry [] entries;// array of sorted entriesprivate int currentSize = 0; // number of entriesprivate final static int DEFAULT_MAX_SIZE = 25;public SortedArrayDictionary(){entries = new Entry[DEFAULT_MAX_SIZE];currentSize = 0;} // end default constructorpublic SortedArrayDictionary(int maxSize){entries = new Entry[maxSize];currentSize = 0;} // end constructor

. . .


Array based implementations2

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)


Vector based implementations

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


Vector based implementations1

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 constructorpublic SortedVectorDictionary(int maxSize){entries = new Vector(maxSize);} // end constructor. . .


Linked implementations

Linked Implementations

a) Store entries in a chain of linked nodes that each reference an entry object


Linked implementations1

Linked Implementations

b) Use parallel chains of search keys and values

figure 18-4 a & b


Linked implementations2

Linked Implementations

c) Use a chain of nodes that each reference a search key and value

figure 18-4 c


Adding to an unsorted linked dictionary

Adding to an unsorted linked dictionary.

Linked node implementation


Adding entry to a sorted linked dictionary

Adding entry to a sorted linked dictionary

  • Algorithm add (key, value)

    • Allocate a new node containing key and value

    • If ( chain is empty of new node belongs at the beginning)

      • Add new node to beginning of chain; increment length of dictionary

    • else

      • Search chain until either you find a node containing key or you pass the point where it should be

    • If ( key is already in dictionary)

    • replace key’s associated value with new value.

    • else

      • Insert new node before the last node that was examined during the search; increment the length of dictionary


Adding entry to a sorted linked dictionary1

Adding entry to a sorted linked dictionary


Linked implementations3

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)


  • Login