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 fc8375f

Browse files
added linked list dictionary implemenations
1 parent e15233c commit fc8375f

File tree

2 files changed

+41
-5
lines changed

2 files changed

+41
-5
lines changed

‎_config.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
permalink: none
2-
name: Algorithm Design Manual notes
2+
name: Algorithm Design Manual notes
3+
markdown: rdiscount

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

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,63 @@ title: 3.3 Dictionaries
88

99
## Dictionary operations
1010

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
1313
* delete - given an element remove it form the dictionary
1414
* max, min - certain implementations support efficient retrieval of the largest/smallest key from the dictionary
1515
* predecessor, successor - certain implementations support efficient retrieval of the item immediately before/after a given key (in sorted order) from the dictionary
1616
* different implementations provide better/worse running time guarantees for different operations
1717

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
1923

2024
* search - O(n) - checking the key against each element (potentionally)
2125
* insert - O(1) - just add the element at the end
2226
* delete - O(1) - pointer is given to element to remove, overwrite position with last element
2327
* min/max - O(n) - requires linear sweep through the elements
2428
* predecessor/successor - O(n) - requires linear sweep through the elements
2529

26-
##Sorted array dictionary implementation
30+
### sorted arrays
2731

2832
* search - O(log n) - using binary search
2933
* insert - O(n) - because making room for the new item
3034
* delete - O(n) - because filling the hole left by the deletem item
3135
* min/max - O(1) - first and last element
3236
* predecessor/successor - O(1) - elements in previous/next positions
3337

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:
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+
3469

3570

0 commit comments

Comments
(0)

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