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: en/docs/chapter_hashing/hash_map.md
+15-15Lines changed: 15 additions & 15 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,16 +1,16 @@
1
1
# Hash table
2
2
3
-
A <u>hash table</u>, also known as a <u>hash map</u>, is a data structure that establishes a mapping between keys and values, enabling efficient element retrieval. Specifically, when we input a `key` into the hash table, we can retrive the corresponding `value` in $O(1)$ time complexity.
3
+
A <u>hash table</u>, also known as a <u>hash map</u>, is a data structure that establishes a mapping between keys and values, enabling efficient element retrieval. Specifically, when we input a `key` into the hash table, we can retrieve the corresponding `value` in $O(1)$ time complexity.
4
4
5
5
As shown in the figure below, given $n$ students, each student has two data fields: "Name" and "Student ID". If we want to implement a query function that takes a student ID as input and returns the corresponding name, we can use the hash table shown in the figure below.
6
6
7
7

8
8
9
9
In addition to hash tables, arrays and linked lists can also be used to implement query functionality, but the time complexity is different. Their efficiency is compared in the table below:
10
10
11
-
-**Inserting elements**: Simply append the element to the tail of the array (or linked list). The time complexity of this operation is $O(1)$.
12
-
-**Searching for elements**: As the array (or linked list) is unsorted, searching for an element requires traversing through all of the elements. The time complexity of this operation is $O(n)$.
13
-
-**Deleting elements**: To remove an element, we first need to locate it. Then, we delete it from the array (or linked list). The time complexity of this operation is $O(n)$.
11
+
-**Inserting an element**: Simply append the element to the tail of the array (or linked list). The time complexity of this operation is $O(1)$.
12
+
-**Searching for an element**: As the array (or linked list) is unsorted, searching for an element requires traversing through all of the elements. The time complexity of this operation is $O(n)$.
13
+
-**Deleting an element**: To remove an element, we first need to locate it. Then, we delete it from the array (or linked list). The time complexity of this operation is $O(n)$.
14
14
15
15
<palign="center"> Table <id> Comparison of time efficiency for common operations </p>
16
16
@@ -20,7 +20,7 @@ In addition to hash tables, arrays and linked lists can also be used to implemen
20
20
| Insert Elements | $O(1)$ | $O(1)$ | $O(1)$ |
21
21
| Delete Elements | $O(n)$ | $O(n)$ | $O(1)$ |
22
22
23
-
It can be seen that**the time complexity for operations (insertion, deletion, searching, and modification) in a hash table is $O(1)$**, which is highly efficient.
23
+
As observed,**the time complexity for operations (insertion, deletion, searching, and modification) in a hash table is $O(1)$**, which is highly efficient.
24
24
25
25
## Common operations of hash table
26
26
@@ -56,7 +56,7 @@ Common operations of a hash table include: initialization, querying, adding key-
56
56
unordered_map<int, string> map;
57
57
58
58
/* Add operation */
59
-
// Add key-value pair (key, value) to the hash table
59
+
// Add key-value pair (key, value) to hash table
60
60
map[12836] = "Xiao Ha";
61
61
map[15937] = "Xiao Luo";
62
62
map[16750] = "Xiao Suan";
@@ -79,7 +79,7 @@ Common operations of a hash table include: initialization, querying, adding key-
79
79
Map<Integer, String> map = new HashMap<>();
80
80
81
81
/* Add operation */
82
-
// Add key-value pair (key, value) to the hash table
82
+
// Add key-value pair (key, value) to hash table
83
83
map.put(12836, "Xiao Ha");
84
84
map.put(15937, "Xiao Luo");
85
85
map.put(16750, "Xiao Suan");
@@ -101,7 +101,7 @@ Common operations of a hash table include: initialization, querying, adding key-
101
101
/* Initialize hash table */
102
102
Dictionary<int, string> map = new() {
103
103
/* Add operation */
104
-
// Add key-value pair (key, value) to the hash table
104
+
// Add key-value pair (key, value) to hash table
105
105
{ 12836, "Xiao Ha" },
106
106
{ 15937, "Xiao Luo" },
107
107
{ 16750, "Xiao Suan" },
@@ -125,7 +125,7 @@ Common operations of a hash table include: initialization, querying, adding key-
125
125
hmap := make(map[int]string)
126
126
127
127
/* Add operation */
128
-
// Add key-value pair (key, value) to the hash table
128
+
// Add key-value pair (key, value) to hash table
129
129
hmap[12836] = "Xiao Ha"
130
130
hmap[15937] = "Xiao Luo"
131
131
hmap[16750] = "Xiao Suan"
@@ -148,7 +148,7 @@ Common operations of a hash table include: initialization, querying, adding key-
148
148
var map: [Int: String] = [:]
149
149
150
150
/* Add operation */
151
-
// Add key-value pair (key, value) to the hash table
151
+
// Add key-value pair (key, value) to hash table
152
152
map[12836] = "Xiao Ha"
153
153
map[15937] = "Xiao Luo"
154
154
map[16750] = "Xiao Suan"
@@ -192,7 +192,7 @@ Common operations of a hash table include: initialization, querying, adding key-
192
192
/* Initialize hash table */
193
193
const map = new Map<number, string>();
194
194
/* Add operation */
195
-
// Add key-value pair (key, value) to the hash table
195
+
// Add key-value pair (key, value) to hash table
196
196
map.set(12836, 'Xiao Ha');
197
197
map.set(15937, 'Xiao Luo');
198
198
map.set(16750, 'Xiao Suan');
@@ -220,7 +220,7 @@ Common operations of a hash table include: initialization, querying, adding key-
220
220
Map<int, String> map = {};
221
221
222
222
/* Add operation */
223
-
// Add key-value pair (key, value) to the hash table
223
+
// Add key-value pair (key, value) to hash table
224
224
map[12836] = "Xiao Ha";
225
225
map[15937] = "Xiao Luo";
226
226
map[16750] = "Xiao Suan";
@@ -245,7 +245,7 @@ Common operations of a hash table include: initialization, querying, adding key-
245
245
let mut map: HashMap<i32, String> = HashMap::new();
246
246
247
247
/* Add operation */
248
-
// Add key-value pair (key, value) to the hash table
248
+
// Add key-value pair (key, value) to hash table
249
249
map.insert(12836, "Xiao Ha".to_string());
250
250
map.insert(15937, "Xiao Luo".to_string());
251
251
map.insert(16750, "Xiao Suan".to_string());
@@ -490,10 +490,10 @@ First, let's consider the simplest case: **implementing a hash table using only
490
490
491
491
So, how do we locate the corresponding bucket based on the `key`? This is achieved through a <u>hash function</u>. The role of the hash function is to map a larger input space to a smaller output space. In a hash table, the input space consists of all the keys, and the output space consists of all the buckets (array indices). In other words, given a `key`, **we can use the hash function to determine the storage location of the corresponding key-value pair in the array**.
492
492
493
-
When given a `key`, the calculation process of the hash function consists of the following two steps:
493
+
With a given `key`, the calculation of the hash function consists of two steps:
494
494
495
495
1. Calculate the hash value by using a certain hash algorithm `hash()`.
496
-
2. Take the modulus of the hash value with the bucket count (array length) `capacity` to obtain the array `index` corresponding to that key.
496
+
2. Take the modulus of the hash value with the bucket count (array length) `capacity` to obtain the array `index` corresponding to the key.
0 commit comments