DS Revision On Heap
DS Revision On 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
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
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