Hash table separate chaining
Key Takeaways
This video discusses hash tables and separate chaining, a hash collision resolution technique that uses an auxiliary data structure, typically a linked list, to handle collisions in a hash table.
Full Transcript
okay let's talk about something super super cool and that is hash tables and separate chaining all rights dive right in what is separate chaining so separate chaining is just one of many many hash collision resolution techniques and how it works is when there's a hash collision meaning two keys hash to the same value we need to have some sort of way of handling that within our hash table so it's still functional well what separate chaining does is it maintains an auxilary data structure to essentially hold all the collisions and so we can go back and look up inside that bucket or that data structure of values for the item we're looking for and usually we use the linked list for this but it's not limited to only linked lists can use arrays binary trees cell balancing trees or even a hybrid approach okay so suppose we have the following hash table it's just a fancy name for an array of key value pairs of age and names and we associate an arbitrary hash value that has been computed with some hash function so those are our hash values they don't really matter for now we're just going to see how we can use separate chaining to handle the collisions okay so on the left is our hash table so our array and I'm going to be inserting all of these key value pairs into this hash table via separate chaining and you'll see how easy it is okay so our first person is will his ages 21 and he hashed to 3 so we're going to put him in that slot 3 Leah age 18 hash to 4 since she has to 4 we're going to put her at index for so the hash is a way to index into the array okay Rick age 61 hash to going to put them there and Ray age 25 hash one okay we're starting to get a little bit full in our hash table here so make some collisions pretty soon okay lera age 34 hash to four but we say oh shoot leah is already there what do we do well in separate chaining we just chain along so essentially each each position in the array is actually a linked list data structure so we're going to scan through the linked list and see if Alera exists and if Lehrer does not exist then we're going to add lera at the very end of the chain okay so Ryan also hash to one but then we look and Ryan is not equal to R a so we need to add a new entry at position one al era age 34 hash to four so nope and Oh layer already existed in our hash table so we're good and she has the same age so we don't need to update it so thin age 21 hash to 3 so easy our hash collision with will who has also hash to 3 so what we're going to do is we're going to append sin to the end of the length less chain so note that even though that will and Finn both hashed to same value that it'll index 3 oh and they have the same age we tell them apart because we store both the key and the value as an entry in our linked list block so that's how we're able to tell them apart okay now I want insert mark whose age is 10 and who has to four so scan through the link list at index 4 for mark and he's not found so we have to append mark at the very end all right now let's have a look at how we can do lookups in this structure so it's basically the same thing so what we're going to do is give it a name we want to find well what the person's age is so suppose we have Ryan Ryan when we hash and we get one so we suspect that Ryan should be in bucket one when I say a bucket I just mean whatever data structure we're using at index one and in our case a linked list so I just scan this linked list for Ryan so start right now so here we're comparing the key so we're comparing the key Ryan to the key array and there's not a match to keep going compare right Ryan there's a match okay we found Ryan and then inside that and true say oh his age is 56 so returned 56 let's do another one find the age of Mark hash mark and since our hash functions are deterministic we know that if there's a mark then it's going to be fine in position four okay so we look in the bucket for scan through oh last one is a mark so we return age 10 so it might happen that the value or the key looking for doesn't exist and so it doesn't exist you turn now okay so here's a good question how do we maintain a constant time insertion and lookup time complexity if the hash table gets really full and I have some long linked list chains good question and the answer is that once there's a lot of elements within your hash table you'll actually want to create a new hash table with the larger capacity and rehash all your items and reinsert them into this new a larger table because tables are fixed size okay and another good question how do I remove key value pairs from my hash table with separate chaining well the answer is basically the same procedure you hash your key and instead of doing a lookup well you would remove particular entry and the length list that's it another question how do I use can I use another data structure to model the bucket behavior of such a mean yes of course and clumb data structures that are used inside a linked list include arrays binary trees up by self balancing trees java uses a hybrid approach in our hash map so once we get to a certain chain length they switch to a binary tree or maybe a self balance by a tree I'm not too sure however these alternative methods are a bit more memory intensive and complex to implement which is wighting maybe less popular but they might be a lot faster - I haven't actually implemented them myself so have a look at those okay so that's it for this video next video we're going to be going over hash tables with open addressing which I'm really excited about the guys if you want an implementation of a hash table with separate chaining there's joint be one at github.com slash will infuse a slash dated - structures and I will also be going this or some source code in the next video so stay tuned for that thanks so much for watching catch you later
Original Description
Related Videos:
Hash table intro/hash function: https://www.youtube.com/watch?v=2E54GqF0H4s
Hash table separate chaining: https://www.youtube.com/watch?v=T9gct6Dx-jo
Hash table separate chaining code: https://www.youtube.com/watch?v=Av9kwXkuQFw
Hash table open addressing: https://www.youtube.com/watch?v=xIejolxzZS8
Hash table linear probing: https://www.youtube.com/watch?v=Ma9XOInZJWM
Hash table quadratic probing: https://www.youtube.com/watch?v=b0858c55TGQ
Hash table double hashing: https://www.youtube.com/watch?v=H5e9V5x92vI
Hash table open addressing removing: https://www.youtube.com/watch?v=7eLDTtbzX4M
Hash table open addressing code: https://www.youtube.com/watch?v=7eLDTtbzX4M
Data Structures Source Code:
https://github.com/williamfiset/algorithms
My website: http://www.williamfiset.com ===================================
Practicing for interviews? I have used, and recommend `Cracking the Coding Interview` which got me a job at Google. Link on Amazon: https://amzn.to/3cvMof5
A lot of the content on this channel is inspired by the book `Competitive Programming` by Steven Halim which I frequently use as a resource and reference. Link on Amazon: https://amzn.to/3wC2nix
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from WilliamFiset · WilliamFiset · 60 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
▶
JES Image Manipulation - 2 - Installation
WilliamFiset
JES Image Manipulation - 3 - User Interface
WilliamFiset
JES Image Manipulation - 5 - Negative
WilliamFiset
JES Image Manipulation - 6 - Black & White
WilliamFiset
JES Image Manipulation - 4 - Grayscale
WilliamFiset
JES Image Manipulation - 8 - Blur
WilliamFiset
JES Image Manipulation - 7 - Edge Detection
WilliamFiset
JES Image Manipulation - 9 - Blend
WilliamFiset
JES Image Manipulation - 10 - Matte
WilliamFiset
JES Image Manipulation - 13 - Rotate90
WilliamFiset
JES Image Manipulation - 12 - Mirroring Picture
WilliamFiset
JES Image Manipulation - 11 - Crop Image
WilliamFiset
JES Image Manipulation - 14 - Stretch picture
WilliamFiset
Java Fractal Explorer [6/8]
WilliamFiset
Java Fractal Explorer [4/8]
WilliamFiset
Java Fractal Explorer [8/8]
WilliamFiset
Java Fractal Explorer [5/8]
WilliamFiset
Java Fractal Explorer [2/8]
WilliamFiset
Java Fractal Explorer [7/8]
WilliamFiset
Java Fractal Explorer [1/8]
WilliamFiset
Java Fractal Explorer [3/8]
WilliamFiset
Introduction [Programming Competition Problems]
WilliamFiset
String Manipulation 1 [Programming Competition Problems]
WilliamFiset
String Manipulation 2 [Programming Competition Problems]
WilliamFiset
Graph Theory 1 [Programming Competition Problems]
WilliamFiset
Logic 1 [Programming Competition Problems]
WilliamFiset
Grid Problems 1 [Programming Competition Problems]
WilliamFiset
Dynamic Programming 1 [Programming Competition Problems]
WilliamFiset
Introduction to Big-O
WilliamFiset
Dynamic and Static Arrays
WilliamFiset
Dynamic Array Code
WilliamFiset
Linked Lists Introduction
WilliamFiset
Doubly Linked List Code
WilliamFiset
Stack Introduction
WilliamFiset
Stack Implementation
WilliamFiset
Stack Code
WilliamFiset
Queue Introduction
WilliamFiset
Queue Implementation
WilliamFiset
Queue Code
WilliamFiset
Priority Queue Introduction
WilliamFiset
Priority Queue Min Heaps and Max Heaps
WilliamFiset
Priority Queue Inserting Elements
WilliamFiset
Priority Queue Removing Elements
WilliamFiset
Priority Queue Code
WilliamFiset
Union Find Introduction
WilliamFiset
Union Find Kruskal's Algorithm
WilliamFiset
Union Find - Union and Find Operations
WilliamFiset
Union Find Path Compression
WilliamFiset
Union Find Code
WilliamFiset
Binary Search Tree Introduction
WilliamFiset
Binary Search Tree Insertion
WilliamFiset
Binary Search Tree Removal
WilliamFiset
Binary Search Tree Traversals
WilliamFiset
Binary Search Tree Code
WilliamFiset
Fenwick Tree range queries
WilliamFiset
Fenwick Tree point updates
WilliamFiset
Fenwick Tree construction
WilliamFiset
Fenwick tree source code
WilliamFiset
Hash table hash function
WilliamFiset
Hash table separate chaining
WilliamFiset
More on: Algorithm Basics
View skill →Related Reads
📰
📰
📰
📰
Day 29/100 Koko Eating Bananas (Binary Search)
Medium · Programming
O(N) Manacher's Algorithm with Mirror Boundary Optimization
Dev.to · Dipaditya Das
Building a Power Grid Inside Minecraft with BFS Algorithms
Dev.to · Carlos Cortez 🇵🇪 [AWS Hero]
The Run-Length Encoding Trick: How Simple Strings Get Compressed
Medium · Programming
🎓
Tutor Explanation
DeepCamp AI