From 5bb2531aa0686f87c7e3a813d4c2efea4a14c304 Mon Sep 17 00:00:00 2001 From: Softy <161766793+softy-dev@users.noreply.github.com> Date: Fri, 3 Jan 2025 17:21:47 -0300 Subject: [PATCH] Hash Map Lesson: Make use of terms more consistent (#29008) Co-authored-by: nekosoffy <161766793+nekosoffy@users.noreply.github.com> --- .../hash_map_data_structure.md | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/javascript/computer_science/hash_map_data_structure.md b/javascript/computer_science/hash_map_data_structure.md index 3578fb72bb..2a81773451 100644 --- a/javascript/computer_science/hash_map_data_structure.md +++ b/javascript/computer_science/hash_map_data_structure.md @@ -86,7 +86,7 @@ You might be thinking, wouldn't it just be better to save the whole name as a ha ### Buckets -Buckets are storage that we need to store our elements. Simply, it's an array. For a specific key, we decide which bucket to use for storage through our hash function. The hash function returns a number that serves as the index of the array at which we store this specific key value pair. Let's say we wanted to store a person's full name as a key "Fred" with a value of "Smith": +Buckets are storage that we need to store our elements. We can consider each index of an array to have a bucket. For a specific key, we decide which bucket to use for storage through our hash function. The hash function returns a number that serves as the index of the array at which we store this specific key value pair. Let's say we wanted to store a person's full name as a key "Fred" with a value of "Smith": 1. Pass "Fred" into the hash function to get the hash code which is `385`. 1. Find the bucket at index `385`. @@ -98,20 +98,20 @@ This is an oversimplified explanation; we'll discuss more internal mechanics lat Now if we wanted to get a value using a key: -1. To retrieve the value, we hash the key and calculate its bucket number. +1. To retrieve the value, we hash the key and calculate the index of its bucket. 1. If the bucket is not empty, then we go to that bucket. 1. Now we compare if the node's key is the same key that was used for the retrieval. 1. If it is, then we can return the node's value. Otherwise, we return `null`. Maybe you are wondering, why are we comparing the keys if we already found the index of that bucket? Remember, a hash code is just the location. Different keys might generate the same hash code. We need to make sure the key is the same by comparing both keys that are inside the bucket. -This is it, making this will result in a hash table with `has`, `set` and `get`. +This is it, making this will result in a hash map with `has`, `set` and `get`. #### Insertion order is not maintained A hash map does not guarantee insertion order when you iterate over it. The translation of hash codes to indexes does not follow a linear progression from the first to the last index. Instead, it is more unpredictable, irrespective of the order in which items are inserted. That means if you are to retrieve the array of keys and values to iterate over them, then they will not be in order of when you inserted them. -Some libraries implement hash tables with insertion order in mind such as JavaScript's own `Map`. For the coming project however we will be implementing an unordered hash table. +Some libraries implement hash maps with insertion order in mind such as JavaScript's own `Map`. For the coming project however we will be implementing an unordered hash map. Example: if we insert the values `Mao`, `Zach`, `Xari` in this order, we may get back `["Zach", "Mao", "Xari"]` when we call an iterator. If iterating over a hash map frequently is your goal, then this data structure is not the right choice for the job, a simple array would be better. @@ -152,9 +152,9 @@ Up until now, our hash map is a one-dimensional data structure. What if each `No You probably understand by this point why we must write a good hashing function which eliminates as many collisions as possible. Most likely you will not be writing your own hash functions, as most languages have it built in, but understanding how hash functions work is important. -### Growth of a hash table +### Growth of a hash map -Let's talk about the growth of our buckets. We don't have infinite memory, so we can't have infinite number of buckets. We need to start somewhere, but starting too big is also a waste of memory if we're only going to have a hash map with a single value in it. So to deal with this issue, we should start with a small array for our buckets. We'll use an array of size `16`. +Let's talk about our number of buckets. We don't have infinite memory, so we can't have an infinite amount of them. We need to start somewhere, but starting too big is also a waste of memory if we're only going to have a hash map with a single value in it. So to deal with this issue, we should start with a small array for our buckets. We'll use an array of size `16`.