You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: _posts/2013-02-27-3.3-dictionaries.md
+39-4Lines changed: 39 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,28 +8,63 @@ title: 3.3 Dictionaries
8
8
9
9
## Dictionary operations
10
10
11
-
* search – given a search key, return a pointer to the element whose key value is k (if one exists)
12
-
* insert – given an element and it's key add it to the dictionary
11
+
* search - given a search key, return a pointer to the element whose key value is k (if one exists)
12
+
* insert - given an element and it's key add it to the dictionary
13
13
* delete - given an element remove it form the dictionary
14
14
* max, min - certain implementations support efficient retrieval of the largest/smallest key from the dictionary
15
15
* predecessor, successor - certain implementations support efficient retrieval of the item immediately before/after a given key (in sorted order) from the dictionary
16
16
* different implementations provide better/worse running time guarantees for different operations
17
17
18
-
## Unsorted array dictionary implementation
18
+
## Dictionary implementations
19
+
20
+
In practice dictionaries are implemented with binary search trees or hashtables but it's interesting to have a look at how we would implement them using array or linked lists.
21
+
22
+
### unsorted arrays
19
23
20
24
* search - O(n) - checking the key against each element (potentionally)
21
25
* insert - O(1) - just add the element at the end
22
26
* delete - O(1) - pointer is given to element to remove, overwrite position with last element
23
27
* min/max - O(n) - requires linear sweep through the elements
24
28
* predecessor/successor - O(n) - requires linear sweep through the elements
25
29
26
-
##Sorted array dictionary implementation
30
+
### sorted arrays
27
31
28
32
* search - O(log n) - using binary search
29
33
* insert - O(n) - because making room for the new item
30
34
* delete - O(n) - because filling the hole left by the deletem item
31
35
* min/max - O(1) - first and last element
32
36
* predecessor/successor - O(1) - elements in previous/next positions
33
37
38
+
### linked lists
39
+
40
+
A linked list can be singly or doubly linked and sorted or unsorted. This has effects on the running time of the various dictionary operations.
41
+
42
+
* search - have to sweep through all elements in all cases
43
+
* insert - items can simply be added at the en of the unsorted list but have to find their place in the sorted list
44
+
* delete - we have to find the element before the element to delete to update to pointers, double linked list are useful here but has to do a linear sweep for singly linked
45
+
* successor is straightforward in sorted lists
46
+
* finding the precedessor is only easy in doubly linked sorted lists
47
+
* minimum - sits at the head of the sorted lists
48
+
* maximum - maintaining a pointer to the last element (tail) of the sorted lists makes this easy
49
+
* on insertion check if we inserted in the last position (last->next still null)
50
+
* on delete in doubly linked lists if deleting last update the tail pointer to its precedessor
51
+
* delete in singly lists is already linear so adding an extra linear sweep to update the tail pointer doesn't do any harm
52
+
53
+
The table below lists the runtime complexity for each operation with different implementations:
0 commit comments