HASH TABLE
Hash Table is a data structure
which stores data in an
associative manner. In a hash
table, data is stored in an array
format, where each data value
has its own unique index value.
Access of data becomes very
fast if we know the index of the
desired data.
Formation of Hash Table :
A hash table uses a hash function to compute an index, also called a
hash code, into an array of buckets or slots, from which the desired
value can be found. During lookup, the key is hashed, and the resulting
hash indicates where the corresponding value is stored.
• Hash Tables
• Hashing
• Hash function
Terms Used :
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
HASHING
KEY VALUES OR ELEMENTS: 3, 8 ,13, 6, 4, 10
A
We store or we take the value of element itself as the index
3 4 6 8 10 13
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
A
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
WHAT IF WE WANT TO STORE KEY ELEMENTS
3, 8, 13, 6, 4, 10, 50
3 4 6 8 10 13
• To store key element 50, we have to ma array up to index 50 , in
this way a lot of memory will be allocated and wasted.
• To deal with such condition we introduce mathematical model called
as hashing.
8
3
13
6
4
10
Key space Hash function
H(x) = x % Size 0
1
2
3
4
5
6
7
8
9
10
3,13
4
6
8
Hash Table
8 % 10 = 8
3 % 10 = 3
13 %10 = 3
6 % 10 = 6
4 % 10 = 4
10 % 10 = 0
Collision
Size of hash table =10
8
3
13
6
4
10
10
4
6
8
3 13
Open hashing / chaining:
Key space
Hash Table
Hash function
H(x) = x % Size
0
1
2
3
4
5
6
7
8
9
Closed hashing / linear probing:
10
3
13
4
6
8
0
1
2
3
4
5
6
7
8
9
8
3
13
6
4
10
Key space
Hash function
H(x) = x % Size
Hash Table
H(x) = x % size
H’(x) = [ h (x) + f (i) ] % size
H’(13) = [ h (13) + f (0) ] % 10
= 3
H’(13) = [ h (13) + f (1) ] % 10
= 4
F(I) = I
I = 0,1,2,……
Closed hashing / Quadradic Probing:
10
3
13
23
8
0
1
2
3
4
5
6
7
8
9
8
3
13
23
43
10
Key space
Hash function
H(x) = x % Size
Hash Table
H(x) = x % size
H’(x) = [ h (x) + f (i) ] % size
H’(23) = [ h (23) + f (0) ] % 10
= 3
H’(23) = [ h (23) + f (1) ] % 10
= (3+1) %10 = 4
H’(23) = [ h (23) + f (2) ] % 10
=(3 + 4 ) %10 = 7
F(I) = 𝐼2
0,1,2,……
program to implement hashing with
chaining
class Hash
{
int BUCKET; // No. of buckets
// Pointer to an array containing buckets
list<int> *table;
public:
Hash(int V); // Constructor
// inserts a key into hash table
void insertItem(int x);
// deletes a key from hash table
void deleteItem(int key);
// hash function to map values to key
int hashFunction(int x) {
return (x % BUCKET);
}
void displayHash();
};
void Hash::deleteItem(int key)
{
// get the hash index of key
int index = hashFunction(key);
// find the key in (index)th list
list <int> :: iterator i;
for (i = table[index].begin();
i != table[index].end(); i++) {
if (*i == key)
break;
}
// if key is found in hash table, remove it
if (i != table[index].end())
table[index].erase(i);
}
// function to display hash table
void Hash::displayHash() {
for (int i = 0; i < BUCKET; i++) {
cout << i;
for (auto x : table[i])
cout << " --> " << x;
cout << endl;
}
}
// Driver program
int main()
{
// array that contains keys to be mapped
int a[] = {15, 11, 27, 8, 12};
int n = sizeof(a)/sizeof(a[0]);
// insert the keys into the hash table
Hash h(7); // 7 is count of buckets in
// hash table
for (int i = 0; i < n; i++)
h.insertItem(a[i]);
// delete 12 from hash table
h.deleteItem(12);
// display the Hash table
h.displayHash();
return 0;
}
It is done by hash algorithm
A hash algorithm is a function that converts a data string into a
numeric string output of fixed length.
How hashing used in networking?
THANK YOU !

Hash table (2)

  • 1.
    HASH TABLE Hash Tableis a data structure which stores data in an associative manner. In a hash table, data is stored in an array format, where each data value has its own unique index value. Access of data becomes very fast if we know the index of the desired data.
  • 2.
    Formation of HashTable : A hash table uses a hash function to compute an index, also called a hash code, into an array of buckets or slots, from which the desired value can be found. During lookup, the key is hashed, and the resulting hash indicates where the corresponding value is stored. • Hash Tables • Hashing • Hash function Terms Used :
  • 3.
    0 1 23 4 5 6 7 8 9 10 11 12 13 14 HASHING KEY VALUES OR ELEMENTS: 3, 8 ,13, 6, 4, 10 A We store or we take the value of element itself as the index 3 4 6 8 10 13 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 A
  • 4.
    0 1 23 4 5 6 7 8 9 10 11 12 13 14 WHAT IF WE WANT TO STORE KEY ELEMENTS 3, 8, 13, 6, 4, 10, 50 3 4 6 8 10 13 • To store key element 50, we have to ma array up to index 50 , in this way a lot of memory will be allocated and wasted. • To deal with such condition we introduce mathematical model called as hashing.
  • 5.
    8 3 13 6 4 10 Key space Hashfunction H(x) = x % Size 0 1 2 3 4 5 6 7 8 9 10 3,13 4 6 8 Hash Table 8 % 10 = 8 3 % 10 = 3 13 %10 = 3 6 % 10 = 6 4 % 10 = 4 10 % 10 = 0 Collision Size of hash table =10
  • 6.
    8 3 13 6 4 10 10 4 6 8 3 13 Open hashing/ chaining: Key space Hash Table Hash function H(x) = x % Size 0 1 2 3 4 5 6 7 8 9
  • 7.
    Closed hashing /linear probing: 10 3 13 4 6 8 0 1 2 3 4 5 6 7 8 9 8 3 13 6 4 10 Key space Hash function H(x) = x % Size Hash Table H(x) = x % size H’(x) = [ h (x) + f (i) ] % size H’(13) = [ h (13) + f (0) ] % 10 = 3 H’(13) = [ h (13) + f (1) ] % 10 = 4 F(I) = I I = 0,1,2,……
  • 8.
    Closed hashing /Quadradic Probing: 10 3 13 23 8 0 1 2 3 4 5 6 7 8 9 8 3 13 23 43 10 Key space Hash function H(x) = x % Size Hash Table H(x) = x % size H’(x) = [ h (x) + f (i) ] % size H’(23) = [ h (23) + f (0) ] % 10 = 3 H’(23) = [ h (23) + f (1) ] % 10 = (3+1) %10 = 4 H’(23) = [ h (23) + f (2) ] % 10 =(3 + 4 ) %10 = 7 F(I) = 𝐼2 0,1,2,……
  • 9.
    program to implementhashing with chaining class Hash { int BUCKET; // No. of buckets // Pointer to an array containing buckets list<int> *table; public: Hash(int V); // Constructor // inserts a key into hash table void insertItem(int x); // deletes a key from hash table void deleteItem(int key); // hash function to map values to key int hashFunction(int x) { return (x % BUCKET); } void displayHash(); }; void Hash::deleteItem(int key) { // get the hash index of key int index = hashFunction(key); // find the key in (index)th list list <int> :: iterator i; for (i = table[index].begin(); i != table[index].end(); i++) { if (*i == key) break; } // if key is found in hash table, remove it if (i != table[index].end()) table[index].erase(i); } // function to display hash table void Hash::displayHash() { for (int i = 0; i < BUCKET; i++) { cout << i; for (auto x : table[i]) cout << " --> " << x; cout << endl; } }
  • 10.
    // Driver program intmain() { // array that contains keys to be mapped int a[] = {15, 11, 27, 8, 12}; int n = sizeof(a)/sizeof(a[0]); // insert the keys into the hash table Hash h(7); // 7 is count of buckets in // hash table for (int i = 0; i < n; i++) h.insertItem(a[i]); // delete 12 from hash table h.deleteItem(12); // display the Hash table h.displayHash(); return 0; }
  • 11.
    It is doneby hash algorithm A hash algorithm is a function that converts a data string into a numeric string output of fixed length. How hashing used in networking?
  • 12.