Presentation is loading. Please wait.

Presentation is loading. Please wait.

Dictionaries CS 110: Data Structures and Algorithms First Semester, 2010-2011.

Similar presentations


Presentation on theme: "Dictionaries CS 110: Data Structures and Algorithms First Semester, 2010-2011."— Presentation transcript:

1 Dictionaries CS 110: Data Structures and Algorithms First Semester, 2010-2011

2 Definition ► The Dictionary Data Structure ► structure that facilitates searching ► objects are stored with search keys; insertion of an object must include a key ► searching requires a key and returns the key- object pair ► removal also requires a key ► Need an Entry interface/class ► Entry encapsulates the key-object pair (just like with priority queues)

3 Sample Applications ► An actual dictionary ► key: word ► object: word record (definition, pronunciation, etc.) ► Record keeping applications ► Bank account records (key: account number, object: holder and bank account info) ► Student records (key: id number, object: student info)

4 Dictionary Interface public interface Dictionary { public int size(); public boolean isEmpty(); public Entry insert( int key, Object value ) throws DuplicateKeyException; public Entry find( int key ); // return null if not found public Entry remove( int key ) // return null if not found; }

5 Dictionary Details/Variations ► Key types ► For simplicity, we assume that the keys are ints ► But the keys can be any kind of object as long as they can be ordered (e.g., string and alphabetical ordering) ► Duplicate entries (entries with the same key) may be allowed ► Our textbook calls the data structure that does not allows duplicates a Map, while a Dictionary allows duplicates ► For purposes of this discussion, we assume that dictionaries do not allow duplicates

6 Dictionary Implementations ► Unordered list ► Ordered table ► Binary search tree

7 Unordered List ► Strategy: store the entries in the order that they arrive ► O( 1 ) insert operation ► Can use an array, ArrayList, or linked list ► Find operation requires scanning the list until a matching key value is found ► Scanning implies an O( n ) operation ► Remove operation similar to find operation ► Entries need to be adjusted if using array/ArrayList ► O( n ) operation

8 Ordered Table ► Idea: if the list was ordered by key, searching is simpler/easier ► Just like for priority queues, insertion is slightly more complex ► Need to search for proper position of element -> O( n ) ► Find: don’t do a linear scan; instead, do a binary search ► Note: use array/ArrayList; not a linked list

9 Binary Search ► Take advantage of the fact that the elements are ordered ► Compare the target key with middle element to reduce the search space in half ► Repeat the process until the element is found or search space reduces to 1 ► Arithmetic on array indexes facilitate easy computation of middle position ► Middle of S[low] and S[high] is S[(low+high)/2] ► Not possible with linked lists

10 Binary Search Algorithm Algorithm BinarySearch( S, k, low, high ) if low > high then return null; // not found else mid  (low+high)/2 e  S[mid]; if k = e.getKey() then return e; else if k < e.getKey() then return BinarySearch( S, k, low, mid-1 ) else return BinarySearch( S, k, mid+1, high ) BinarySearch( S, someKey, 0, size-1 ); array of Entries target key

11 Binary Search Algorithm 42578912141719222527283337 lowmidhigh find(22) mid = (low+high)/2

12 Binary Search Algorithm 42578912141719222527283337 highlowmid find(22) mid = (low+high)/2

13 Binary Search Algorithm low 42578912141719222527283337 midhigh find(22) mid = (low+high)/2

14 Binary Search Algorithm low=mid=high 42578912141719222527283337 find(22) mid = (low+high)/2

15 Time Complexity of Binary Search ► Search space reduces by half until it becomes 1 ► n  n/2  n/4  …  1 ► log n steps ► Find operation using binary search is O( log n )

16 Time Complexity O( log n ) O( n ) find() O(n ) Ordered Table O( n )O( 1 )Unsorted List remove()insert()Operation

17 Binary Search Tree (BST) ► Strategy: store entries as nodes in a tree such that an in-order traversal of the entries would list them in increasing order ► Search, remove, and insert are all O( log n ) operations ► All operations require a search that mimics binary search: go to left or right subtree depending on target key value

18 Traversing a BST ► Insert, remove, and find operations all require a key ► First step involves checking for a matching key in the tree ► Start with the root, go to left or right child depending on key value ► Repeat the process until key is found or a null child is encountered (not found) ► For insert operation, duplicate key error occurs if key already exists ► Operation is proportional to height of tree ( usually O(log n ) )

19 Insertion in a BST (insert 78) 44 1788 32 28 29 65 5482 76 97 80

20 Insertion in a BST (insert 78) 44 1788 32 28 29 65 5482 76 97 80

21 Insertion in a BST (insert 78) 44 17 88 32 28 29 65 5482 76 97 80

22 Insertion in a BST (insert 78) 44 17 88 32 28 29 65 5482 76 97 80

23 Insertion in a BST (insert 78) 44 17 88 32 28 29 65 54 82 76 97 80

24 Insertion in a BST (insert 78) 44 17 88 32 28 29 65 54 82 76 97 80

25 Insertion in a BST (insert 78) 44 17 88 32 28 29 65 54 82 76 97 80

26 Insertion in a BST (insert 78) 44 17 88 32 28 29 65 54 82 76 97 80

27 Insertion in a BST (insert 78) 44 17 88 32 28 29 65 54 82 76 97 80 78

28 Removal from a BST (Ex 1) 44 1788 32 28 29 65 5482 76 97 80 78

29 Removal from a BST (Ex 1) 44 1788 32 28 29 65 5482 76 97 80 78 Remove 32

30 Removal from a BST (Ex 1) 44 1788 32 28 29 65 5482 76 97 80 78

31 Removal from a BST (Ex 1) 44 17 88 32 28 29 65 5482 76 97 80 78

32 Removal from a BST (Ex 1) 44 17 88 32 28 29 65 5482 76 97 80 78

33 Removal from a BST (Ex 1) 44 17 88 32 28 29 65 5482 76 97 80 78

34 Removal from a BST (Ex 1) 44 17 88 28 29 65 5482 76 97 80 78

35 Removal from a BST (Ex 1) 44 17 88 28 29 65 5482 76 97 80 78

36 Removal from a BST (Ex 2) 44 1788 32 28 29 65 5482 76 97 80 78 Remove 65

37 Removal from a BST (Ex 2) 44 17 88 32 28 29 65 5482 76 97 80 78

38 Removal from a BST (Ex 2) 44 17 88 32 28 29 76 5482 80 97 78

39 Time Complexity for BSTs ► O( log n ) operations not guaranteed since resulting tree is not necessarily “balanced” ► If tree is excessively skewed, operations would be O( n ) since the structure degenerates to a list ► Tree could be periodically reordered to prevent skewedness

40 Time Complexity (average case) O(n )O( log n )O( n )Ordered Table O( log n ) O( n ) find() O( log n ) BST O( n )O( 1 )Unsorted List remove()insert()Operation

41 Time Complexity (worst case) O(n )O( log n )O( n )Ordered Table O( n ) find() O( n ) BST O( n )O( 1 )Unsorted List remove()insert()Operation

42 About BSTs ► AVL tree: BST that “self-balances” ► Ensures that after every operation, the difference between the left subtree height and the right subtree height is at most 1 ► O( log n ) operation is guaranteed ► Many efficient searching methods are variants of binary search trees ► Database indexes are B-trees (number of children > 2, but the same principles apply)


Download ppt "Dictionaries CS 110: Data Structures and Algorithms First Semester, 2010-2011."

Similar presentations


Ads by Google