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
+9-28Lines changed: 9 additions & 28 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -17,7 +17,7 @@ title: 3.3 Dictionaries
17
17
18
18
## Dictionary implementations
19
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.
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
21
22
22
### unsorted arrays
23
23
@@ -37,34 +37,15 @@ In practice dictionaries are implemented with binary search trees or hashtables
37
37
38
38
### linked lists
39
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.
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
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
42
+
* search - have to sweep through all elements in all cases (O(n))
43
+
* insert - items can simply be added at the end of the unsorted list (O(1)) 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, O(1) in doubly linked lists but has to do a linear sweep for singly linked
45
+
* successor is O(1) in sorted lists
46
+
* finding the precedessor is O(1) in doubly linked sorted lists
47
+
* minimum - sits at the head of the sorted lists (O(1))
48
+
* maximum - maintaining a pointer to the last element (tail) makes it O(1) in sorted lists
49
49
* on insertion check if we inserted in the last position (last->next still null)
50
50
* on delete in doubly linked lists if deleting last update the tail pointer to its precedessor
51
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