Hashing
(Concept & Techniques)
What is Hashing?
• Hashing is a technique that maps data of arbitrary size →
fixed-size value (index) using a hash function.
• Enables fast insertion, search, deletion.
Hashing Techniques
A. Open Addressing (In-Table Collisions)
• Linear Probing: h'(key, i) = (h(key) + i) % table_size
• Quadratic Probing: h'(key, i) = (h(key) + i^2) % table_size
• Double Hashing: h'(key, i) = (h1(key) + i*h2(key)) % table_size
B. Chaining
• Each index holds a linked list of colliding keys.
C. Perfect Hashing
• Maps n keys n slots
→ without collisions.
• Ideal for static datasets.
Hash Table
• Array-like structure using hash function to store keys.
Features:
Fast operations: O(1) average time
Requires collision handling
Pseudo Code (Insertion using Chaining):
Hash Function
Converts key index
→
Properties:
• Fast
• Uniform
• Deterministic
Common Types:
• Division: h(key) = key % table_size
• Multiplication: h(key) =
floor(table_size * (key * A % 1))
• String Hashing: ASCII sum,
polynomial rolling hash
Advantages of Hashing Techniques
• Fast Access: O(1) average for search, insert, delete
• Efficient Memory: Optimized storage if table size chosen
well
• Flexible: Can store different types of keys
• Widely Used: Databases, caching, symbol tables
Open Addressing Advantages:
• Simple structure, no extra memory
for linked lists
• Cache-friendly
• Easy to implement
Perfect Hashing Advantages:
• No collisions O(1) search
→
guaranteed
• Ideal for static datasets
Disadvantages of Hashing
Techniques
• Collisions require handling (chaining or probing)
• Poor hash function clustering & uneven distribution
→
• Fixed-size table overflow or need for resizing
→
• Cannot maintain sorted order of elements
• Open addressing: performance degrades with high load
factor
Applications of Hashing
• Database Indexing – fast retrieval of records
• Symbol Tables – in compilers and interpreters
• Caching – storing frequently accessed data
• Password Storage – hashing + salting
• Cryptography – MD5, SHA algorithms
• Sets & Maps – key-value pairs in programming languages
AVL Tree
• Self-balancing Binary Search
Tree (BST)
• Balance Factor (BF) =
height(left) - height(right)
• Rotations: LL, RR, LR, RL
Pseudo Code (Insertion):
Red- Black Tree
• Self-balancing BST using colors (Red/Black)
Properties:
Root is black
Red nodes cannot have red children
Every path from root leaf has same black height
→
Node Structure:
Rotations
Left Right
BST Insertion Red-Black Tree Insertion
Fixing Violations After Insertion
Binomial Heap
• A Binomial Heap is a collection of Binomial Trees that
satisfies the Min-Heap (or Max-Heap) property.
• Each tree in the heap follows heap-order property (parent
children for min-heap).
≤
• Efficient for mergeable priority queues.
Properties:
oA Binomial Tree of order k has 2^k nodes.
oHeight of tree = k
oNumber of nodes at depth i = C(k, i) (binomial coefficient)
Basic Operations
Merge Two Heaps Insert Key
Find Minimum Delete Minimum
Fibonacci Heap
• A Fibonacci Heap is a collection of heap-ordered trees with
a relaxed structure, allowing very fast amortized
operations.
• Optimized for algorithms that require lots of decrease-key
operations, like Dijkstra’s algorithm.
Properties:
oEach tree satisfies min-heap property (parent children).
≤
oNodes are linked circularly in a root list
oMaintains pointer to the minimum node for O(1) access.
oLazy consolidation — trees are merged only when
necessary.
Basic Operations
Insert Node Find Minimum
Extract Minimum
Decrease Key
Array, Structure and Binary List
Array: Contiguous elements, random access O(1)
Structure: Groups multiple data types
Binary List: Efficient 0/1 storage used in sets, bitmap
indexing
Hashing (Concept & Techniques)- Data Structure

Hashing (Concept & Techniques)- Data Structure

  • 1.
  • 2.
    What is Hashing? •Hashing is a technique that maps data of arbitrary size → fixed-size value (index) using a hash function. • Enables fast insertion, search, deletion.
  • 3.
    Hashing Techniques A. OpenAddressing (In-Table Collisions) • Linear Probing: h'(key, i) = (h(key) + i) % table_size • Quadratic Probing: h'(key, i) = (h(key) + i^2) % table_size • Double Hashing: h'(key, i) = (h1(key) + i*h2(key)) % table_size B. Chaining • Each index holds a linked list of colliding keys. C. Perfect Hashing • Maps n keys n slots → without collisions. • Ideal for static datasets.
  • 4.
    Hash Table • Array-likestructure using hash function to store keys. Features: Fast operations: O(1) average time Requires collision handling Pseudo Code (Insertion using Chaining):
  • 5.
    Hash Function Converts keyindex → Properties: • Fast • Uniform • Deterministic Common Types: • Division: h(key) = key % table_size • Multiplication: h(key) = floor(table_size * (key * A % 1)) • String Hashing: ASCII sum, polynomial rolling hash
  • 6.
    Advantages of HashingTechniques • Fast Access: O(1) average for search, insert, delete • Efficient Memory: Optimized storage if table size chosen well • Flexible: Can store different types of keys • Widely Used: Databases, caching, symbol tables Open Addressing Advantages: • Simple structure, no extra memory for linked lists • Cache-friendly • Easy to implement Perfect Hashing Advantages: • No collisions O(1) search → guaranteed • Ideal for static datasets
  • 7.
    Disadvantages of Hashing Techniques •Collisions require handling (chaining or probing) • Poor hash function clustering & uneven distribution → • Fixed-size table overflow or need for resizing → • Cannot maintain sorted order of elements • Open addressing: performance degrades with high load factor
  • 8.
    Applications of Hashing •Database Indexing – fast retrieval of records • Symbol Tables – in compilers and interpreters • Caching – storing frequently accessed data • Password Storage – hashing + salting • Cryptography – MD5, SHA algorithms • Sets & Maps – key-value pairs in programming languages
  • 9.
    AVL Tree • Self-balancingBinary Search Tree (BST) • Balance Factor (BF) = height(left) - height(right) • Rotations: LL, RR, LR, RL Pseudo Code (Insertion):
  • 11.
    Red- Black Tree •Self-balancing BST using colors (Red/Black) Properties: Root is black Red nodes cannot have red children Every path from root leaf has same black height → Node Structure:
  • 12.
  • 13.
  • 14.
  • 15.
    Binomial Heap • ABinomial Heap is a collection of Binomial Trees that satisfies the Min-Heap (or Max-Heap) property. • Each tree in the heap follows heap-order property (parent children for min-heap). ≤ • Efficient for mergeable priority queues. Properties: oA Binomial Tree of order k has 2^k nodes. oHeight of tree = k oNumber of nodes at depth i = C(k, i) (binomial coefficient)
  • 16.
    Basic Operations Merge TwoHeaps Insert Key Find Minimum Delete Minimum
  • 17.
    Fibonacci Heap • AFibonacci Heap is a collection of heap-ordered trees with a relaxed structure, allowing very fast amortized operations. • Optimized for algorithms that require lots of decrease-key operations, like Dijkstra’s algorithm. Properties: oEach tree satisfies min-heap property (parent children). ≤ oNodes are linked circularly in a root list oMaintains pointer to the minimum node for O(1) access. oLazy consolidation — trees are merged only when necessary.
  • 18.
    Basic Operations Insert NodeFind Minimum Extract Minimum Decrease Key
  • 19.
    Array, Structure andBinary List Array: Contiguous elements, random access O(1) Structure: Groups multiple data types Binary List: Efficient 0/1 storage used in sets, bitmap indexing