Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a1d54ac

Browse files
update dictionaries note
1 parent fc8375f commit a1d54ac

File tree

1 file changed

+9
-28
lines changed

1 file changed

+9
-28
lines changed

‎_posts/2013-02-27-3.3-dictionaries.md

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ title: 3.3 Dictionaries
1717

1818
## Dictionary implementations
1919

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.
2121

2222
### unsorted arrays
2323

@@ -37,34 +37,15 @@ In practice dictionaries are implemented with binary search trees or hashtables
3737

3838
### linked lists
3939

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.
4141

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
4949
* on insertion check if we inserted in the last position (last->next still null)
5050
* on delete in doubly linked lists if deleting last update the tail pointer to its precedessor
5151
* 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:
54-
55-
* singly (1) or double (2) linked
56-
* sorted (S) or unsorted (U)
57-
58-
59-
| operation | 1,U | 2,U | 1,S | 2,S 
60-
|:-----------|:-------------:|:-------------:|:-------------:|:-------------:
61-
| search | O(n) | O(n) | O(n) | O(n)
62-
| insert | O(1) | O(1) | O(n) | O(n)
63-
| delete | O(n) | O(1) | O(n) | O(1)
64-
| successor | O(n) | O(n) | O(1) | O(1)
65-
| precedessor| O(n) | O(n) | O(n) | O(1)
66-
| minimum | O(n) | O(n) | O(1) | O(1)
67-
| maximum | O(n) | O(n) | O(1) | O(1)
68-
69-
70-

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /