Skip to content

Commit 78b5bbf

Browse files
committed
updated hash tables guide
1 parent b0d1bd7 commit 78b5bbf

File tree

3 files changed

+5
-6
lines changed

3 files changed

+5
-6
lines changed
134 KB
Loading

hash_tables/figures/hash_table.png

120 KB
Loading

hash_tables/hash_tables.md

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,20 @@ Internally,a hash table stores its values in an array. A special **hash function
1818

1919
Hash function generate a code, known as the **hash code**. Each data type will have its own hash function that generates its hash code differently. It is important to remember that a hash code is not equivalent to the index in the underlying array storage structure--there are often more hash codes than indices in the underlying array.
2020

21-
[insert diagram here]
21+
**Figure below**: Internals of a hash table
22+
![](https://i.imgur.com/bEIWPaQ.png)
2223

2324

2425
**Note:** When utilizing a hash table with a class you've created, be sure that the hash function for that object type operates as you would expect. If two objects are equivalent, you should ensure that their hash codes are the same.
2526

2627

2728
### Collisions
28-
If the hash function is implemented well, inputted objects will be distributed evenly across the array indices. However, the number of hash codes is often greater than the size of the underlying array, so some keys will bes assigned the same index. When two keys are matched to the same index, this is called a **collision**. There are several different ways of addressing collisions.
29+
If the hash function is implemented well, inputted objects will be distributed evenly across the array indices. However, the number of hash codes is often greater than the size of the underlying array, so some keys will be assigned the same index. When two keys are matched to the same index, this is called a **collision**. There are several different ways of addressing collisions.
2930

3031
There are multiple approaches to dealing with collisions. The most common one is simply to store all the objects that get assigned to the same index in a linked list. In this scenario, instead of simply storing the value at that index, the linked list must contain both the entire key and the value in pairs instead of just the value, so that the values can be uniquely tied to a key.
3132

32-
[insert diagram here]
33+
**Figure below**: Hash Collision
34+
![](https://i.imgur.com/ZqF2crs.png)
3335

3436
For more information about other ways to address hash collisions, see the [hash tables Wikipedia page](https://en.wikipedia.org/wiki/Hash_table#Open_addressing) for descriptions of several different approaches. If you have time, I'd encourage you to learn about open addressing, the other common hash collision method.
3537

@@ -53,6 +55,3 @@ To get a more thorough understanding on the internals of hash tables, here are a
5355
* Hash table guide with helpful diagrams: https://medium.com/basecs/hashing-out-hash-functions-ea5dd8beb4dd
5456
* Princeton Coursera video lecture series: https://www.coursera.org/learn/algorithms-part1/lecture/CMLqa/hash-tables
5557
* HackerRank video (short summary): https://youtu.be/shs0KM3wKv8
56-
57-
58-

0 commit comments

Comments
 (0)