Docsity

Data Structures and Algorithms Complete Notes | DSA Study Guide for Exams & Interviews, Study Guides, Projects, Research of Computer Science

Partial preview of the text

Download Data Structures and Algorithms Complete Notes | DSA Study Guide for Exams & Interviews and more Study Guides, Projects, Research Computer Science in PDF only on Docsity!

Data Structures & Algorithms

A Complete Study Guide for Exams & Interviews

Covers arrays, linked lists, stacks, queues, trees, graphs, sorting, searching, hashing, and complexity analysis —

with examples, diagrams-in-text, and comparison tables.

Table of Contents

3.1 Types of Linked Lists

  • Singly Linked List — each node points to the next node only.
  • Doubly Linked List — each node points to both the next and previous nodes.
  • Circular Linked List — the last node points back to the first node. 3.2 Key Operations struct Node { int data; Node* next; }; // Insertion at head - O(1) Node* newNode = new Node{value, head}; head = newNode;
  • Insertion at head: O(1)
  • Insertion at tail: O(n) without a tail pointer, O(1) with one
  • Search: O(n), since traversal is required
  • Deletion: O(n) to find the node, O(1) to unlink once found Note: Linked lists trade fast random access (which they don't have) for cheap insertion/deletion at known positions.
  1. Stacks & Queues 4.1 Stack (LIFO)

A stack follows the Last-In-First-Out principle. Elements are added and removed from the same end, called the

top. Common uses include function call management (call stack), undo operations, and expression evaluation.

  • push(x) — add element to top: O(1)
  • pop() — remove top element: O(1)
  • peek()/top() — view top element without removing: O(1) stack s; s.push(10); s.push(20); s.pop(); // removes 20 s.top(); // returns 10 4.2 Queue (FIFO)

A queue follows the First-In-First-Out principle. Elements are added at the rear and removed from the front.

Used in scheduling, buffering, and breadth-first search.

  • enqueue(x) — add element to rear: O(1)
  • dequeue() — remove element from front: O(1)
  • Variants: Circular Queue, Priority Queue, Deque (double-ended queue)
  1. Trees

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

designated as the root. Every node except the root has exactly one parent, and there are no cycles.

5.1 Binary Trees

A binary tree restricts each node to at most two children, commonly referred to as left and right.

  • Binary Search Tree (BST): left subtree values < node < right subtree values, enabling O(log n) average search.
  • Balanced trees (AVL, Red-Black Tree): self-balance to guarantee O(log n) operations even in the worst case.
  • Heap: a complete binary tree used to implement priority queues (min-heap or max-heap).

5.2 Tree Traversals

  • Inorder (Left, Root, Right) — visits nodes in sorted order for a BST.
  • Preorder (Root, Left, Right) — used to copy a tree.
  • Postorder (Left, Right, Root) — used to delete a tree.
  • Level-order — visits nodes level by level using a queue (BFS). void inorder(Node* root) { if (!root) return; inorder(root->left); cout << root->data << " "; inorder(root->right); }
  1. Graphs

A graph is a set of vertices (nodes) connected by edges. Graphs can be directed or undirected, and weighted or

unweighted, making them suitable for modeling networks, maps, and relationships.

6.1 Representations

  • Adjacency Matrix: a 2D array where cell [i][j] indicates an edge between vertex i and j. Space: O(V²).
  • Adjacency List: each vertex stores a list of its neighbors. Space: O(V + E), more efficient for sparse graphs. 6.2 Traversal Algorithms
  • Breadth-First Search (BFS): explores neighbors level by level using a queue — useful for shortest path in unweighted graphs.
  • Depth-First Search (DFS): explores as far as possible along a branch before backtracking, using a stack or recursion. 6.3 Common Graph Algorithms
  • Dijkstra's Algorithm — shortest path in weighted graphs with non-negative weights.
  • Kruskal's / Prim's Algorithm — minimum spanning tree.
  • Topological Sort — ordering of vertices in a Directed Acyclic Graph (DAG).
  1. Sorting Algorithms

Sorting arranges data in a particular order (ascending or descending) and is foundational to searching, database

indexing, and many other algorithms.

Operation Best Average Worst Bubble Sort O(n) O(n²) O(n²) Selection Sort O(n²) O(n²) O(n²) Insertion Sort O(n) O(n²) O(n²) Merge Sort O(n log n) O(n log n) O(n log n) Quick Sort O(n log n) O(n log n) O(n²) Heap Sort O(n log n) O(n log n) O(n log n) 7.1 Merge Sort (Divide & Conquer)

Merge Sort repeatedly divides the array into halves until each sub-array has one element, then merges them

back in sorted order. It guarantees O(n log n) performance regardless of input, but requires O(n) extra space.