0% found this document useful (0 votes)
7 views25 pages

10 Hash Table

The document provides an overview of hash tables, including their definition, time complexity, and comparison with other data structures. It discusses hashing, hash functions, and collision resolution techniques such as open and closed hashing. Additionally, it includes examples and code snippets for implementing hash tables in C++.

Uploaded by

monycc2024
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views25 pages

10 Hash Table

The document provides an overview of hash tables, including their definition, time complexity, and comparison with other data structures. It discusses hashing, hash functions, and collision resolution techniques such as open and closed hashing. Additionally, it includes examples and code snippets for implementing hash tables in C++.

Uploaded by

monycc2024
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Data structure and

Programming
(C++)
Hash Table

1
About array ….

▪ Assume that we have an array


int a[ ] = {4, 7, 10, 0, 9, 100, 50, 80, 25, 56, 90, 17};

We can use for loop to access data in this array.


Suppose that we want to search for a number and tell the user whether that
number is exist or not.
A. Let’s search for the number 90 in this array. How many times did you use the comparison operation?
B. Let’s search for the number 10 in this array. How many times did you use the comparison operation?
C. Let’s search for the number 99 in this array. How many times did you use the comparison operation?

By using hash table, we compare only 1 time in order to search. 2


Outline

▪ Time complexity

▪ What is Hashing? Hash table? Hash function?

▪ Collision

▪ Collision resolution techniques


▪ Open hashing

▪ Close hashing

▪ Examples
3
Time complexity

Time complexity is a time required for an algorithm to run.


It is denoted by a big-O notation: O(n) ▪ Best case
▪ Average case
▪ Worst case
▪ Linked list : O(n)

▪ Array (unsorted) : O(n) Searching time


complexity
▪ Array (sorted) : O(log2 n)

4
Big-O Notation
❑ Introduction
▪ Introduction about Big-O Notation: bit.ly/312x9ou

5
Other Data structure
❑ Searching operation

▪ Linked list

▪ Array

▪ Stack What is the time required


to search ?
▪ Queue

O(n)
6
Hash Table
❑ Searching operation: Time Complexity

Hash Table : O(1)

7
General rules for time complexity

8
9
10
11
12
13
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.

14
Hash Functions

▪ A hash function usually means a function that compresses.


▪ The output is shorter than the input

15
An example of a hash function
❑ Definition

int hash(int key){


Size of array
return key%7;
}

16
#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!
17
Collision
❑ Definition

▪ A situation when two or more data hash to be stored in the same


location in the table, is called a hash collision.

• Suppose we have a hash table with 7 elements


• Hash function is to modulo with 7
• How to insert these numbers?
• 15, 11, 27, 8

18
Factors of a good hash function

✓ Easy to compute

✓ Minimize collision

19
Perfect Hashing
❑ Definition

▪ Perfect hashing maps each valid input to a different hash value

(no collision)

20
Collision Resolution Techniques

(Array-based implementation)
(An array of linked list implementation)

An example of using chaining


21
Solution for Hash Collision
❑ Open Hashing

▪ Open hashing defines each slot in the hash table to be the head of a
linked list

▪ All records that hash to a particular slot (collision cases) are


placed on that slot’s linked list

22
Hash Table with Chaining resolution technique (An array of linked list)

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;
}
}
23
Solution for Hash Collision
❑ Closed Hashing

1. Linear probing

2. Quadratic probing

3. Double hashing

Case Study
Try these topics and explore how each method works!

24
Q&A
25

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy