34

JS Monday 11 – The HashTable Data Structure

 5 years ago
source link: https://www.tuicool.com/articles/hit/IZJFRfu
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
jArQz27.png!webRzmMVzN.png!web

An HashTable is a simple but powerful data structure which is used to associate a key with a given value.

If you’re coming from PHP , you may remember associative arrays , which are implemented as HashTables !

VZzQZvu.png!web

As you can see in the PHP example above, we’re creating an array which contains key-value data. Nothing strange!

So what’s so special about associative arrays? Why are they so popular and powerful? How are they implemented?

Why HashTables?

HashTables are used to store data, just like arrays or objects.

An HashTable uses a hashing algorithm to compute an index into an array of buckets (or slots ), where we can find the desired data.

Let’s illustrate an example:

RnquUnM.png!webuYjmme7.png!web

Given the example above, we can pretend we have the following associative array ( in pseudocode ):

6zmmUfn.png!webEVVfIjJ.png!web

So, if we want to get the “ Ghibli” brand, we could loop over the array until we find the key “Ghibli” and then we retrive its value… but what if our array has thousands of items? Looping over it will require some time!

And what if we have some kind of hashing function that can retrieve for us the correct array index in a deterministic way?

That’s actually what HashTables do! So, our hashing function (in that case), could just return 4 , which is the index of “Ghibli”! Much more efficient than looping over an array!

Does the previous illustration make sense now?

Are there any chances that we can get the same index for different keys?

Yes of course! That is called collision .

We get a collision every time that two (or more) hashed keys returns the same index.

How do we handle that?

We just store both the key-value pairs in the same index using other collections like arrays or linked lists.

This technique is called separate chaining .

Implementing an HashTable

First of all, we need a simple and efficient hashing algorithm:

VRRf2mi.png!web3uAvUva.png!web

Great! With this simple function we’ll be able to always get the same result for the same input. That’s exactly what we need!

Let’s quickly test it:

UzIfiyb.png!webrAbYfmn.png!web

So now we need to implement our HashTable class!

Let’s write it down:

JvEBVzj.png!webRZFNjui.png!web

We just created our HashTable class with 42 buckets. A bucket is a single array index, which we’ll store our data in.

For instance, if we get 21 from our hashing function, that means that we’ll find our desired data in the 21st bucket.

We also use the newest ES6 Map object, which will iterate its element in insertion order and will save us a bit of work!

Let’s implement our insert method:

qYFNJff.png!webN7nE7bZ.png!web

Pretty easy, isn’t it? And what about a search method, to retrieve our data?

ryMzUzv.png!webyieuQ3f.png!web

Even easier! Now we just need a method to delete our data:

Rn2Qvmv.png!webfuaq63n.png!web

Believe or not, we’re done! We’ve implemented a super basic HashTable in just a few lines of JavaScript! Let’s test it:

qqmqUjn.png!webYBV3IrZ.png!web

So now we log the following output to the console:

yi2Ubyq.png!web7niQbe2.png!web

As you can see, “ Giulietta ” and “ DB11 ” causes a collision! Let’s try to retrieve their brands:

FFRfyin.png!webzIZryme.png!web

Oh yes! It works! And what about deleting some keys?

2U777zy.png!webiemIfu6.png!web

A Little Disclaimer

Ok, we cheated a bit so far. We implemented an HashTable using the Map object, which is doing some work for us.

We’re not currently managing collisions by hand, but we’ve achieved our task: building a simple HashTable class and understood how it works!

UjyANva.png!webqeYbem7.png!web

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK