MODULE 5 - BCS304 - HASHING - Leftisht Trees - OBST - Notes
MODULE 5 - BCS304 - HASHING - Leftisht Trees - OBST - Notes
HASHING
Hashing in Data Structures
Hashing is a technique used to map data to a fixed-size table using a hash function. The primary
purpose of hashing is to enable efficient data retrieval in constant or near-constant time.
1. Hash Table:
o A data structure that stores key-value pairs.
o Keys are mapped to indices in the table using a hash function.
2. Hash Function:
o A function that converts a key into an index in the hash table.
o Ideal properties of a hash function:
▪ Uniform distribution of keys.
▪ Minimizes collisions.
3. Collisions:
o Occur when two keys map to the same index in the hash table.
o Collision resolution techniques are used to handle these situations.
4. Load Factor:
o The ratio of the number of stored keys to the size of the hash table.
o Helps determine when to resize the table to maintain efficiency.
1. Direct Hashing
• The hash function directly maps the key to a unique table index.
• Example: index = key % table_size.
• No collisions occur if keys are unique and within the table's range.
2. Open Hashing (Separate Chaining)
• Each index in the hash table stores a linked list (or another structure) of keys that map to
that index.
• Advantages:
o Handles collisions efficiently.
o Easy to implement.
• Disadvantages:
o Can lead to long chains if many keys collide.
o Requires additional memory for linked lists.
• Collisions are resolved by finding another open slot within the hash table.
• Common methods:
o Linear Probing: Increment the index sequentially until an empty slot is found.
▪ Pros: Simple to implement.
▪ Cons: Clustering can occur.
o Quadratic Probing: Increment the index using a quadratic function.
▪ Pros: Reduces clustering compared to linear probing.
▪ Cons: Still prone to secondary clustering.
o Double Hashing: Use a secondary hash function to compute the increment.
▪ Pros: Minimizes clustering.
▪ Cons: More complex to implement.
Applications of Hashing
1. Databases:
o Indexing for faster data retrieval.
2. Caching:
o Fast access to frequently used data.
3. Cryptography:
o Storing passwords securely.
4. Compiler Design:
o Symbol table implementation.
5. Networking:
o Hash-based routing in distributed systems.
In static hashing, the size of the hash table is fixed and does not change after the
hash table is created.
Characteristics:
Advantages:
1. Simple to implement.
2. Predictable memory requirements since the table size is fixed.
Disadvantages:
2. Dynamic Hashing
In dynamic hashing, the hash table grows or shrinks dynamically based on the
number of elements stored. This technique is useful when the dataset size is
unknown or can change significantly over time.
Characteristics:
• The hash function and table size adapt to the current load.
• Commonly used in database systems to handle large, varying datasets.
• Load Factor: When it exceeds a threshold, the table resizes.
1. Extendible Hashing:
o Uses a directory with pointers to buckets.
o The hash function creates a binary hash, and the number of bits used
depends on the current table size.
o If a bucket overflows, only the bucket is split, and the directory size
may double.
2. Linear Hashing:
o Expands the hash table incrementally without using a directory.
o Splits buckets as needed, based on a split pointer.
o Helps avoid sudden jumps in memory usage.
Advantages:
Disadvantages:
Collision
Requires explicit strategies Buckets split or table resizes
Handling
Memory Usage Wastes memory if underutilized Optimized for current data size
Just An Idea
◼ Hash table :
◼ Collection of pairs,
◼ Lookup function (Hash function)
◼ Hash tables are often used to implement
associative arrays,
◼ Worst-case time for Get, Insert, and Delete
is O(size).
◼ Expected time is O(1).
Search vs. Hashing
◼ Search tree methods: key comparisons
◼ Time complexity: O(size) or O(log n)
◼ Hashing methods: hash functions
◼ Expected time: O(1)
◼ Types
◼ Static hashing (section 8.2)
◼ Dynamic hashing (section 8.3)
Static Hashing
◼ Key-value pairs are stored in a fixed size table
called a hash table.
◼ A hash table is partitioned into many buckets.
◼ Each bucket has many slots.
◼ Each slot holds one record.
◼ A hash function f(x) transforms the identifier (key)
into an address in the hash table
void main()
{
int a[MAX],num,key,i;
int ans=1;
printf(" collision handling by linear probing : \n");
for (i=0;i<MAX;i++)
{
a[i] = -1;
}
do
{
printf("\n Enter the data");
scanf("%4d", &num);
key=create(num);
linear_prob(a,key,num);
printf("\n Do you wish to continue ? (1/0) ");
scanf("%d",&ans);
}while(ans);
display(a);
}
int create(int num)
{
int key;
key=num%100;
return key;
}
printf("Collision avoided successfully using LINEAR PROBING\n");
if(count == MAX)
{
printf("\n Hash table is full");
display(a);
exit(1);
}
for(i=key+1; i<MAX; i++)
if(a[i] == -1)
{
a[i] = num;
flag =1;
break;
}
//for(i=0;i<key;i++)
i=0;
while((i<key) && (flag==0))
{
if(a[i] == -1)
{
a[i] = num;
flag=1;
break;
i++;
}
}
else
{
printf("\n the hash table is\n");
for(i=0; i<MAX; i++)
if(a[i]!=-1)
{
printf("\n %d %d ", i, a[i]);
12_Hashing .txt
continue;
}
}
Ideal Hashing
Some Issues
◼ Requirements
◼ easy to compute
◼ minimal number of collisions
◼ If a hashing function groups key values
together, this is called clustering of the
keys.
◼ A good hashing function distributes the
key values uniformly throughout the
range.
◼ Middle of square
◼ H(x):= return middle digits of x^2
◼ Division
◼ H(x):= return x % k
◼ Multiplicative:
◼ H(x):= return the first few digits of the
fractional part of x*k, where k is a fraction.
◼ advocated by D. Knuth in TAOCP vol. III.
Hashing By Division
#define SIZE 5
int PQ[SIZE],front=0,rear=-1;
void insert()
{
int ele,j;
if(rear==SIZE-1)
{
printf("Priority Queue is Full\n");
}
else
{
printf("Enter the element to Insert\n");
scanf("%d",&ele);
j=rear;
while(ele>PQ[j]&&j>=0)
{
PQ[j+1]=PQ[j];
j--;
}
PQ[j+1]=ele;
rear=rear+1;
}
}
void delet()
{
int item;
if(front>rear)
{
printf("Priority Queue is Empty\n");
}
else
{
item=PQ[front];
front=front+1;
printf("The deleted Element is %d\n",item);
}
}
void display()
{
int i;
if(front>rear)
{
printf("Proirity Queue Underflow\n");
}
else
{
printf("The elements in Priority Queue are\n");
for(i=front;i<=rear;i++)
printf("%d\t",PQ[i]);
}
}
void main()
{
int ch=1;
clrscr();
while(ch)
{
printf(" 1.Insert 2.Delete 3.Display 4.Exit \n");
printf("Enter your Choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1: insert();
break;
case 2: delet();
break;
case 3 :display();
break;
case 4 :exit(0);
default: printf("Enter valid choice\n");
break;
}
}
}
#define SIZE 5
int PQ[SIZE],front=0,rear=-1;
void insert()
{
int ele,j;
if(rear==SIZE-1)
{
printf("Priority Queue is Full\n");
}
else
{
printf("Enter the element to Insert\n");
scanf("%d",&ele);
j=rear;
while(ele<PQ[j]&&j>=0)
{
PQ[j+1]=PQ[j];
j--;
}
PQ[j+1]=ele;
rear=rear+1;
}
}
void delet()
{
int item;
if(front>rear)
{
printf("Priority Queue is Empty\n");
}
else
{
item=PQ[front];
front=front+1;
printf("The deleted Element is %d\n",item);
}
}
void display()
{
int i;
if(front>rear)
{
printf("Proirity Queue Underflow\n");
}
else
{
printf("The elements in Priority Queue are\n");
for(i=front;i<=rear;i++)
printf("%d\t",PQ[i]);
}
}
void main()
{
int ch=1;
clrscr();
while(ch)
{
printf(" 1.Insert 2.Delete 3.Display 4.Exit \n");
printf("Enter your Choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1: insert();
break;
case 2: delet();
break;
case 3 :display();
break;
case 4 :exit(0);
default: printf("Enter valid choice\n");
break;
}
}
}
r .~ .. c.,-o<J • • . '
L
~ ~
' t
□
l l
D
D
l
o
O
0
b 7) D
[
'tl
1) 0
L.1,W-,- ~ ("1->-j_
/'--~" lPth't' 'b~'1 ~
i~ \} ~ o-,{~
'tf cJ.; lJ_ '0ocU. ~
!)'?_·
I\ )\~~ 91
2- <: [
I\ \)
1 /r,
t I
1
'},. J l} I to l J
I ~ I 9-1< I ~)) [
l )
o (5 b 6 tJ
\) I O 1)
rJ
0
ti 0
G
0 o
o- () 0 () D
'
1
3 0
..3
- 2- 'i- ~ ~
-- 3 l;; 2-
~ ~ 3bJ 2-o) lo
t~)
~ ~ d-,o Jlo,3-0
~vltx
,
¥ ~,·1 "~
il, n~-3
~
• I
' '
........ ,
_)
4-' 1 --.,:._t--+---L_J
f ,
-~
•
s , + ~ ~ -l-
it7;:;--,
~
R[,·,;J ~ , 0
..
. g C; ,j ) k .. . ;!
. ~(\
' . . .
' .
-,~ I 2- ~ 4-
'
0.7
'1---- 3
I •5""""
4-
...
2.. . ,6 , 0 I 2-
2-
~
2-
q-·
-3
1- 2-
, .o. ~ .Q -3 3
J j
4- '
·r1>. "
4- - •
+
-:! s -
Q)
-- ·----- ------·- ---·------
~
, I
' I J
C [ L4-) ; ""'" c[s J.].,-c[414]-r P3+ P,-, 1?
1
K ~ ::>
jl : 4 c[3,3}t ,c~-,4] + r1 tf'( ~
, .
@ cc,';~];,
1
t--'C'\ c[t1 o]+c{j,1]+{ P,-+Pl.--.+P~
~ ~,
t~1-- c[f11j,t--C~3,))f',, +pz._+fJ_,,~
~3
c. [r, 1-) ,t c_[4- ~ 1) +P, +p,__+f!J
.
~ "'-'-'~ \ 0 f f, 0 f 0. 1-· +O•S f 0, 4-,J
l. o.1.- ;- o. 4 + o .l- fo .3 {- 0.4-j
. ,) · e,.7 + o +o . 'L-+ o. '1 +o:lf-
:- ~" ( i ,_'11 .1s,, ,. q.
c_ [1/1) ; (. ~ ~ .. ~ 9--
7
. ~h'-' ~ i,.-, ~ T~
"-l_av_~1' '~ ~ '1f- ~ - - -
2- J
,
0 ,
2- 3 4-
r I, s- J._,'
0•7
'- D,~ I ,o 2-, 0 3 3
3 0, lf- I,~ ~ 3 q..
-1---1------l~__µ,..._-+--'------;
4- 0,) 4- L-L-1--+--i~,
s-- ~ L---,.___._--L..-__,__~
11 I