5-Hash Table Datastructure
5-Hash Table Datastructure
Programming
(C++)
Hash Table
1
Outline
▪ Time complexity
▪ Collision
▪ Close hashing
▪ Examples
2
Time complexity
3
Big-O Notation
4
Other Data structure
❑ Searching operation
▪ Linked list
▪ Array
O(n)
5
Hash Table
❑ Searching operation: Time Complexity
6
Terms
❑ Hash Table Vs. Hash Function Vs. Hashing
▪ Hash Table
▪ Is a data structure which is used to store key-value pairs. It is implemented as array
▪ Hash Function
▪ Is a function used by hash table to compute an index of an array in which an element will
be inserted to or be searched at
▪ Hashing
▪ In hashing, large keys are converted into smaller ones using hash function AND then the
values are stored in hash table
7
Hash Functions
8
An example of a hash function
❑ Definition
9
#include<iostream> Example: Implementation of hash table
using namespace std;
void initializeArray(){ 1 main(){
for(int i=0; i<SIZE; i++){
const int SIZE=7; ht[i] = -999 main(){ insertData(7); 2
int ht[SIZE]; }
} insertData(7); insertData(8);
int hashFunction(int n){ insertData(8); insertData(15);
return n%SIZE; insertData(25); insertData(22);
} displayHT(); insertData(25);
void insertData(int value){ } displayHT();
int index; }
index = hashFunction(value);
ht[index] = value; What is the output?
}
void displayHT(){
for(int i=0; i<SIZE; i++){
cout<<i<<"\t --> ";
cout<<ht[i]<<"\n“’
}
} Ooop! Data is overridden?
This is known as collision!
10
Collision
❑ Definition
11
Factors of a good hash function
✓ Easy to compute
✓ Minimize collision
12
Perfect Hashing
❑ Definition
(no collision)
13
Collision Resolution Techniques
(Array-based implementation)
(An array of linked list implementation)
▪ Open hashing defines each slot in the hash table to be the head of a
linked list
15
Hash Table with Chaining resolution technique
struct Element{ 1 void addEnd(List *ls, int value){ 4
int value; Element *e; int hashFunction(int n){ int main(){ 7
Element *next; e= new Element;
}; 3 return n%SIZE;
createEmptyAllLists();
e->next=NULL; }
struct List{
e->value=value; insertData(7);
int n;
Element *head, *tail;
}; if(ls->n==0){ 5 insertData(8);
const int SIZE=7; ls->head = e; void insertData(int value){ insertData(15);
List *ht[SIZE]; ls->tail = e;
int index; insertData(22);
ls->n = ls->n + 1;
}else{ index = hashFunction(value);
List* createAnEmptylist(){ 2 addEnd(ht[index], value); insertData(25);
ls->tail->next = e;
List *L1; ls->tail = e; } displayHT();
L1 = new List(); ls->n = ls->n + 1;
L1->n = 0; }
}
L1->head=NULL; void displayHT(){
}
L1->tail=NULL; Element *e;
return L1; for(int i=0; i<SIZE; i++){ 6 Output?
} cout<<i<<"\t -->";
void createEmptyAllLists(){ if(ht[i]!=NULL){
for(int i=0; i<SIZE; i++){ e = ht[i]->head;
ht[t] = createEmptyList() while(e!=0){
} cout<<e->value<<" ";
e=e->next;
}
Display data }
}
in hash table cout<<endl;
}
}
16
Solution for Hash Collision
❑ Closed Hashing
1. Linear probing
2. Quadratic probing
3. Double hashing
Case Study
-Teamwork (4-5 person/team) –using project team
-Period: 1 week
-Submit a report with code testing to G Classroom
12th December 2021
17
Q&A
18
Discussion