0% found this document useful (0 votes)
5 views34 pages

DS Revision On Heap

The document provides an overview of data structures, specifically focusing on Heaps and Hashing techniques. It explains the properties and operations of Heaps, including insertion, deletion, and heapify operations, as well as the complexities and applications of heaps. Additionally, it covers hashing methods, collision resolution techniques (linear, quadratic, and double hashing), the significance of load factors, and rehashing strategies.

Uploaded by

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

DS Revision On Heap

The document provides an overview of data structures, specifically focusing on Heaps and Hashing techniques. It explains the properties and operations of Heaps, including insertion, deletion, and heapify operations, as well as the complexities and applications of heaps. Additionally, it covers hashing methods, collision resolution techniques (linear, quadratic, and double hashing), the significance of load factors, and rehashing strategies.

Uploaded by

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

1.2.

Review of elementary Data Structures


Heaps
A Heap is a special Tree-based Data
Structure that has the following properties.
 It is a complete Complete Binary Tree.
 It either follows max heap or min heap

property.
Max-Heap: The value of the root node must be
the greatest among all its descendant nodes
and the same thing must be done for its left and
right sub-tree also.
Min-Heap: The value of the root node must
be the smallest among all its descendant
nodes and the same thing must be done for
its left and right sub-tree also.
Operations on Heaps
Insertion:
•Insert the new element at the end of the heap
(maintaining completeness).
Deletion (typically root deletion):
 Replace the root with the last element in the

heap.
Peek:
•Access the root element (max in max-heap or min in
min-heap) without removing it.
Heapify Operation:
 Used to convert an arbitrary array into a heap.
Heap Complexities
Heap Application

Priority Queues:
Sorting (Heapsort)
Graph Algorithms:
Hashing in Data Structure
Hashing is a technique used in data structures that
efficiently stores and retrieves data in a way that
allows for quick access. It involves mapping data
to a specific index in a hash table using a hash
function that enables fast retrieval of information
based on its key.
 The great thing about hashing is, we can achieve all three
operations (search, insert and delete) in O(1) time on
average.
Collision Resolution

The following techniques are used in open


addressing:

1.Linear probing
2. Quadratic probing
3.Double hashing
1. Linear probing:
This involves doing a linear probe for the
following slot when a collision occurs and
continuing to do so until an empty slot is
discovered.
The worst time to search for an element in
linear probing is O(n). The cache performs
best with linear probing, but clustering is a
concern. This method’s key benefit is that it
is simple to calculate.
 Main Idea: When collision occurs, scan down
the array one cell at a time looking for an
empty cell
◦ hi(X) = (Hash(X) + i) mod TableSize (i = 0, 1,
2, …)
◦ Compute hash value and increment it until a
free cell is found
2. Quadratic probing:
When a collision happens in this, we
probe for the i2 slot in the i iteration,
th

continuing to do so until an empty slot


is discovered. In comparison to linear
probing, quadratic probing has a
worse cache performance.
Additionally, clustering is less of a
concern with quadratic probing.
Quadratic Probing
Main Idea: Spread out the search for an empty
slot
Increment by i2 instead of i
 hi(X) = (Hash(X) + i2) % TableSize
h0(X) = Hash(X) % TableSize
h1(X) = Hash(X) + 1 % TableSize
h2(X) = Hash(X) + 4 % TableSize
h3(X) = Hash(X) + 9 % TableSize
The load factor in a hash table, including those
using quadratic probing, measures how full the
table is. It is calculated as:
Role of Load Factor in Quadratic
Probing
Load Factor Threshold:
Hash tables generally perform well with load factors up
to around 0.5 to 0.7 for open addressing techniques
like quadratic probing.
When the load factor is high (close to 1), it becomes
more difficult to find an open slot due to increased
clustering, leading to slower insertion and search times.
Rehashing:
 When the load factor reaches a certain threshold (e.g.,

0.5 or 0.7), many hash tables trigger rehashing.


Example: Suppose we have:A hash table with a
capacity of 10 slots.4 elements already
inserted.
The load factor would be:

As long as the load factor remains under the threshold


(e.g., 0.7), new elements can generally be inserted with
reasonable efficiency using quadratic probing. When the
load factor approaches the threshold, rehashing may be
performed to maintain performance.
3. Double hashing:
In this, you employ a different hashing algorithm, and in the i th iteration, you look for
(i * hash 2(x)). The determination of two hash functions requires more time. Although
there is no clustering issue, the performance of the cache is relatively poor when using
double probing.
Idea: Spread out the search for an empty slot by using a second hash function.
◦ No primary or secondary clustering
 hi(X) = (Hash1(X) + iHash2(X)) mod TableSize
for i = 0, 1, 2, …
Good choice of Hash2(X) can guarantee does not get “stuck” as long as  < 1
◦ Integer keys:
Hash2(X) = R – (X mod R)
where R is a prime smaller than Table Size
Example on Double Hashing
Table size: 10
•Primary hash function: h1(k)=kmod 10
•Secondary hash function: h2(k)=1+(kmod 9)
•Elements to insert: 15, 25, 35
 Insert 15:

 Calculate h1(15)=15mod 10=5

Index 5 is empty, so place 15 at index 5.


Insert 25:
•Calculate h1(25)=25mod 10=5
Collision occurs at index 5 (already occupied by 15).
•Use the secondary hash function to get the step size:
h2(25)=1+(25mod 9)=1+7=8
•First probe: (5+1×8)mod 10=13mod 10=3
•Index 3 is empty, so place 25 at index 3.
 Insert 35:
 Calculate h1(35)=35mod 10=5

Collision at index 5 (occupied by 15).


 Calculate step size with h2(35)=1+(35mod

9)=1+8=9
First probe: (5+1×9)mod 10=14mod 10=4
Index 4 is empty, so place 35 at index 4.
Benefits of Double Hashing
Less Clustering
Efficiency: With an appropriate choice of
h2(k)h_2(k)h2​(k), double hashing can achieve
good distribution and fewer probes per
insertion/search.
Load Factor Double Hashing

Significance of Load Factor in Double Hashing


Maintaining Efficiency:
Ideal Load Factor Range:
Rehashing at High Load Factor:
Lower load factors (ideally λ≤0.5\lambda /help maintain efficiency.
Higher load factors increase the chance of collisions,
Rehashing
Set Representation
SETS Representation ,UNION & FIND operation.

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