šŸ“š DSA Unit 2 - Premium Master Notes

Linked Lists • Trees • Hashing

Compiled & Formatted by Ankush Raj

⭐ CHAPTER 2.1 — LINKED LISTS
🌟 1. What is a Linked List? (Clear Explanation)

A Linked List is a dynamic data structure used to store a collection of elements. Unlike arrays, a linked list does not store elements in continuous memory locations.

Instead, it is made of several small parts called nodes.

šŸ”ø Each node contains:
• Data – the actual value
• Pointer (Reference) – the address of the next node

So nodes are connected through pointers, forming a chain.

Example (Imagine a chain):
[10 | next] → [20 | next] → [30 | next] → NULL
⭐ Why do we use Linked Lists?
āœ” The size can grow or shrink at runtime
āœ” No memory is wasted
āœ” Insertion and deletion are fast (no shifting like arrays)
⭐ Limitations
✘ Searching is slow → O(n)
✘ Extra memory for pointers
✘ Cannot access elements directly by index
🌟 2. Types of Linked Lists
šŸ”¹ 1) Singly Linked List (SLL)

Each node has one pointer which points to the next node.

[data | next] → [data | next] → NULL

Features:

• Can move forward only
• Simple and memory-efficient
šŸ”¹ 2) Doubly Linked List (DLL)

Each node has two pointers: one for the next node and one for the previous node.

NULL ← [prev | data | next] → [prev | data | next] → NULL

Features:

• Can move both forward and backward
• Faster deletion but uses more memory
šŸ”¹ 3) Circular Linked List (CLL)

The last node points back to the first node.

A → B → C → A (again)

Uses:

• Round-robin scheduling
• Repeated processes
🌟 3. Operations on Linked List
šŸ”µ Insertion

1. Insert at Beginning

New node → next = head
head = new node
Time: O(1)

2. Insert at End

Traverse to last node
last.next = new node
Time: O(n)

3. Insert After a Given Node

new.next = target.next
target.next = new
Time: O(1) (after finding target)
šŸ”µ Deletion

1. Delete First Node

head = head.next
Time: O(1)

2. Delete Last Node

Traverse to second-last node → next = NULL
Time: O(n)

3. Delete a Specific Value

Search + re-link pointers
Time: O(n)
šŸ”µ Searching

Visit nodes one-by-one until the element is found. Time: O(n)

🌟 4. Stack using Linked List

A stack follows LIFO (Last In First Out). The head of the linked list works as the top of the stack.

Operations:

• push() → insert at head
• pop() → delete head
• peek() → return head element

All operations: O(1)

🌟 5. Queue using Linked List

A queue follows FIFO (First In First Out). Use two pointers:

• front → first node
• rear → last node

Operations:

• enqueue() → insert at rear
• dequeue() → delete from front

All operations: O(1)

⭐ CHAPTER 2.2 — TREES
🌟 1. What is a Tree?

A tree is a non-linear, hierarchical data structure consisting of nodes connected by edges.

Basic terms:

• Root – topmost node
• Parent – node above another
• Child – node below another
• Leaf – no children
• Height – longest path from root to leaf

Trees are used to represent hierarchical structures like:

āœ” file systems
āœ” organization charts
āœ” decision structures
🌟 2. Binary Tree

A Binary Tree is a tree in which each node has at most 2 children.

Types:

• Full Binary Tree – each node has 0 or 2 children
• Complete Binary Tree – every level is filled left to right
• Perfect Binary Tree – all leaves at same level
🌟 3. Binary Search Tree (BST)

A BST is a binary tree that follows a sorted structure:

Left child < Root < Right child

Operations:

• Search → O(log n)
• Insert → O(log n)
• Delete → O(log n)
Deletion cases:
1. Node with no child

2. Node with one child

3. Node with two children → replace with inorder successor
🌟 4. Tree Traversals
šŸ”¹ Depth-First Traversals

1) Inorder (Left → Root → Right)

Gives sorted output for BST

2) Preorder (Root → Left → Right)

Used for copying tree

3) Postorder (Left → Right → Root)

Used for deleting a tree
šŸ”¹ Breadth-First Traversal (Level-order)
• Uses Queue
• Visits level by level
🌟 5. Balanced Search Trees

A balanced tree maintains height close to log n, so operations remain efficient.

Examples:

• AVL Tree
• Red-Black Tree
• B-Tree
🌟 6. Introduction to B-Tree

A B-tree is a multi-way search tree used in databases and file systems.

Features:

• One node contains multiple keys
• Children also multiple
• Height remains low → fast disk access
🌟 7. Heap and Heap Sort
šŸ”¹ Heap

A heap is a complete binary tree that satisfies:

• Max-heap: parent ≄ children
• Min-heap: parent ≤ children

Root contains the largest or smallest value.

šŸ”¹ Heap Sort Steps
1. Build a max heap
2. Swap root with last element
3. Reduce heap size
4. Heapify remaining
5. Repeat
Time:
• Build heap → O(n)
• Heap sort → O(n log n)
⭐ CHAPTER 2.3 — HASHING
🌟 1. What is Hashing?

Hashing is a technique to store data using a hash function that converts a key → index.

index = hash(key)

This allows O(1) average search, insert, delete.

Used in:

āœ” databases
āœ” hash tables
āœ” compiler symbol tables
🌟 2. Collision

A collision occurs when two keys get the same index.

🌟 3. Collision Handling Methods
šŸ”¹ 1) Chaining
• Each index stores a linked list
• Simple and widely used
šŸ”¹ 2) Open Addressing

a) Linear Probing

Check next index: h(k) + i

b) Quadratic Probing

Use squares: h(k) + i²

c) Double Hashing

Use second hash function: h1(k) + i*h2(k)
Most efficient among probing methods.
⭐ FINAL EXAM ONE-LINERS
āœ” SLL → forward only
āœ” DLL → backward + forward
āœ” Stack LL → head operations
āœ” Queue LL → front-rear
āœ” BST → left < root < right
āœ” Inorder → sorted BST
āœ” Heap → complete binary tree
āœ” Hashing → O(1) average
āœ” Chaining → linked list inside table
āœ” Double hashing → two hash functions