20
Dictionary Implementati ons Chapter 18 Slides by Steve Armstrong LeTourneau University Longview, TX 2007, Prentice Hall

Dictionary Implementations Chapter 18 Slides by Steve Armstrong LeTourneau University Longview, TX 2007, Prentice Hall

Embed Size (px)

Citation preview

Page 1: Dictionary Implementations Chapter 18 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

Dictionary Implementations

Chapter 18

Slides by Steve ArmstrongLeTourneau University

Longview, TX2007,Prentice Hall

Page 2: Dictionary Implementations Chapter 18 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

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

Page 3: Dictionary Implementations Chapter 18 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

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

Text focuses on first approach

Page 4: Dictionary Implementations Chapter 18 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

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 …

Page 5: Dictionary Implementations Chapter 18 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

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

Page 6: Dictionary Implementations Chapter 18 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

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 ArrayDictionary

Page 7: Dictionary Implementations Chapter 18 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

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()

Click to see method add()

Page 8: Dictionary Implementations Chapter 18 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

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()

Click to see method

remove()

Page 9: Dictionary Implementations Chapter 18 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

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)

Page 10: Dictionary Implementations Chapter 18 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

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.

Page 11: Dictionary Implementations Chapter 18 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

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• Note methods

add locateIndex

Page 12: Dictionary Implementations Chapter 18 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

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)

Page 13: Dictionary Implementations Chapter 18 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

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 Note private inner class KeyIterator

Page 14: Dictionary Implementations Chapter 18 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

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; …

Page 15: Dictionary Implementations Chapter 18 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

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 …

Page 16: Dictionary Implementations Chapter 18 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

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

Page 17: Dictionary Implementations Chapter 18 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

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)

Page 18: Dictionary Implementations Chapter 18 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

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.

Page 19: Dictionary Implementations Chapter 18 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

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• Note private inner class KeyIterator

Page 20: Dictionary Implementations Chapter 18 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

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)