Docsity

Data Structures & Algorithms - Complete Study Notes (Big-O, Exam Tips), Study notes of Data Structures and Algorithms

Toggle sidebar
bg1
DATA STRUCTURES & ALGORITHMS
Complete Course Notes
A comprehensive, exam-ready study guide covering core data structures, algorithms, and complexity
analysis
Covers: Big-O Notation • Arrays & Linked Lists • Stacks & Queues • Trees • Graphs • Sorting • Searching • Hash
Tables • Recursion • Dynamic Programming
12 Chapters | Key Terms | Complexity Tables | Exam Tips | Practice Review Questions
Suitable for: Intro Data Structures & Algorithms courses in Computer Science and Software Engineering programs
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Data Structures & Algorithms - Complete Study Notes (Big-O, Exam Tips) and more Study notes Data Structures and Algorithms in PDF only on Docsity!

DATA STRUCTURES & ALGORITHMS

Complete Course Notes

A comprehensive, exam-ready study guide covering core data structures, algorithms, and complexity

analysis

Covers: Big-O Notation • Arrays & Linked Lists • Stacks & Queues • Trees • Graphs • Sorting • Searching • Hash Tables • Recursion • Dynamic Programming

12 Chapters | Key Terms | Complexity Tables | Exam Tips | Practice Review Questions

Suitable for: Intro Data Structures & Algorithms courses in Computer Science and Software Engineering programs

Table of Contents

Chapter 1 — Algorithmic Complexity and Big-O Notation

Chapter 2 — Arrays and Linked Lists

Chapter 3 — Stacks and Queues

Chapter 4 — Recursion

Chapter 5 — Trees

Chapter 6 — Binary Search Trees and Balanced Trees

Chapter 7 — Heaps and Priority Queues

Chapter 8 — Hash Tables

Chapter 9 — Graphs

Chapter 10 — Sorting Algorithms

Chapter 11 — Searching Algorithms

Chapter 12 — Dynamic Programming and Greedy Algorithms

Final Exam Review — Practice Questions

Operation Time Complexity Search (unsorted) O(n) Insertion/deletion at end O(1) amortized Insertion/deletion at beginning/middle O(n) 2.2 Linked Lists

  • Linked list: A linear data structure where each element (node) points to the next, allowing non-

contiguous memory storage.

Type Description Singly linked list Each node points only to the next node Doubly linked list Each node points to both the next and previous node Circular linked list The last node points back to the first node EXAM TIP

Arrays offer O(1) random access but O(n) insertion/deletion in the middle; linked lists offer O(1)

insertion/deletion (once you have a reference to the node) but O(n) access — this trade-off is the single

most tested comparison in this chapter.

Chapter 3 — Stacks and Queues 3.1 Stacks (LIFO)

  • Stack: A Last-In-First-Out (LIFO) structure: the most recently added element is the first one

removed.

  • Core operations: push (add to top), pop (remove from top), peek (view top without removing).
  • Common uses: function call stacks, undo features, expression evaluation, backtracking. 3.2 Queues (FIFO)
  • Queue: A First-In-First-Out (FIFO) structure: the earliest added element is the first one removed.
  • Core operations: enqueue (add to back), dequeue (remove from front).
  • Common uses: task scheduling, breadth-first search, print/request queues. Structure Order Add Remove Stack LIFO push (top) pop (top) Queue FIFO enqueue (back) dequeue (front)

EXAM TIP

A classic exam question gives a real-world scenario (e.g., a browser's back button, a printer queue) and

asks whether a stack or queue models it — back button = stack (LIFO); printer = queue (FIFO).

Chapter 4 — Recursion 4.1 Key Components of Recursion

  • Base case: The condition under which a recursive function stops calling itself, preventing infinite

recursion.

  • Recursive case: The part of the function that calls itself with a smaller/simpler input, moving toward

the base case.

function factorial(n) { if (n <= 1) return 1; // base case return n * factorial(n - 1); // recursive case } 4.2 Recursion vs. Iteration

  • Recursion often produces cleaner code for naturally recursive problems (tree traversal, divide-and-

conquer).

  • Recursion uses more memory due to the call stack; deep recursion risks a stack overflow.
  • Any recursive solution can be rewritten iteratively, typically using an explicit stack. EXAM TIP

Forgetting the base case (or writing one that's never reached) is the most common recursion bug tested

on exams — always check that every recursive call moves strictly closer to the base case.

Chapter 5 — Trees 5.1 Basic Tree Terminology

  • Root: The topmost node of a tree, with no parent.
  • Leaf: A node with no children.
  • Height: The number of edges on the longest path from a node down to a leaf.
  • Depth: The number of edges from the root down to a given node. 5.2 Tree Traversals Traversal Order Common Use Pre-order Root → Left → Right Copying/serializing a tree In-order Left → Root → Right Retrieving sorted data from a BST

7.2 Heap Operations Operation Time Complexity Find min/max O(1) Insert O(log n) Delete min/max O(log n) Build heap from array O(n) 7.3 Priority Queues

A priority queue is an abstract data type where each element has a priority, and elements are served in

priority order rather than strictly by insertion order. Heaps are the standard underlying implementation.

EXAM TIP

A heap is NOT fully sorted — it only guarantees the root is the min/max. Don't confuse heap order with a

fully sorted array; this is a very common misconception.

Chapter 8 — Hash Tables 8.1 How Hash Tables Work

  • Hash function: A function that converts a key into an index (hash code) used to store/retrieve data

in the table.

  • Collision: When two different keys hash to the same index. 8.2 Collision Resolution
  • Chaining: each index stores a linked list (or similar structure) of all elements that hash there.
  • Open addressing: on collision, probe for the next available slot (e.g., linear probing, quadratic

probing).

8.3 Time Complexity Operation Average Case Worst Case Insert O(1) O(n) Search O(1) O(n) Delete O(1) O(n) EXAM TIP

Hash table worst-case O(n) happens only when many keys collide (a poor hash function or adversarial

input) — under normal conditions with a good hash function, O(1) average case is expected and

frequently the 'correct' exam answer.

Chapter 9 — Graphs 9.1 Graph Terminology

  • Vertex (node): A fundamental unit of a graph.
  • Edge: A connection between two vertices.
  • Directed graph: Edges have a direction (one-way relationship).
  • Undirected graph: Edges have no direction (two-way relationship).
  • Weighted graph: Edges carry a numeric cost/weight (e.g., distance, time). 9.2 Graph Representations Representation Space Best For Adjacency matrix O(V²) Dense graphs; fast edge lookup Adjacency list O(V + E) Sparse graphs; space-efficient 9.3 Graph Traversal
  • Breadth-First Search (BFS): explores neighbors level by level using a queue; finds shortest path in

unweighted graphs.

  • Depth-First Search (DFS): explores as far as possible along a branch before backtracking, using a

stack (or recursion).

9.4 Shortest Path Algorithms Algorithm Use Case Handles Negative Weights? Dijkstra's algorithm Single-source shortest path, non- negative weights No Bellman-Ford algorithm Single-source shortest path Yes EXAM TIP

BFS uses a queue and finds the shortest path by number of edges in an unweighted graph; DFS uses a

stack/recursion and is better suited for tasks like detecting cycles or exploring all paths — a frequently

tested pairing.

Chapter 12 — Dynamic Programming and Greedy Algorithms 12.1 Dynamic Programming (DP)

  • Optimal substructure: A problem has optimal substructure if an optimal solution can be built from

optimal solutions of its subproblems.

  • Overlapping subproblems: A problem has overlapping subproblems if the same smaller

subproblems are solved repeatedly.

  • Memoization (top-down): store results of expensive recursive calls and reuse them when the same

inputs occur again.

  • Tabulation (bottom-up): iteratively build up a table of solutions starting from the base cases. 12.2 Greedy Algorithms
  • Greedy algorithm: Makes the locally optimal choice at each step, hoping this leads to a globally

optimal solution.

Greedy algorithms are simpler and faster than DP but only produce a correct, optimal result for problems

with the 'greedy choice property' — they don't work for every problem that DP can solve.

EXAM TIP

Dynamic programming requires BOTH optimal substructure AND overlapping subproblems — a problem

with optimal substructure but no overlapping subproblems (like standard merge sort) is a divide-and-

conquer problem, not a DP problem. This distinction is frequently tested.

Final Exam Review — Practice Questions

Use these questions to self-test after each study session. Cover the answers and try to explain each

concept out loud before checking your notes.

    1. Explain why Big-O notation ignores constants and lower-order terms.
    1. Compare the time complexity trade-offs between arrays and linked lists.
    1. Give a real-world example each for a stack and a queue, and explain why.
    1. What are the two essential components of any correct recursive function?
    1. What does in-order traversal of a BST always produce, and why?
    1. Why does a BST's worst-case performance degrade to O(n), and how do self-balancing trees

prevent this?

    1. Explain why a heap is not the same as a fully sorted array.
    1. Under what condition does a hash table degrade from O(1) to O(n)?
    1. Compare BFS and DFS: what data structure does each use, and when is each preferred?
    1. What two properties must a problem have for dynamic programming to apply?

End of Notes — Good luck with your studies!