The Dictionary ADT. Implementing a Dictionary with an Ordered Sequence. Implementing a Dictionary with an Unordered Sequence. Dictionary ADT methods:

Dictionary ADT methods: The Dictionary ADT The dictionary ADT models a searchable collection of key-element items findElement(k): if the dictionary ...
Author: Dylan Armstrong
64 downloads 2 Views 240KB Size
Dictionary ADT methods:

The Dictionary ADT The dictionary ADT models a searchable collection of key-element items

findElement(k): if the dictionary has an item with key k, returns its element, else, returns the special element NO_SUCH_KEY

The main operations of a dictionary are searching, inserting, and deleting items

insertItem(k, o): inserts item (k, o) into the dictionary removeElement(k): if the dictionary has an item with key k,

Multiple items with the same key are allowed Applications: „

address book

„

credit card authorization

removes it from the dictionary and returns its element, else returns the special element NO_SUCH_KEY

size(), isEmpty()

„mapping host names (e.g., cs16.net) to internet addresses (e.g., 128.148.34.101) „

keys(), Elements()

English dictionary 1

Implementing a Dictionary with an Unordered Sequence

• searching and removing takes O(n) time • inserting takes O(1) time • applications to log files (frequent insertions, rare searches and removals)

3

findAllElements(k), removeAllElements(k)

2

Implementing a Dictionary with an Ordered Sequence

• searching takes O(log n) time (binary search) • inserting and removing takes O(n) time • application to look-up tables (frequent searches, rare insertions and removals)

4

1

Pseudocode for Binary Search

Binary Search • narrow down the search range in stages • “high-low” game • Example: findElement(7)

0

1

3

4

5

7

l 0

1

0

3

4

5

m

l 0

8

9

11

14

16

18

19

9

11

14

16

18

19

m

1 1

3 3

7

8

h

h 4

5

7

l

m

h

4

5

7

Algorithm BinarySearch(S, k, low, high) if low > high then return NO_SUCH_KEY else mid ← (low+high) / 2 if k = key(mid) then return key(mid) else if k < key(mid) then return BinarySearch(S, k, low, mid-1) else return BinarySearch(S, k, mid+1, high) 2

4

5

7

8

9

12

low

8

9

11

14

16

18

19

8

9

11

14

16

18

19

l=m =h

5

2

14

17

19

22

27

28

33

4

5

7

8

9

12

14

4

5

7

8

9

12

14

37 high

17

19

22

low 2

25

mid

17

25

27

28

33

19

22

25

37 high

mid 27

28

33

37

6

low mid

Running Time of Binary Search • The range of candidate items to be searched is halved

after each comparison

Running Time of Binary Search • binary search runs in O(log n) time for findElement(k) • If we need to find all elements with a given key (findAllElements(k)), it runs in O(log n + s), where s is the number of element of elements in the iterator returned. – Simply do a binary search to find an element equal to k. Then step back through the array until you reach the first element equal to k. Finally, step forward through the area adding each element to the iterator until you reach the first element that is not equal to k. This takes O(logn) time for the search and then at most s time to search back to the beginning of the run of k’s and s time return all of the elements k. Therefore we have a solution running in at most O(logn+s) time.

In the array-based implementation, access by rank takes 7 O(1) time, thus binary search runs in O(log n) time

8

2

Binary Search Tree

New Insertion Sort

Searching Cost of Searching Insertion Deletion

What is the running time of the insertion sort, using a sequence implemented with an array? If we use a binary search to do the insertions?

< 2

6 9

> 4 =

1

9

8

10

Binary Search Trees

Gregor

• A binary search tree is a binary tree T such that

– each internal node stores an item (k, e) of a dictionary. – keys stored at nodes in the left subtree of v are less than or equal to k. – keys stored at nodes in the right subtree of v are greater than or equal to k. – external nodes do not hold elements but serve as place holders.

Fabio

Bob

11

Nicole

Frank

12

3

Operations

10 3 1

17 8

5

15

Searching: findElement(k):

20

Inserting: insertItem(k, o): Removing: removeElement(k):

9

Question: How can we traverse the tree so that we visit the elements in increasing key order?

13

Search

14

Search Example I

• To search for a key k, Algorithm findElement(k, v) we trace a downward if T.isExternal (v) path starting at the return NO_SUCH_KEY root if k < key(v) • The next node visited return findElement(k, T.leftChild(v)) depends on the else if k = key(v) outcome of the return element(v) comparison of k with else { k > key(v) } the key of the current return findElement(k, T.rightChild(v)) node 6 • If we reach a leaf, the < key is not found and 2 9 we return > NO_SUCH_KEY 8 1 4 = • Example: findElement(4) 15

Successful findElement(76) 76>44 7665 76 key(v)} return findAllElements(k,T,rightChild(v)) Note that after finding k, if it occurs again, it will be in the left most 17 internal node of the right subtree.

Unsuccessful findElement(25) 2517 25