Complete DS Course Notes
Complete DS Course Notes
Assistant Professor
IIITM Gwalior
January 1, 2026
Dr. Dheeraj Kodati (Assistant Professor IIITM Gwalior) Data Structures January 1, 2026 1 / 13
What is a Data Structure?
Dr. Dheeraj Kodati (Assistant Professor IIITM Gwalior) Data Structures January 1, 2026 2 / 13
Why Study Data Structures?
Dr. Dheeraj Kodati (Assistant Professor IIITM Gwalior) Data Structures January 1, 2026 3 / 13
Role of C in Data Structures
Dr. Dheeraj Kodati (Assistant Professor IIITM Gwalior) Data Structures January 1, 2026 4 / 13
Basic Types of Data Structures
Primitive
int, char, float, double
Non-Primitive
Arrays, Linked Lists, Stacks, Queues, Trees, Graphs
Dr. Dheeraj Kodati (Assistant Professor IIITM Gwalior) Data Structures January 1, 2026 5 / 13
Linear Data Structures
Array
Linked List
Stack
Queue
Dr. Dheeraj Kodati (Assistant Professor IIITM Gwalior) Data Structures January 1, 2026 6 / 13
Non-Linear Data Structures
Trees
Binary Search Trees
Graphs
Heaps
Dr. Dheeraj Kodati (Assistant Professor IIITM Gwalior) Data Structures January 1, 2026 7 / 13
Program Correctness and Efficiency
Dr. Dheeraj Kodati (Assistant Professor IIITM Gwalior) Data Structures January 1, 2026 8 / 13
Searching Techniques
Linear Search
Binary Search
Hash-based Searching
Dr. Dheeraj Kodati (Assistant Professor IIITM Gwalior) Data Structures January 1, 2026 9 / 13
Sorting Techniques
Bubble Sort
Selection Sort
Insertion Sort
Merge and Quick Sort
Dr. Dheeraj Kodati (Assistant Professor IIITM Gwalior) Data Structures January 1, 2026 10 / 13
Applications of Data Structures
Operating Systems
Databases
Compiler Design
Machine Learning and AI
Dr. Dheeraj Kodati (Assistant Professor IIITM Gwalior) Data Structures January 1, 2026 11 / 13
About the Instructor
Dr. Dheeraj Kodati (Assistant Professor IIITM Gwalior) Data Structures January 1, 2026 12 / 13
Thank You
Dr. Dheeraj Kodati (Assistant Professor IIITM Gwalior) Data Structures January 1, 2026 13 / 13
Comprehensive Python Programming
Foundations of Python
1
1. Why Python for Research?
2
2. Dynamic Typing & Variables
# Multiple Assignment
a , b , c = 5 , 10 , 15
3
3. String Interpolation (f-Strings)
4
4. Conditional Logic
# Ternary Operator
result = " Pass " if 85 > 40 else " Fail "
5
5. Iteration: For Loops
6
6. Iteration: While Loops
count = 5
while count > 0:
print ( f " Countdown : { count } " )
count -= 1 # No count - - in Python
7
7. Lists: The Workhorse
8
8. Tuples & Sets
# Set operations
A = {1 , 2 , 3}; B = {3 , 4 , 5}
print ( A | B ) # Union {1 , 2 , 3 , 4 , 5}
9
9. Dictionaries (JSON-like)
paper = {
" title " : " Explainable AI " ,
" year " : 2024 ,
" citations " : 150
}
10
10. List Comprehensions
# Traditional way
evens = []
for x in range (10) :
if x % 2 == 0:
evens . append ( x **2)
# Pythonic way
evens = [ x **2 for x in range (10) if x % 2 == 0]
11
11. Functions & Default Arguments
12
12. Lambda & Functional Tools
13
13. Global vs Local Scope
x = 100 # Global
def func () :
global x
x = 200 # Modifies global variable
y = 50 # Local
func ()
print ( x ) # 200
14
14. OOP: Classes and Objects
class Agent :
def __init__ ( self , name , task ) :
self . name = name # Attribute
self . task = task
15
15. OOP: Inheritance
16
16. Robust Error Handling
try :
with open ( " config . json " ) as f :
data = f . read ()
except F i leN otF oun dErr o r :
print ( " Error : File missing . " )
except Exception as e :
print ( f " Unexpected error : { e } " )
17
17. Generators (Memory Efficient)
gen = c o unt_to_million ()
print ( next ( gen ) ) # 1
18
18. Decorators
@my_decorator
def say_hello () :
print ( " Hello ! " )
19
19. Intro to NumPy (Numerical Python)
import numpy as np
20
Thank you
Thank You!
Email: dheeraj@[Link]
21
Program Correctness and Analysis
Data Structures and Algorithms
Dr. Dheeraj
Assistant Professor
ABV-IIITM Gwalior
February 2026
{P} S {Q}
{P}: Pre-condition
S: The Code/Statement
{Q}: Post-condition
A Loop Invariant is a statement about the variables that stays true before
and after every iteration.
The Analogy: Think of climbing a ladder. Your ”invariant” is that
your hands are always on a rung. Whether you are at the bottom,
middle, or top, that truth never changes.
sum ← 0, i ← 1
while i ≤ n do
sum ← sum + i
i ←i +1
end while
Pi−1
Invariant: At the start of each loop, sum = j=1 j.
At Termination: i = n + 1.
Pn
Therefore, sum = j=1 j. (Correct!)
Analogy
If a car’s top speed is 200 km/h:
O(200): Speed is ≤ 200.
Ω(10): Speed is ≥ 10 (it’s moving).
Θ(x): The car is cruising at exactly x.
Questions?
January 6, 2026
Definition
A Data Type defines:
The type of data
The operations allowed on that data
Examples:
Integer: addition, subtraction
Character: comparison, assignment
Key Idea
ADT separates what an operation does from how it is implemented.
Definition
An Abstract Data Type is a mathematical model that defines:
A set of values
A set of operations on those values
Stack
Queue
List
Deque
Set
Map (Dictionary)
Definition
A Stack is an ADT that follows the LIFO principle.
Operations:
push()
pop()
peek()
isEmpty()
Pseudo Code
PUSH(stack, item):
[Link] ← [Link] + 1
stack[[Link]] ← item
Example Output
Input: PUSH(10), PUSH(20)
Stack Content: [10, 20]
Pseudo Code
POP(stack):
if [Link] == -1:
return "Stack Underflow"
item ← stack[[Link]]
[Link] ← [Link] - 1
return item
Example Output
Stack before: [10, 20]
POP()
Output: 20
Stack after: [10]
Definition
A Queue is an ADT that follows the FIFO principle.
Operations:
enqueue()
dequeue()
front()
isEmpty()
Pseudo Code
ENQUEUE(queue, item):
[Link] ← [Link] + 1
queue[[Link]] ← item
Example Output
ENQUEUE(5), ENQUEUE(15)
Queue Content: [5, 15]
Pseudo Code
DEQUEUE(queue):
if [Link] > [Link]:
return "Queue Empty"
item ← queue[[Link]]
[Link] ← [Link] + 1
return item
Example Output
Queue before: [5, 15]
DEQUEUE()
Output: 5
Queue after: [15]
Definition
A List is an ordered collection of elements.
Operations:
insert(position, element)
delete(position)
retrieve(position)
size()
Pseudo Code
Example Output
List before: [1, 2, 4]
INSERT(3, 3)
List after: [1, 2, 3, 4]
Improves modularity
Enhances code reusability
Simplifies maintenance
Implementation independent
IIITM Gwalior
Syntax:
data type array name[size]
Example:
int marks[5];
int main () {
int marks [5] = {85 , 90 , 78 , 92 , 88};
printf ( " % d " , marks [2]) ;
return 0;
}
Output:
78
int main () {
int i ;
int a [5] = {10 , 20 , 30 , 40 , 50};
Output:
10 20 30 40 50
int main () {
int a [10] = {10 , 20 , 30 , 40 , 50};
int n = 5 , i , pos = 2 , value = 25;
a [ pos ] = value ;
n ++;
Output:
10 20 25 30 40 50
int main () {
int a [5] = {10 , 20 , 30 , 40 , 50};
int i , pos = 2;
Output:
10 20 40 50
Dr. Dheeraj Kodati (IIITM Gwalior) Arrays in Data Structures 8 / 10
Practice Questions
Dr. Dheeraj
Lecture Slides
January 8, 2026
1 Introduction to Lists
2 Array vs Linked List
3 Linked List Concept
4 Singly Linked List
5 Traversal Operation
6 Insertion Operations
7 Deletion Operation
8 Time Complexity
9 Advantages and Disadvantages
10 Practice Questions
Array List
Singly Linked List
Doubly Linked List
Circular Linked List
|Data| → |Next|
Note: The last node always points to NULL.
temp = head
while temp != NULL
print [Link]
temp = [Link]
Output:
10 20 30
Steps:
Create new node
Point new node to head
Update head
[Link] = head
head = new
Example:
Before:
10 → 20
After:
5 → 10 → 20
Steps:
Traverse to last node
Attach new node
temp = head
while [Link] != NULL
temp = [Link]
[Link] = new
[Link] = NULL
Result:
10 → 20 → 30
Insert at position = 2
temp = head
for i = 1 to pos-1
temp = [Link]
[Link] = [Link]
[Link] = new
Example:
10 → 15 → 20
Steps:
Search the node
Adjust links
Free memory
temp = head
prev = NULL
while [Link] != key
prev = temp
temp = [Link]
[Link] = [Link]
Traversal: O(n)
Insertion at beginning: O(1)
Insertion at end: O(n)
Deletion: O(n)
Dynamic size
Easy insertion and deletion
Efficient memory usage
Dr. Dheeraj
Lecture Slides
January 8, 2026
1 Introduction
2 Types of Lists
3 Array List
4 Singly Linked List
5 Doubly Linked List
6 Circular Singly Linked List
7 Circular Doubly Linked List
8 Comparison
9 Practice Questions
1 Array List
2 Singly Linked List
3 Doubly Linked List
4 Circular Singly Linked List
5 Circular Doubly Linked List
for i = 0 to n-1
print A[i]
Output:
10 20 30
temp = head
while temp != NULL
print [Link]
temp = [Link]
Output:
10 20 30
temp = head
while temp != NULL
print [Link]
temp = [Link]
Output:
10 20 30
temp = head
do
print [Link]
temp = [Link]
while temp != head
Output:
10 20 30
temp = head
do
print [Link]
temp = [Link]
while temp != head
Output:
10 20 30
Introduction to Stack
Stack Operations
Stack Implementation
Algorithms (Push, Pop, Peek)
Examples with Output
Problem Solving Examples
Practice Questions
Stack of plates
Undo/Redo operations
Function calls (Call Stack)
Fixed size
Faster access
Possible overflow
Dynamic size
No overflow (until memory full)
Extra memory for pointers
Input: ABCD
Steps:
Push A, B, C, D
Pop elements
Output: DCBA
Input: (a+b)*(c-d)
Logic:
Push opening bracket
Pop on closing bracket
Output: Balanced
Input: A+B*C
Output: ABC*+
Push – O(1)
Pop – O(1)
Peek – O(1)
Stack Overflow
Stack Underflow
Forgetting to update TOP
Dr. Dheeraj
A + (B ∗ C − D)/E
(X − Y ) ∗ (Z + W )
XY − ZW + ∗
Convert:
(A − B) ∗ (C + D)
Original Infix:
(A − B) ∗ (C + D)
After reversing and swapping brackets:
(D + C ) ∗ (B − A)
Postfix obtained:
DC + BA − ∗
After reversing:
∗ − AB + CD
Final Prefix Expression:
∗ − AB + CD
(A − B) ∗ (C + D) ⇒ ∗ − AB + CD
(A + B) ∗ (C − D/E ) ˆ(F + G ∗ H) − I
Note:
Operator precedence must be strictly followed
Use stack-based conversion
Convert:
∗ + AB − CD
Symbol Stack
D D
C C, D
- (C-D)
Scan right to left
B B, (C-D)
A A, B, (C-D)
+ (A+B), (C-D)
* (A+B)*(C-D)
(A ∗ B) − C
Convert:
− ∗ AB/CD
Symbol Stack
D D
C C, D
/ CD/
Scan right to left
B B, CD/
A A, B, CD/
* AB*, CD/
- AB*CD/-
ABC ∗ +
Convert:
AB + CD − ∗
Symbol Stack
A A
B A, B
+ (A+B)
C (A+B), C
D (A+B), C, D
- (A+B), (C-D)
* (A+B)*(C-D)
(A ∗ B) + C
Convert:
AB + CD − ∗
Symbol Stack
A A
B A, B
+ +AB
C +AB, C
D +AB, C, D
- -CD
* *+AB-CD
ABCD ˆ∗ +EF / − +
Hint:
Use a stack
Process operands left to right
Carefully handle operator precedence
+ + A ∗ B ˆCD − E /F
Dr. Dheeraj
Assistant Professor
January 2026
Elements enter from the Rear and leave from the Front.
No fixed size.
Enqueue: Add node to the tail.
Dequeue: Remove node from the head.
front points to Head, rear points to Tail.
Questions?
Dr. Dheeraj
Assistant Professor
January 2026
Elements enter from the Rear and leave from the Front.
No fixed size.
Enqueue: Add node to the tail.
Dequeue: Remove node from the head.
front points to Head, rear points to Tail.
Questions?
Dr. Dheeraj
ABV-IIITM Gwalior
February 2026
Real-World Example
Looking for a specific face in a crowd or finding a tool in a messy toolbox.
Question: Trace the steps to find 42 in [12, 5, 8, 42, 10]. How many comparisons
are made?
Step 1: 12 == 42 (False)
Step 2: 5 == 42 (False)
Step 3: 8 == 42 (False)
Step 4: 42 == 42 (True!)
Result: Index 3, Total Comparisons: 4.
Real-World Example
Finding a word in a physical Dictionary or finding a page in a textbook.
Question: Find 70 in sorted array [10, 20, 30, 40, 50, 60, 70, 80].
√
Logic: Jump ahead by fixed blocks of size n. When target is passed, do
Linear Search backward.
√
Complexity: O( n).
Real-World Example
Checking a long sorted list of files by skipping 10 files at a time.
Question: Array size n = 16, Target is at index 13. How many ”jumps” of size
√
16 = 4 are made?
Jump 1: Index 0 to 4.
Jump 2: Index 4 to 8.
Jump 3: Index 8 to 12.
Jump 4: Index 12 to 16 (Passed target).
Linear Search starts from Index 12.
Result: 4 Jumps + Linear Search.
Real-World Example
Finding the name ”Brown” in a Phonebook. You don’t start in the middle; you
start near the front because ’B’ is early.
Practice: If arr = [10, 20, 30, 40, 50] and target = 40, what is pos?
(40 − 10) × (4 − 0) 30 × 4
pos = 0 + = =3
50 − 10 40
Result: Index 3. It found the element in one hit!
Logic: Find the range where the element exists by doubling the index
(1, 2, 4, 8 . . . ). Then do Binary Search in that range.
Best For: Unbounded/Infinite arrays.
Real-World Example
Searching for a specific timestamp in a massive, ongoing server log.
Question: If the target is at index 10, what are the ranges checked during the
”doubling” phase?
Check index 1.
Check index 2.
Check index 4.
Check index 8.
Check index 16 (Beyond 10).
Range for Binary Search: [8, 16].
Logic: Divide the array into three parts using two midpoints (m1, m2).
Complexity: O(log3 n).
Real-World Example
Finding the peak of a unimodal function (e.g., finding the maximum brightness in
a video frame).
Question: How many comparisons per step in Ternary Search vs Binary Search?
Logic: Uses Fibonacci numbers to divide the array into unequal parts.
Advantage: Uses only addition and subtraction (no division), which is
faster on some CPUs.
Real-World Example
You need to find a specific mark on a long ribbon, but your calculator’s ”Division”
button is broken. You can only add or subtract.
1 Linear Search.
2 Data must be uniformly distributed (gaps between values are similar).
√
3 Block size = 10 ( 100).
4 No. Binary search requires ”Random Access” (O(1) to middle). Linked lists
are O(n) to reach the middle.
Thank You! Questions?
Dr. Dheeraj (IIITM Gwalior) Sorting in Data Structures March 27, 2026 1 / 21
Lecture Overview
1 Introduction to Sorting
2 Bubble Sort
3 Selection Sort
4 Insertion Sort
5 Merge Sort
6 Quick Sort
7 Heap Sort
8 Real World Examples
9 Comparison Summary
10 Practice Sessions
Dr. Dheeraj (IIITM Gwalior) Sorting in Data Structures March 27, 2026 2 / 21
What is Sorting?
Real-World Example:
E-commerce: Sorting products by price (Low to High).
Contact List: Sorting names alphabetically.
Dr. Dheeraj (IIITM Gwalior) Sorting in Data Structures March 27, 2026 3 / 21
Bubble Sort: The Concept
Dr. Dheeraj (IIITM Gwalior) Sorting in Data Structures March 27, 2026 4 / 21
Problem 1: Bubble Sort Trace
Problem: Sort the array A = [5, 1, 4, 2] using Bubble Sort. Show the state
after each swap in the first pass.
Dr. Dheeraj (IIITM Gwalior) Sorting in Data Structures March 27, 2026 5 / 21
Solution 1: Bubble Sort Trace
Step-by-Step:
1 Compare (5, 1): 5 > 1 → Swap: [1, 5, 4, 2]
2 Compare (5, 4): 5 > 4 → Swap: [1, 4, 5, 2]
3 Compare (5, 2): 5 > 2 → Swap: [1, 4, 2, 5]
After Pass 1, 5 is at the correct position.
Dr. Dheeraj (IIITM Gwalior) Sorting in Data Structures March 27, 2026 6 / 21
Selection Sort
Dr. Dheeraj (IIITM Gwalior) Sorting in Data Structures March 27, 2026 7 / 21
Problem 2: Selection Sort Analysis
Dr. Dheeraj (IIITM Gwalior) Sorting in Data Structures March 27, 2026 8 / 21
Solution 2: Selection Sort Analysis
n(n − 1)
Total Comparisons =
2
For n = 100:
100 × 99
= 4950 comparisons
2
Dr. Dheeraj (IIITM Gwalior) Sorting in Data Structures March 27, 2026 9 / 21
Insertion Sort: The ”Card Player” Method
Dr. Dheeraj (IIITM Gwalior) Sorting in Data Structures March 27, 2026 10 / 21
Merge Sort: Divide and Conquer
Dr. Dheeraj (IIITM Gwalior) Sorting in Data Structures March 27, 2026 11 / 21
Problem 3: Merge Sort Space
Problem: Why is Merge Sort generally not preferred for sorting in-place
arrays, and what is its space complexity?
Dr. Dheeraj (IIITM Gwalior) Sorting in Data Structures March 27, 2026 12 / 21
Solution 3: Merge Sort Space
Dr. Dheeraj (IIITM Gwalior) Sorting in Data Structures March 27, 2026 13 / 21
Quick Sort: Partitioning
Dr. Dheeraj (IIITM Gwalior) Sorting in Data Structures March 27, 2026 14 / 21
Problem 4: Quick Sort Pivot
Problem: Given A = [10, 80, 30, 90, 40, 50, 70]. Perform the first partition
using the last element (70) as the pivot.
Dr. Dheeraj (IIITM Gwalior) Sorting in Data Structures March 27, 2026 15 / 21
Solution 4: Quick Sort Pivot
Result of first partition: [10, 30, 40, 50, 70, 90, 80]
70 is now in its final sorted position.
Left: {10, 30, 40, 50}, Right: {90, 80}.
Dr. Dheeraj (IIITM Gwalior) Sorting in Data Structures March 27, 2026 16 / 21
Heap Sort
Dr. Dheeraj (IIITM Gwalior) Sorting in Data Structures March 27, 2026 17 / 21
Which Algorithm to Use?
Dr. Dheeraj (IIITM Gwalior) Sorting in Data Structures March 27, 2026 18 / 21
Complexity Comparison
Dr. Dheeraj (IIITM Gwalior) Sorting in Data Structures March 27, 2026 19 / 21
Practice Questions for Students
Dr. Dheeraj (IIITM Gwalior) Sorting in Data Structures March 27, 2026 20 / 21
Thank You
Questions?
Dr. Dheeraj Kodati
dkodati@[Link]
Dr. Dheeraj (IIITM Gwalior) Sorting in Data Structures March 27, 2026 21 / 21
Data Structures: Dictionaries (Hash Maps)
Dr. Dheeraj
Assistant Professor
ABV-IIITM, Gwalior
The Problem: How does your phone find ”Mom’s” number instantly?
Key: Contact Name (e.g., ”Mom”)
Value: Phone Number (e.g., ”+1-555-0199”)
Instead of searching every name, the phone uses a Dictionary structure to
jump straight to the number.
Platform: Instagram/X
Key: Username (e.g., @DrDheeraj)
Value: User Profile Object (Bio, Followers, Posts)
Usernames must be unique because they act as keys.
1. Uniqueness
Keys must be unique. You cannot have two identical keys in one dictionary.
2. Immutability
In most languages (like Python), keys must be of a type that cannot
change (like strings or integers).
// Adding values
[Link]("Apple", 50)
[Link]("Banana", 20)
Scenario: You are building a student database. You want to store Student
IDs and Student Names.
Question: Which should be the Key and which should be the Value?
Why?
Answer:
Key: Student ID (e.g., Roll Number)
Value: Student Name
Reasoning: Multiple students can have the same name (e.g., ”Rahul”), so
Names cannot be keys. Student IDs are unique and permanent.
D = CreateDictionary()
[Link]("A", 100)
[Link]("B", 200)
[Link]("A", 300)
print([Link]("A"))
Output: 300
True or False?
”A dictionary can have two different keys that point to the same value.”
Answer: TRUE
D = {"Red": 1, "Blue": 2}
[Link]("Red")
print([Link]("Red"))
If a dictionary has 1,000,000 items, roughly how many steps does it take
to find a specific key if there are no collisions?
Answer: 1 Step
# Creation
my_car = {"brand": "Tesla", "model": "S"}
# Access
print(my_car["brand"]) # Output: Tesla
# Adding
my_car["year"] = 2024
Questions?
Dr. Dheeraj
Assistant Professor, IIITM
Dr. Dheeraj
Assistant Professor, IIITM
Dr. Dheeraj Assistant Professor, IIITM Data Structures: Trees in the Real World February 12, 2026 1 / 21
1. What are Trees?
Dr. Dheeraj Assistant Professor, IIITM Data Structures: Trees in the Real World February 12, 2026 2 / 21
2. Tree Anatomy
Dr. Dheeraj Assistant Professor, IIITM Data Structures: Trees in the Real World February 12, 2026 3 / 21
3. Binary Trees
Dr. Dheeraj Assistant Professor, IIITM Data Structures: Trees in the Real World February 12, 2026 4 / 21
4. Real World Scenario: File Systems
Dr. Dheeraj Assistant Professor, IIITM Data Structures: Trees in the Real World February 12, 2026 5 / 21
5. Pseudo Code: Pre-order Traversal
Pre-order Algorithm
Procedure PreOrder(node)
If node is null: return
Print([Link]) // Visit Root
PreOrder([Link])
PreOrder([Link])
End Procedure
Dr. Dheeraj Assistant Professor, IIITM Data Structures: Trees in the Real World February 12, 2026 6 / 21
6. Industry Challenge: Question 1
Dr. Dheeraj Assistant Professor, IIITM Data Structures: Trees in the Real World February 12, 2026 7 / 21
7. Answer 1: In-order Traversal
Dr. Dheeraj Assistant Professor, IIITM Data Structures: Trees in the Real World February 12, 2026 8 / 21
8. Binary Search Tree (BST)
Dr. Dheeraj Assistant Professor, IIITM Data Structures: Trees in the Real World February 12, 2026 9 / 21
9. Pseudo Code: Searching a Database
Search Algorithm
Function Search(root, target)
If root is null or [Link] == target:
return root
If target < [Link]:
return Search([Link], target)
Else:
return Search([Link], target)
End Function
Dr. Dheeraj Assistant Professor, IIITM Data Structures: Trees in the Real World February 12, 2026 10 / 21
10. Industry Challenge: Question 2
Scenario: You are designing a ”Undo” feature for a coding IDE where you
need to delete a specific version of code but keep the history structure
intact.
Question: If the versions are stored in a tree, what is the most complex
part of deleting a ”Parent” version?
Dr. Dheeraj Assistant Professor, IIITM Data Structures: Trees in the Real World February 12, 2026 11 / 21
11. Answer 2: Node Deletion with Two Children
The Problem: If you delete a node with two children, the tree
”breaks.”
Solution: Replace the deleted node with its In-order Successor (the
smallest value in the right subtree).
Industry Context: Essential in dynamic memory management and
database indexing.
Dr. Dheeraj Assistant Professor, IIITM Data Structures: Trees in the Real World February 12, 2026 12 / 21
12. AVL Trees (Self-Balancing)
The Issue: If we insert items in sorted order (1, 2, 3), the tree
becomes a Linked List (O(N)).
AVL Solution: Rotates itself to stay balanced.
Real World: High-frequency trading platforms where search latency
must be consistent.
Dr. Dheeraj Assistant Professor, IIITM Data Structures: Trees in the Real World February 12, 2026 13 / 21
13. Heaps in Industry
A special tree where the root is always the Max (or Min).
Real World: Task Scheduling in OS, Network traffic prioritization
(Priority Queues).
Industry Use: Uber/Ola finding the ”Nearest Driver.”
Dr. Dheeraj Assistant Professor, IIITM Data Structures: Trees in the Real World February 12, 2026 14 / 21
14. Industry Challenge: Question 3
Scenario: You are working at Google Search. When a user starts typing
”Tree...”, you want to suggest ”Treehouse”, ”Treenet”, etc.
Question: Is a standard Binary Tree efficient for this ”Auto-complete”
feature? If not, what is?
Dr. Dheeraj Assistant Professor, IIITM Data Structures: Trees in the Real World February 12, 2026 15 / 21
15. Answer 3: Trie (Prefix Tree)
Dr. Dheeraj Assistant Professor, IIITM Data Structures: Trees in the Real World February 12, 2026 16 / 21
16. Trees in Web Development: The DOM
Dr. Dheeraj Assistant Professor, IIITM Data Structures: Trees in the Real World February 12, 2026 17 / 21
17. Performance at Scale
Dr. Dheeraj Assistant Professor, IIITM Data Structures: Trees in the Real World February 12, 2026 18 / 21
18. Practice Problems for Students
Dr. Dheeraj Assistant Professor, IIITM Data Structures: Trees in the Real World February 12, 2026 19 / 21
19. Conclusion
Dr. Dheeraj Assistant Professor, IIITM Data Structures: Trees in the Real World February 12, 2026 20 / 21
Questions?
Thank You!
Dr. Dheeraj, IIITM
Dr. Dheeraj Assistant Professor, IIITM Data Structures: Trees in the Real World February 12, 2026 21 / 21
Data Structure Traversals: From Theory to Industry
Lecture Series - Data Structures & Algorithms
Dr. Dheeraj
Assistant Professor, IIITM
18-02-2026
Algorithm Preorder(node)
if node == NULL return
VISIT([Link]) // Process Root
Preorder([Link]) // Move Left
Preorder([Link]) // Move Right
Algorithm Postorder(node)
if node == NULL return
Postorder([Link])
Postorder([Link])
VISIT([Link]) // Process Root last
Algorithm BFS(root)
Create empty Queue Q
[Link](root)
while Q is not empty:
current = [Link]()
VISIT([Link])
if [Link]: [Link]([Link])
if [Link]: [Link]([Link])
Problem: You have a Binary Search Tree (BST) containing employee IDs.
You need to print all IDs in ascending order to generate a payroll report.
Problem: Google Maps needs to find the shortest path (minimum number
of turns) between two intersections in a city grid.
Answer: **BFS**.
Logic: BFS explores all neighbors at distance 1, then distance 2. The
first time it hits the destination, it is guaranteed to be the shortest
path in terms of steps.
Industry Use: Peer-to-peer (P2P) networks use BFS to find the
nearest neighbor with a specific file chunk.
1 Scenario: A web crawler visits a page and finds 5 links. It follows the
first link, finds 5 more, and continues following the first link until it
hits a dead end.
Which traversal is this web crawler mimicking?
Questions?
Dr. Dheeraj
Dr. Dheeraj (Assistant Professor, IIITM) Data Structures: Binary Trees Masterclass February 17, 2026 1 / 20
Lecture Overview
Dr. Dheeraj (Assistant Professor, IIITM) Data Structures: Binary Trees Masterclass February 17, 2026 2 / 20
Tree Terminology
Dr. Dheeraj (Assistant Professor, IIITM) Data Structures: Binary Trees Masterclass February 17, 2026 3 / 20
Problem: Edges and Height
Question
1. If a tree has N nodes, how many edges does it have?
2. What is the maximum height of a tree with N nodes?
3. What is the minimum height?
Dr. Dheeraj (Assistant Professor, IIITM) Data Structures: Binary Trees Masterclass February 17, 2026 4 / 20
Solution: Edges and Height
Dr. Dheeraj (Assistant Professor, IIITM) Data Structures: Binary Trees Masterclass February 17, 2026 5 / 20
Problem: Internal vs Leaf Nodes
Question
In a strictly binary tree (every node has 0 or 2 children), if there are L leaf
nodes, how many internal nodes (I ) are there?
Dr. Dheeraj (Assistant Professor, IIITM) Data Structures: Binary Trees Masterclass February 17, 2026 6 / 20
Solution: Internal vs Leaf Nodes
Formula: L = I + 1
Proof:
For 1 leaf, I = 0 (Root only).
For 2 leaves, I = 1.
Therefore, I = L − 1.
Total Nodes: N = L + I = 2L − 1.
Dr. Dheeraj (Assistant Professor, IIITM) Data Structures: Binary Trees Masterclass February 17, 2026 7 / 20
Binary Tree Traversals
2 3
4 5
Dr. Dheeraj (Assistant Professor, IIITM) Data Structures: Binary Trees Masterclass February 17, 2026 8 / 20
Why move to Conversions?
Dr. Dheeraj (Assistant Professor, IIITM) Data Structures: Binary Trees Masterclass February 17, 2026 9 / 20
Type 1: Inorder to Preorder
Problem Statement
Reconstruct the tree:
Inorder: D, B, E, A, F, C
Preorder: A, B, D, E, C, F
Dr. Dheeraj (Assistant Professor, IIITM) Data Structures: Binary Trees Masterclass February 17, 2026 10 / 20
Solution: Inorder to Preorder
B C
D E F
Dr. Dheeraj (Assistant Professor, IIITM) Data Structures: Binary Trees Masterclass February 17, 2026 11 / 20
Type 2: Inorder to Postorder
Problem Statement
Reconstruct the tree:
Inorder: 4, 2, 5, 1, 3
Postorder: 4, 5, 2, 3, 1
Dr. Dheeraj (Assistant Professor, IIITM) Data Structures: Binary Trees Masterclass February 17, 2026 12 / 20
Solution: Inorder to Postorder
2 3
4 5
Dr. Dheeraj (Assistant Professor, IIITM) Data Structures: Binary Trees Masterclass February 17, 2026 13 / 20
Type 3: Preorder to Postorder
Problem Statement
Given Preorder and Postorder, can we always find a unique tree?
Dr. Dheeraj (Assistant Professor, IIITM) Data Structures: Binary Trees Masterclass February 17, 2026 14 / 20
Solution: Preorder to Postorder
Answer: NO.
Without Inorder, we cannot distinguish between a left-child and a
right-child.
Example: Preorder: AB, Postorder: BA. Could be A as root with B
as left child OR right child.
Exception: Only possible for Full Binary Trees.
Dr. Dheeraj (Assistant Professor, IIITM) Data Structures: Binary Trees Masterclass February 17, 2026 15 / 20
Type 4: Postorder to Inorder
Problem Statement
Postorder: D, E, B, F, C, A
Inorder: D, B, E, A, F, C
Dr. Dheeraj (Assistant Professor, IIITM) Data Structures: Binary Trees Masterclass February 17, 2026 16 / 20
Solution: Postorder to Inorder
Root = A.
Since B is to the left of A in Inorder, B is the root of the left subtree.
Structure:
A
B C
D E F
Dr. Dheeraj (Assistant Professor, IIITM) Data Structures: Binary Trees Masterclass February 17, 2026 17 / 20
Type 5: Postorder to Preorder
Problem Statement
Reconstruct from:
Postorder: 8, 9, 4, 10, 5, 2, 6, 7, 3, 1
Preorder: 1, 2, 4, 8, 9, 5, 10, 3, 6, 7
Dr. Dheeraj (Assistant Professor, IIITM) Data Structures: Binary Trees Masterclass February 17, 2026 18 / 20
Solution: Postorder to Preorder
2 3
4 5 6 7
8 9 10
Dr. Dheeraj (Assistant Professor, IIITM) Data Structures: Binary Trees Masterclass February 17, 2026 19 / 20
Summary and Complexity
Operation Complexity
Traversal (All) O(N)
Height Calculation O(N)
Reconstruction O(N 2 ) (or O(N) with Hashmap)
Dr. Dheeraj (Assistant Professor, IIITM) Data Structures: Binary Trees Masterclass February 17, 2026 20 / 20
Binary Search Tree Traversals
Dr. Dheeraj
ABV-IIITM Gwalior
BEE and IMG
Dr. Dheeraj (ABV-IIITM Gwalior BEE and IMG) Binary Search Tree Traversals March 15, 2026 1 / 19
What is Tree Traversal?
Dr. Dheeraj (ABV-IIITM Gwalior BEE and IMG) Binary Search Tree Traversals March 15, 2026 2 / 19
Example Tree
B C
D E F G
Dr. Dheeraj (ABV-IIITM Gwalior BEE and IMG) Binary Search Tree Traversals March 15, 2026 3 / 19
Inorder Traversal
Rule:
Dr. Dheeraj (ABV-IIITM Gwalior BEE and IMG) Binary Search Tree Traversals March 15, 2026 4 / 19
Inorder Example
Tree:
B C
D E F G
Inorder Output:
D B E AF C G
Dr. Dheeraj (ABV-IIITM Gwalior BEE and IMG) Binary Search Tree Traversals March 15, 2026 5 / 19
Preorder Traversal
Rule:
Dr. Dheeraj (ABV-IIITM Gwalior BEE and IMG) Binary Search Tree Traversals March 15, 2026 6 / 19
Preorder Example
AB D E C F G
Dr. Dheeraj (ABV-IIITM Gwalior BEE and IMG) Binary Search Tree Traversals March 15, 2026 7 / 19
Postorder Traversal
Rule:
Dr. Dheeraj (ABV-IIITM Gwalior BEE and IMG) Binary Search Tree Traversals March 15, 2026 8 / 19
Postorder Example
Postorder Output:
D E B F G C A
Dr. Dheeraj (ABV-IIITM Gwalior BEE and IMG) Binary Search Tree Traversals March 15, 2026 9 / 19
Inorder Algorithm
Dr. Dheeraj (ABV-IIITM Gwalior BEE and IMG) Binary Search Tree Traversals March 15, 2026 10 / 19
Preorder Algorithm
Dr. Dheeraj (ABV-IIITM Gwalior BEE and IMG) Binary Search Tree Traversals March 15, 2026 11 / 19
Postorder Algorithm
Dr. Dheeraj (ABV-IIITM Gwalior BEE and IMG) Binary Search Tree Traversals March 15, 2026 12 / 19
Real World Examples
Inorder
Produces sorted order in BST
Used in database indexing
Preorder
Used to copy tree structures
Used in prefix expressions
Postorder
Used in postfix expression evaluation
Used for deleting trees
Dr. Dheeraj (ABV-IIITM Gwalior BEE and IMG) Binary Search Tree Traversals March 15, 2026 13 / 19
Problem 1
Given Tree
2 3
4 5
Dr. Dheeraj (ABV-IIITM Gwalior BEE and IMG) Binary Search Tree Traversals March 15, 2026 14 / 19
Solution 1
Tree:
1
Steps:
Left subtree → Root → Right subtree
Answer:
42513
Dr. Dheeraj (ABV-IIITM Gwalior BEE and IMG) Binary Search Tree Traversals March 15, 2026 15 / 19
Problem 2
Given preorder:
AB D E C F G
Find postorder.
Dr. Dheeraj (ABV-IIITM Gwalior BEE and IMG) Binary Search Tree Traversals March 15, 2026 16 / 19
Solution 2
Postorder:
D E B F G C A
Dr. Dheeraj (ABV-IIITM Gwalior BEE and IMG) Binary Search Tree Traversals March 15, 2026 17 / 19
Practice Questions
Dr. Dheeraj (ABV-IIITM Gwalior BEE and IMG) Binary Search Tree Traversals March 15, 2026 18 / 19
Thank You
Dr. Dheeraj (ABV-IIITM Gwalior BEE and IMG) Binary Search Tree Traversals March 15, 2026 19 / 19
Balanced BST: AVL Trees and 2-4 Trees
Dr. Dheeraj
IIITM Gwalior
Motivation
10
20
30
40
Skewed BST (Worst Case)
Search Time = O(n)
Balanced BST Idea
30
20 40
10 25
Balanced Tree
Height = O(log n)
AVL Tree Concept
30
20 40
10 25
Balance Factor = Height(left) - Height(right)
Unbalanced AVL Example
10
20
30
BF = -2 → Rotation required
LL Rotation
30
20
Before: 10
20
After: 10 30
RR Rotation
10
20
Before: 30
20
After: 10 30
LR Rotation
30
10
Before: 20
20
After: 10 30
RL Rotation
10
30
Before: 20
20
After: 10 30
AVL Insertion Example
Insert: 10 → 20 → 30
10
20
30
RR Rotation applied
AVL Deletion
30
20 40
35 50
Rebalance after deletion
AVL Complexity
30
20 40
All operations: O(log n)
2-4 Tree Structure
20 40
10 30 50
Multi-key nodes
2-4 Tree Balanced Property
20 40
10 30 50 60
All leaves same level
Insertion in 2-4 Tree
10 20 30
Overflow → Split
Split Operation
Before: 10 20 30
20
After: 10 30
Real World Problem 1
20
30
What to do?
Answer
20
10 30
Use AVL rotations
Real World Problem 2
20
10 30 40
Comparison
AVL
2-4
Balanced BST
Dr. Dheeraj
ABV-IIITM, Gwalior
BEE and IMG Batches
Linux Kernel
Completely Fair Scheduler (CFS) uses RBTs to manage timeline-ordered
process execution.
Database Indexing
Used in memory-resident databases where predictable search time is more
critical than disk I/O optimization (where B-Trees shine).
Network Routing
Used in high-speed routers to store and retrieve IP prefixes efficiently.
13
8 17
1 11 15 25
Note: All NIL children (not shown) are black. Black height from root = 2.
α y x γ
β γ α β
5 15
2 7
Answer: No.
Violation: Property 4. Node 5 and its child Node 2 are both Red.
Red-Red conflicts are not allowed.
Insert the value 15 into an empty Red-Black Tree. Then insert 10. What
are the colors?
10
1 Draw the RBT resulting from inserting {10, 20, 30, 15} in order.
2 What is the maximum possible height of a Red-Black Tree with 7
internal nodes?
3 If a Red-Black Tree has a black height of 3, what is the minimum
number of nodes it can have?
4 Explain why the root of a Red-Black Tree can never be red even after
a rotation.
Dr. Dheeraj
Assistant Professor
IIITM Gwalior
March 2026
Introduction
20 — 40
10 30 50 — 60
Types of Nodes
10 10 — 20 10 — 20 — 30
▶ 2-node → 1 key
▶ 3-node → 2 keys
▶ 4-node → 3 keys
Balanced Property
20
10 30
Insertion Step 1
20
10 30 — 40
Insertion Continued
20
10 30 — 40 — 50
Problem 2
20 — 40
10 30 50 — 60
Search Operation
20 — 40
15 30 50 — 60
Real World Applications
▶ Always balanced
▶ Fast operations
▶ Efficient storage
Complexity
▶ Search: O(log n)
▶ Insert: O(log n)
▶ Delete: O(log n)
Problem 4
Insert: 5, 15, 25
Solution 4
15
5 25
Key Insight
Dr. Dheeraj
Assistant Professor, IIITM Gwalior
25 March 2026
B-Tree of order m:
Max children = m
Min children = ⌈m/2⌉
Keys = children - 1
All leaves at same level
1 Start at root
2 Compare key with node values
3 Move to appropriate child
4 Repeat until found or NULL
5 — 10 — 20
Overflow occurs
10
5 20
10 — 20 — 30
20
10 30 — 40 — 50
20
10 30
Delete 10
Delete 30
Merge nodes
Reduce tree height
Update key 20 to 25
Search key 20
Replace with 25
Structure unchanged
Search: O(log n)
Insert: O(log n)
Delete: O(log n)
Dr. Dheeraj
Assistant Professor, IIITM Gwalior
P(2,2)
Compute orientations
Segments intersect
Questions?
Dr. Dheeraj
Assistant Professor, IIITM Gwalior
8 April 2026
Batch: BEE and IMG
KD-Tree: Definition
▶ Vertical split
▶ Horizontal split
▶ Creates regions
Example Points
▶ Sort by x-coordinate
▶ Median = (7,2)
▶ Root node created
Step 2: Left/Right Split
(7,2)
(5,4) (9,6)
(2,3) (4,7)(8,1)
Nearest Neighbor Search
▶ Traverse tree
▶ Compare distances
▶ Prune subtrees
Range Search
▶ Average: O(log n)
▶ Worst: O(n)
Problem 6
▶ Curse of dimensionality
▶ Ineffective partitioning
Practice Problem 7
Dr. Dheeraj
Assistant Professor, IIITM Gwalior
8 April 2026
Batch: BEE and IMG
Quad Tree: Idea
▶ Stores intervals/segments
▶ Used for overlap detection I2
I1
▶ Supports efficient range
queries
Segment Tree Use
▶ Hierarchical MBRs
▶ Efficient spatial search
R-Tree Problem
▶ Hospital regions
▶ Delivery zones
Voronoi Problem
▶ Partition space
▶ Each region = closest point
Delaunay Triangulation: Idea
▶ Mesh generation
▶ Terrain modeling
Delaunay Problem
▶ Adjust edges
▶ Ensure empty circumcircle property
Practice Questions
115/04/30 IT102 1
Trees
115/04/30 IT102 2
What is a tree?
• Trees are structures used to represent hierarchical
relationship
• Each tree consists of nodes and edges
• Each node represents an object
• Each edge represents the relationship between two
nodes.
node
edge
115/04/30 IT102 3
Some applications of Trees
Organization Chart Expression Tree
President
+
VP VP
Personnel Marketing * 5
3 2
Director Director
Customer Sales
Relation
115/04/30 IT102 4
Terminology I
• For any two nodes u and v, if there is an edge
pointing from u to v, u is called the parent of v
while v is called the child of u. Such edge is
denoted as (u, v).
• In a tree, there is exactly one node without
parent, which is called the root. The nodes
without children are called leaves.
root
u
u: parent of v
v: child of u
v
115/04/30 IT102 leaves 5
Terminology II
• In a tree, the nodes without children are
called leaves. Otherwise, they are called
internal nodes.
internal nodes
leaves
115/04/30 IT102 6
Terminology III
• If two nodes have the same parent, they are
siblings.
• A node u is an ancestor of v if u is parent of v or
parent of parent of v or …
• A node v is a descendent of u if v is child of v or
child of child of v or …
u
T
A subtree of T
v v
115/04/30 IT102 8
Terminology V
• Level of a node n: number of nodes on the path from
root to node n
• Height of a tree: maximum level among all of its node
Level 1
Level 2
height=4
n Level 3
Level 4
115/04/30 IT102 9
Binary Tree
• Binary Tree: Tree in which every node has at
most 2 children
• Left child of u: the child on the left of u
• Right child of u: the child on the right of u
x: left child of u
u v y: right child of u
w: right child of v
x y z: left child of w
w
z
115/04/30 IT102 10
Full binary tree
• If T is empty, T is a full binary tree of height 0.
• If T is not empty and of height h >0, T is a full
binary tree if both subtrees of the root of T are
full binary trees of height h-1.
115/04/30 IT102 11
Property of binary tree (I)
• A full binary tree of height h has 2h-1
nodes
No. of nodes = 20 + 21 + … + 2(h-1)
= 2h – 1
Level 1: 20 nodes
Level 2: 21 nodes
Level 3: 22 nodes
115/04/30 IT102 12
Property of binary tree (II)
• Consider a binary tree T of height h. The
number of nodes of T 2h-1
115/04/30 IT102 13
Property of binary tree (III)
• The minimum height of a binary tree with n
nodes is log(n+1)
115/04/30 IT102 14
Binary Tree ADT
setElem
getElem
setLeft, setRight
binary
getLeft, getRight
tree
isEmpty, isFull,
isComplete
makeTree
115/04/30 IT102 15
Representation of a Binary Tree
• An array-based representation
• A reference-based representation
115/04/30 IT102 16
An array-based representation
nodeNum item leftChild rightChild
–1: empty tree
root
0 d 1 2 0
1 b 3 4
2 f 5 -1
3 a -1 -1
d 4 c -1 -1
5 e -1 -1
b f 6 ? ? ? free
7 ? ? ? 6
8 ? ? ?
a c e 9 ? ? ?
... ..... ..... ....
115/04/30 IT102 17
Reference Based
Representation
NULL: empty tree left element right
b f
b f
a c
a c
115/04/30 IT102 18
Tree Traversal
• Given a binary tree, we may like to do
some operations on all nodes in a binary
tree. For example, we may want to double
the value in every node in a binary tree.
• To do this, we need a traversal algorithm
which visits every node in the binary tree.
115/04/30 IT102 19
Ways to traverse a tree
• There are three main ways to traverse a tree:
– Pre-order:
• (1) visit node, (2) recursively visit left subtree, (3) recursively
visit right subtree
– In-order:
• (1) recursively visit left subtree, (2) visit node, (3) recursively
right subtree
– Post-order:
• (1) recursively visit left subtree, (2) recursively visit right
subtree, (3) visit node
– Level-order:
• Traverse the nodes level by level
• In different situations, we use different traversal
algorithm.
115/04/30 IT102 20
Examples for expression tree
• By pre-order, (prefix)
+*23/84
• By in-order, (infix) +
2*3+8/4
• By post-order, (postfix) * /
23*84/+ 2 3 8 4
• By level-order,
+*/2384
• Note 1: Infix is what we read!
• Note 2: Postfix expression can be computed
efficiently using stack
115/04/30 IT102 21
Pre-order
Algorithm pre-order(BTree x)
If (x is not empty) {
print [Link](); // you can do other things!
pre-order([Link]());
pre-order([Link]());
}
115/04/30 IT102 22
Pre-order example
Print c;
Pre-order(null);
Pre-order(null);
a b d c b c
d
115/04/30 IT102 23
Time complexity of Pre-order
Traversal
• For every node x, we will call
pre-order(x) one time, which performs
O(1) operations.
• Thus, the total time = O(n).
115/04/30 IT102 24
In-order and post-order
Algorithm in-order(BTree x)
If (x is not empty) {
in-order([Link]());
print [Link](); // you can do other things!
in-order([Link]());
}
Algorithm post-order(BTree x)
If (x is not empty) {
post-order([Link]());
post-order([Link]());
print [Link](); // you can do other things!
}
115/04/30 IT102 25
In-order example
In-order(null);
Print c;
In-order(null);
d b a c b c
d
115/04/30 IT102 26
Post-order example
Post-order(null);
Print c;
Post-order(null);
d b c a b c
d
115/04/30 IT102 27
Time complexity for in-order and
post-order
• Similar to pre-order traversal, the time
complexity is O(n).
115/04/30 IT102 28
Level-order
• Level-order traversal requires a queue!
Algorithm level-order(BTree t)
Queue Q = new Queue();
BTree n;
[Link](t); // insert pointer t into Q
while (! [Link]()){
n = [Link](); //remove next node from the front of Q
if (![Link]()){
print [Link](); // you can do other things
[Link]([Link]()); // enqueue left subtree on rear of Q
[Link]([Link]()); // enqueue right subtree on rear of Q
};
};
115/04/30 IT102 29
Time complexity of Level-order
traversal
• Each node will enqueue and dequeue one
time.
• For each node dequeued, it only does one
print operation!
• Thus, the time complexity is O(n).
115/04/30 IT102 30
General tree implementation
struct TreeNode A
{
Object element
TreeNode *firstChild B C D E
TreeNode *nextsibling
}
F G
because we do not know how many children a
node has in advance.
115/04/30 IT102 32
Graphs
115/04/30 IT102 33
What is a graph?
• Graphs represent the relationships among data
items
• A graph G consists of
– a set V of nodes (vertices)
– a set E of edges: each edge connects two nodes
• Each node represents an item
• Each edge represents the relationship between
two items
node
edge
115/04/30 IT102 34
Examples of graphs
Molecular Structure Computer Network
H Server 1 Terminal 1
H C H
Terminal 2
H Server 2
115/04/30 IT102 35
Formal Definition of graph
• The set of nodes is denoted as V
• For any nodes u and v, if u and v are
connected by an edge, such edge is denoted
as (u, v) v
(u, v)
u
• The set of edges is denoted as E
• A graph G is defined as a pair (V, E)
115/04/30 IT102 36
Adjacent
• Two nodes u and v are said to be adjacent
if (u, v) E
v
(u, v)
u
w
u and v are adjacent
v and w are not adjacent
115/04/30 IT102 37
Path and simple path
• A path from v1 to vk is a sequence of
nodes v1, v2, …, vk that are connected by
edges (v1, v2), (v2, v3), …, (vk-1, vk)
• A path is called a simple path if every
node appears at most once. v2 v
v1 3
v4 v5
This is a connected graph because there exists
path between every pair of nodes
115/04/30 IT102 40
Example of disconnected graph
v1 v3 v7 v8
v2
v4 v5
v6 v9
This is a disconnected graph because there does not
exist path between some pair of nodes, says, v1 and
v7
115/04/30 IT102 41
Connected component
• If a graph is disconnect, it can be partitioned into
a number of graphs such that each of them is
connected. Each such graph is called a
connected component.
v2 v7 v8
v1 v3
v4 v5
v6 v9
115/04/30 IT102 42
Complete graph
• A graph is complete if each pair of distinct
nodes has an edge
115/04/30 IT102 43
Subgraph
• A subgraph of a graph G =(V, E) is a graph
H = (U, F) such that U V and
F E.
v2 v2
v1 v3 v3
v4 v5 v4 v5
G H
115/04/30 IT102 44
Weighted graph
• If each edge in G is assigned a weight, it
is called a weighted graph
3500
2000
Houston
115/04/30 IT102 45
Directed graph (digraph)
• All previous graphs are undirected graph
• If each edge in E has a direction, it is called a directed
edge
• A directed graph is a graph where every edges is a
directed edge
Chicago 1000 New York
Directed edge
2000
3500
Houston
115/04/30 IT102 46
More on directed graph
x y
115/04/30 IT102 47
Multigraph
• A graph cannot have duplicate edges.
• Multigraph allows multiple edges and self
edge (or loop).
115/04/30 IT102 48
Property of graph
• A undirected graph that is connected and
has no cycle is a tree.
• A tree with n nodes have exactly n-1
edges.
• A connected undirected graph with n
nodes must have at least n-1 edges.
115/04/30 IT102 49
Implementing Graph
• Adjacency matrix
– Represent a graph using a two-dimensional
array
• Adjacency list
– Represent a graph using n linked lists where n
is the number of vertices
115/04/30 IT102 50
Adjacency matrix for directed graph
Matrix[i][j] = 1 if (vi, vj)E 1 2 3 4 5
0 if (vi, vj)E
v1 v2 v3 v4 v5
1 v1 0 1 0 0 0
v2
v1 v3 2 v 0 0 0 1 0
2
3 v3 0 1 0 1 0
v4 v5 4 v4 0 0 0 0 0
G 5 v5 0 0 1 1 0
115/04/30 IT102 51
Adjacency matrix for weighted
undirected graph
Matrix[i][j] = w(vi, vj) if (vi, vj)E or (vj, vi)E
∞ otherwise
1 2 3 4 5
v2 v1 v2 v3 v4 v5
v1 2 v3
5 1 v1 ∞ 5 ∞ ∞ ∞
4 3 7 2 v2 5 ∞ 2 4 ∞
v4
8 v5
3 v3 0 2 ∞ 3 7
G 4 v4 ∞ 4 3 ∞ 8
115/04/30 IT102
5 v5 ∞ ∞ 7 8 ∞
52
Adjacency list for directed graph
1 v1 v2
v2 2 v2 v4
v1 v3
3 v3 v2 v4
4 v4
v4 v5
5 v5 v3 v4
G
115/04/30 IT102 53
Adjacency list for weighted
undirected graph
v2
v1 2 v3 1 v1 v2(5)
5
4 3 2 v2 v1(5) v3(2) v4(4)
7
3 v3 v2(2) v4(3) v5(7)
v4
8 v5
4 v4 v2(4) v3(3) v5(8)
G 5 v5 v3(7) v4(8)
115/04/30 IT102 54
Pros and Cons
• Adjacency matrix
– Allows us to determine whether there is an
edge from node i to node j in O(1) time
• Adjacency list
– Allows us to find all nodes adjacent to a given
node j efficiently
– If the graph is sparse, adjacency list requires
less space
115/04/30 IT102 55
Problems related to Graph
• Graph Traversal
• Topological Sort
• Spanning Tree
• Minimum Spanning Tree
• Shortest Path
115/04/30 IT102 56
Graph Traversal Algorithm
• To traverse a tree, we use tree traversal
algorithms like pre-order, in-order, and post-
order to visit all the nodes in a tree
• Similarly, graph traversal algorithm tries to visit
all the nodes it can reach.
• If a graph is disconnected, a graph traversal that
begins at a node v will visit only a subset of
nodes, that is, the connected component
containing v.
115/04/30 IT102 57
Two basic traversal algorithms
• Two basic graph traversal algorithms:
– Depth-first-search (DFS)
• After visit node v, DFS strategy proceeds along a
path from v as deeply into the graph as possible
before backing up
– Breadth-first-search (BFS)
• After visit node v, BFS strategy visits every node
adjacent to v before visiting any other nodes
115/04/30 IT102 58
Depth-first search (DFS)
• DFS strategy looks similar to pre-order. From a given
node v, it first visits itself. Then, recursively visit its
unvisited neighbours one by one.
• DFS can be defined recursively as follows.
Algorithm dfs(v)
print v; // you can do other things!
mark v as visited;
for (each unvisited node u adjacent to v)
dfs(u);
115/04/30 IT102 59
DFS example
• Start from v3
1
v3
2
v2 v2
v1 v3
x x x 3 4
v1 v4
v4
x x v5
5
G v5
115/04/30 IT102 60
Non-recursive version of DFS
algorithm
Algorithm dfs(v)
[Link]();
[Link](v);
mark v as visited;
while (![Link]()) {
let x be the node on the top of the stack s;
if (no unvisited nodes are adjacent to x)
[Link](); // backtrack
else {
select an unvisited node u adjacent to x;
[Link](u);
mark u as visited;
}
}
115/04/30 IT102 61
Non-recursive DFS example
visit stack
v3 v3
v2
v2 v3, v2
v1 v3
v1 v3, v2, v1
x x x
x
backtrack v3, v2
v4 v3, v2, v4 v4 x v5
v5 v3, v2, v4 , v5
backtrack v3, v2, v4
backtrack v3, v2 G
backtrack v3
backtrack empty
115/04/30 IT102 62
Breadth-first search (BFS)
• BFS strategy looks similar to level-order. From a
given node v, it first visits itself. Then, it visits
every node adjacent to v before visiting any
other nodes.
– 1. Visit v
– 2. Visit all v’s neigbours
– 3. Visit all v’s neighbours’ neighbours
– …
• Similar to level-order, BFS is based on a queue.
115/04/30 IT102 63
Algorithm for BFS
Algorithm bfs(v)
[Link]();
[Link](v);
mark v as visited;
while(![Link]()) {
w = [Link]();
for (each unvisited node u adjacent to w) {
[Link](u);
mark u as visited;
}
}
115/04/30 IT102 64
BFS example
• Start from v5 Visit Queue
(front to
1 back)
v5 v5 v5
v2 v3 empty
v1
x x
2 3
v3 v4
v3 v3
x v4 v3, v4
v4x
v4
x
4
v2 v2 v4, v2
v5 v2
G 5 empty
v1 v1 v1
115/04/30 IT102
empty65
Topological order
• Consider the prerequisite structure for courses:
b d
a
c e
• Each node x represents a course x
• (x, y) represents that course x is a prerequisite to course y
• Note that this graph should be a directed graph without cycles
(called a directed acyclic graph).
• A linear order to take all 5 courses while satisfying all prerequisites
is called a topological order.
• E.g.
– a, c, b, e, d
– c, a, b, e, d
115/04/30 IT102 66
Topological sort
• Arranging all nodes in the graph in a topological
order
Algorithm topSort
n = |V|;
for i = 1 to n {
select a node v that has no successor;
[Link](1, v);
delete node v and its edges from the graph;
}
return aList;
115/04/30 IT102 67
Example
b d b
a a
c e c
e
1. d has no 2. Both b and e have
successor! no successor!
Choose d! Choose e!
b b
a a
a
c
3. Both b and c have 4. Only b has no 5. Choose a!
no successor! successor! The topological
Choose c! Choose b! order is
a,b,c,e,d
115/04/30 IT102 68
Topological sort algorithm 2
• This algorithm is based on DFS
Algorithm topSort2
[Link]();
for (all nodes v in the graph) {
if (v has no predecessors) {
[Link](v);
mark v as visited;
}
}
while (![Link]()) {
let x be the node on the top of the stack s;
if (no unvisited nodes are adjacent to x) { // i.e. x has no unvisited successor
[Link](1, x);
[Link](); // blacktrack
} else {
select an unvisited node u adjacent to x;
[Link](u);
mark u as visited;
}
}
return aList;
115/04/30 IT102 69
Spanning Tree
• Given a connected undirected graph G, a
spanning tree of G is a subgraph of G that
contains all of G’s nodes and enough of its
edges to form a tree.
v2
v1 v3
v4 v5
Spanning
tree Spanning tree is not unique!
115/04/30 IT102 70
DFS spanning tree
• Generate the spanning tree edge during the DFS
traversal.
Algorithm dfsSpanningTree(v)
mark v as visited;
for (each unvisited node u adjacent to v) {
mark the edge from u to v;
dfsSpanningTree(u);
}
115/04/30 IT102 73
Formal definition of minimum
spanning tree
• Given a connected undirected graph G.
• Let T be a spanning tree of G.
• cost(T) = eTweight(e)
• The minimum spanning tree is a spanning tree T
which minimizes cost(T)
v2
v1 2 v3
5 Minimum
4 3 spanning
7
tree
v4
8 v5
115/04/30 IT102 74
Prim’s algorithm (I)
v1 v2 v1 v2 v1 v2
5 2 v3 5 2 v3 5 2 v3
4 3 7 4 3 7 4 3 7
v4 8 v5 v4 8 v5 v4 8 v5
Start from v5, find the Find the minimum Find the minimum
minimum edge attach to edge attach to v3 and edge attach to v2, v3
v5 v5 and v5
v2 v1 v2
v1 2 v3
5 2 v3 5
4 3 7 4 3 7
v4 8 v5 v4 8 v5
115/04/30 IT102 76
Shortest path
• Consider a weighted directed graph
– Each node x represents a city x
– Each edge (x, y) has a number which represent the
cost of traveling from city x to city y
• Problem: find the minimum cost to travel from
city x to city y
• Solution: find the shortest path from x to y
115/04/30 IT102 77
Formal definition of shortest
path
• Given a weighted directed graph G.
• Let P be a path of G from x to y.
• cost(P) = ePweight(e)
• The shortest path is a path P which minimizes
cost(P)
v2
v1 2 v3
5
4 3 Shortest Path
4
v4
8 v5
115/04/30 IT102 78
Dijkstra’s algorithm
• Consider a graph G, each edge (u, v) has
a weight w(u, v) > 0.
• Suppose we want to find the shortest path
starting from v1 to any node vi
• Let VS be a subset of nodes in G
• Let cost[vi] be the weight of the shortest
path from v1 to vi that passes through
nodes in VS only.
115/04/30 IT102 79
Example for Dijkstra’s algorithm
v1 v2 2 v3
5
4 3 4
v4
8 v5
v VS cost[v1] cost[v2] cost[v3] cost[v4] cost[v5]
1 [v1] 0 5 ∞ ∞ ∞
115/04/30 IT102 80
Example for Dijkstra’s algorithm
v2
v1 2 v3
5
4 3 4
v4
8 v5
v VS cost[v1] cost[v2] cost[v3] cost[v4] cost[v5]
1 [v1] 0 5 ∞ ∞ ∞
2 v2 [v1, v2] 0 5 ∞ 9 ∞
115/04/30 IT102 81
Example for Dijkstra’s algorithm
v2
v1 2 v3
5
4 3 4
v4
8 v5
v VS cost[v1] cost[v2] cost[v3] cost[v4] cost[v5]
1 [v1] 0 5 ∞ ∞ ∞
2 v2 [v1, v2] 0 5 ∞ 9 ∞
3 v4 [v1, v2, v4] 0 5 12 9 17
115/04/30 IT102 82
Example for Dijkstra’s algorithm
v2
v1 2 v3
5
4 3 4
v4
8 v5
v VS cost[v1] cost[v2] cost[v3] cost[v4] cost[v5]
1 [v1] 0 5 ∞ ∞ ∞
2 v2 [v1, v2] 0 5 ∞ 9 ∞
3 v4 [v1, v2, v4] 0 5 12 9 17
4 v3 [v1, v2, v4, v3] 0 5 12 9 16
5 v5 [v1, v2, v4, v3, v5] 0 5 12 9 16
115/04/30 IT102 83
Dijkstra’s algorithm
Algorithm shortestPath()
n = number of nodes in the graph;
for i = 1 to n
cost[vi] = w(v1, vi);
VS = { v1 };
for step = 2 to n {
find the smallest cost[vi] s.t. vi is not in VS;
include vi to VS;
for (all nodes vj not in VS) {
if (cost[vj] > cost[vi] + w(vi, vj))
cost[vj] = cost[vi] + w(vi, vj);
}
}
115/04/30 IT102 84
Summary
• Graphs can be used to represent many real-life
problems.
• There are numerous important graph algorithms.
• We have studied some basic concepts and
algorithms.
– Graph Traversal
– Topological Sort
– Spanning Tree
– Minimum Spanning Tree
– Shortest Path
115/04/30 IT102 85
Optimal and Average BSTs with Balanced Trees
Dr. Dheeraj
IIITM Gwalior
Dr. Dheeraj IIITM Gwalior Optimal and Average BSTs with Balanced Trees 1 / 21
Introduction
Dr. Dheeraj IIITM Gwalior Optimal and Average BSTs with Balanced Trees 2 / 21
BST Example
10
5 15
2 7
Dr. Dheeraj IIITM Gwalior Optimal and Average BSTs with Balanced Trees 3 / 21
Problem Statement 1 (Real World)
You are designing a student database where IDs are inserted in sorted
order.
What happens to BST structure?
What is time complexity?
Dr. Dheeraj IIITM Gwalior Optimal and Average BSTs with Balanced Trees 4 / 21
Solution
1
Random insertions
Height ≈ log n
Dr. Dheeraj IIITM Gwalior Optimal and Average BSTs with Balanced Trees 6 / 21
Problem Statement 2
Dr. Dheeraj IIITM Gwalior Optimal and Average BSTs with Balanced Trees 7 / 21
Solution
40
20 60
10 30 50 70
Height = 3
Dr. Dheeraj IIITM Gwalior Optimal and Average BSTs with Balanced Trees 8 / 21
Optimal BST
Uses probabilities
Minimizes expected search cost
Dr. Dheeraj IIITM Gwalior Optimal and Average BSTs with Balanced Trees 9 / 21
Problem Statement 3 (OBST)
Keys: A, B, C
Probabilities: 0.2, 0.5, 0.3
Construct Optimal BST
Dr. Dheeraj IIITM Gwalior Optimal and Average BSTs with Balanced Trees 10 / 21
Solution
A C
B chosen as root
Minimum search cost
Dr. Dheeraj IIITM Gwalior Optimal and Average BSTs with Balanced Trees 11 / 21
Balanced BST
Dr. Dheeraj IIITM Gwalior Optimal and Average BSTs with Balanced Trees 12 / 21
Problem Statement 4 (AVL)
Dr. Dheeraj IIITM Gwalior Optimal and Average BSTs with Balanced Trees 13 / 21
Solution
10
20
30
Before Rotation:
20
10 30
After Rotation:
Dr. Dheeraj IIITM Gwalior Optimal and Average BSTs with Balanced Trees 14 / 21
Problem Statement 5 (Real World)
Dr. Dheeraj IIITM Gwalior Optimal and Average BSTs with Balanced Trees 15 / 21
Solution
Dr. Dheeraj IIITM Gwalior Optimal and Average BSTs with Balanced Trees 16 / 21
Comparison
Dr. Dheeraj IIITM Gwalior Optimal and Average BSTs with Balanced Trees 17 / 21
Question
Dr. Dheeraj IIITM Gwalior Optimal and Average BSTs with Balanced Trees 18 / 21
Answer
Guaranteed O(log n)
Stable performance
Dr. Dheeraj IIITM Gwalior Optimal and Average BSTs with Balanced Trees 19 / 21
Summary
Dr. Dheeraj IIITM Gwalior Optimal and Average BSTs with Balanced Trees 20 / 21
Practice Questions
Dr. Dheeraj IIITM Gwalior Optimal and Average BSTs with Balanced Trees 21 / 21