Top Banner Ad
ACS201 · 1-Page Cheat Sheet

Essentials of Data Structures

ACS201 · YEAR 1 · SEM 1

Essentials of Data Structures — Last-Night Cheat Sheet

prephora.in

Two-credit paper dominated by traces: draw the structure after every operation and write the final contents or complexity, not a prose description.

1. Introduction and Arrays

Recall anchor. Contiguous memory buys O(1) access and costs O(n) shifts: that single trade-off explains most array questions.

Scored points.

  • Data structure definition: a way of organising data for efficient access and modification; need — speed, memory, and clarity.
  • Classification: linear (array, stack, queue, list) vs non-linear (tree, graph); static (fixed size, array) vs dynamic (linked list).
  • Algorithm efficiency — Big O concept only: O(1) constant array access, O(n) linear scan, O(log n) binary search, O(n²) nested loops.
  • Array operations: insert shifts elements right, delete shifts left, search checks positions, traverse visits each index once; index runs 0 to n−1.
  • 2D array: A[i][j] in row-major order; matrix addition adds matching entries, transpose sets B[j][i] = A[i][j].
  • Real connect: NumPy arrays, images as 2D matrices, sensor logs, and spreadsheet rows are all arrays.
  • Practice tasks: trace insertion at index 2, deletion of 30, count even/odd, find max–min–average, remove duplicates.

Formula strip.

&A[i]=base+i×s,insert at i⇒O(n−i) shifts,access A[i]⇒O(1),B[j][i]=A[i][j]\&A[i]=\text{base}+i\times s,\quad \text{insert at }i\Rightarrow O(n-i)\ \text{shifts},\quad \text{access }A[i]\Rightarrow O(1),\quad B[j][i]=A[i][j]

Micro-example. Insert 25 at index 2 of [10,20,30,40]: shift 30 and 40 right → [10,20,25,30,40]; deleting 30 from [10,20,30,40] gives [10,20,40].

Examiner line. Show the index positions before and after the operation, then write the complexity; the trace and the Big O are separate marks.

2. Linked List

Recall anchor. Rewire the pointer, don't shift the data: every insertion or deletion is a two-pointer assignment in a fixed order.

Scored points.

  • Node = data + pointer(next); the head pointer locates the first node and NULL marks the end.
  • Why linked list over array: dynamic size and O(1) insertion at head, but no direct indexing and extra pointer memory.
  • Singly linked list insert at head: new->next = head; head = new. Insert at tail walks to the last node; insert at position k stops at node k−1.
  • Deletion: locate the previous node, set prev->next = cur->next, then free cur; deleting the head updates head itself.
  • Traversal: start at head and follow next until NULL; count nodes with a running counter.
  • Doubly linked list has prev and next pointers, enabling backward movement; circular linked list has tail->next = head.
  • Real connect: browser back/forward uses a doubly linked list, a music playlist is circular, and Python deque supports BFS and LRU caches.

Formula strip.

node=(data,next),head-insert: new.next=head; head=new,delete: prev.next=cur.next,circular: tail.next=head\text{node}=(\text{data},\text{next}),\quad \text{head-insert}:\ \text{new.next}=\text{head};\ \text{head}=\text{new},\quad \text{delete}:\ \text{prev.next}=\text{cur.next},\quad \text{circular}:\ \text{tail.next}=\text{head}

Micro-example. Inserting 10, 20, 30 at the head gives 30 → 20 → 10 → NULL; deleting node 20 leaves 30 → 10 → NULL.

Examiner line. Draw the list with arrows after each pointer assignment; state which pointer changes first and why reversing the order would lose the list.

3. Stack and Queue

Recall anchor. Stack = LIFO at top, Queue = FIFO from front to rear: the application decides which structure, the trace decides which mark.

Scored points.

  • Stack operations: push adds at top, pop removes from top, peek reads top without removing; array implementation uses an index top initialised to −1.
  • Stack applications: balanced-parentheses check (push opening bracket, pop on a matching closing bracket, balanced iff stack is empty at the end), function call stack, and undo (Ctrl+Z).
  • Queue operations: enqueue adds at rear, dequeue removes from front; array implementation with front and rear indices must handle wrap-around in a circular queue.
  • Queue applications: print spooler, task scheduling, WhatsApp delivery queue, and CPU scheduling.
  • Polish notation: prefix operators precede operands; scanf/postfix conversion uses a stack of operands.
  • Tracing convention: write the stack bottom-to-top and the queue front-to-rear after every operation.
  • Python traceback is a visible call stack; both structures can be shown with array or linked implementation in C/Python.

Formula strip.

stack: push(x)⇒top++, pop()⇒x=a[top−−],queue: enqueue at rear, dequeue at front,balanced⇔empty stack at end\text{stack}:\ \text{push}(x)\Rightarrow \text{top}++,\ \text{pop}()\Rightarrow x=a[\text{top}--],\quad \text{queue}:\ \text{enqueue at rear},\ \text{dequeue at front},\quad \text{balanced}\Leftrightarrow\text{empty stack at end}

Micro-example. push(5), push(10), pop() returns 10, push(15) → top = 15 with stack [5, 15]; enqueue(A), enqueue(B), dequeue() → A, enqueue(C) → front = B.

Examiner line. For a bracket or call-stack question, show the stack contents at each bracket or call; conclude with one explicit yes/no or final top value.

4. Searching and Sorting

Recall anchor. Search needs order for binary; sorting buys that order: compare the number of comparisons, not just the final list.

Scored points.

  • Linear search scans index 0…n−1 until a match; O(n) worst case and works on unsorted data.
  • Binary search requires a sorted array: mid = ⌊(low+high)/2⌋; discard the half that cannot contain the key; O(log n).
  • Bubble sort repeatedly swaps adjacent out-of-order pairs; n−1 passes, O(n²), and early exit if a pass has no swaps.
  • Insertion sort grows a sorted prefix by inserting the next element at its correct place; O(n²) worst case, efficient for nearly sorted data.
  • Selection sort picks the minimum of the unsorted suffix and swaps it into place; O(n²) with at most n−1 swaps.
  • Quick sort partitions around a pivot and recurses on the two sides; merge sort divides, sorts, and merges in O(n log n) with extra memory.
  • Python built-ins: sort() sorts in place, sorted() returns a new list, both use Timsort (merge + insertion hybrid); applications include database indexes, leaderboards, and ML preprocessing.

Formula strip.

mid=⌊low+high2⌋,Tlinear=O(n), Tbinary=O(log⁡n), Tbubble/insertion/selection=O(n2), Tmerge/quick=O(nlog⁡n)\text{mid}=\left\lfloor\frac{\text{low}+\text{high}}2\right\rfloor,\quad T_{\text{linear}}=O(n),\ T_{\text{binary}}=O(\log n),\ T_{\text{bubble/insertion/selection}}=O(n^2),\ T_{\text{merge/quick}}=O(n\log n)

Micro-example. Binary search for 35 in [10,20,30,35,40,50]: mid = 30 → go right; mid = 40 → go left; mid = 35 → found in 3 comparisons, versus 4 for linear search.

Examiner line. Show each comparison and the retained half for binary search, or each pass for a sort; then state the complexity and stability in the last line.

5. Trees and Graphs (Concept Level)

Recall anchor. BST order gives sorted inorder; graph representation decides traversal cost: draw the small tree or graph before answering.

Scored points.

  • Binary tree terminology: root, child, leaf, level, height; a binary tree of height h has at most 2^{h+1}−1 nodes and 2^h leaves at the last level.
  • Binary Search Tree property: left subtree values < node < right subtree values; insert by comparison, search by the same comparison.
  • Traversals: inorder (left, root, right) of a BST gives sorted order; preorder (root, left, right) copies the tree structure; postorder (left, right, root) is used for deletion.
  • Graph basics: vertex, edge, directed/undirected, weighted/unweighted; adjacency list stores only existing edges, saving space for sparse graphs.
  • Insertion into an adjacency list adds a neighbour to the vertex's list; undirected graphs add the reverse edge too.
  • BFS uses a queue and visits level by level; DFS uses a stack (or recursion) and goes deep first; trace both on a 5–6 node graph.
  • Real connect: file system = tree, database indexing = BST, maps = weighted graph, social network = graph, decision tree in ML = binary tree.

Formula strip.

Nmax⁡=2h+1−1,BST: left<v<right,inorder=L+root+R,BFS=queue, DFS=stackN_{\max}=2^{h+1}-1,\quad \text{BST}:\ \text{left}<v<\text{right},\quad \text{inorder}=\text{L}+\text{root}+\text{R},\quad \text{BFS}=\text{queue},\ \text{DFS}=\text{stack}

Micro-example. Inserting 50, 30, 70, 20, 40 into a BST: inorder = 20, 30, 40, 50, 70; preorder = 50, 30, 20, 40, 70; a BFS from A on A–B, A–C, B–D, C–E visits A, B, C, D, E.

Examiner line. Draw the tree/graph, label root and leaves, then write the traversal order as a comma-separated list; a trace without the drawing loses the structure marks.

Memory map

%%{init: {'theme': 'base', 'look': 'handDrawn', 'themeVariables': {'fontFamily': "'Nunito', sans-serif", 'fontSize': '15px', 'primaryColor': '#EFEAD6', 'primaryBorderColor': '#0F766E', 'lineColor': '#23211C'}}}%%
flowchart LR
  A["U1: Array — insert, delete, search, traverse"] --> B["U1: 2D array — matrix addition and transpose"]
  B --> C["U2: Node — data + pointer"] --> D["U2: Singly linked list — insert, delete, traverse"]
  D --> E["U2: Doubly linked list"] --> F["U3: Stack — LIFO, push, pop, peek"]
  F --> G["U3: Application: balanced parentheses check"] --> H["U3: Queue — FIFO, enqueue, dequeue"]
  H --> I["U4: Linear search"] --> J["U4: binary search"]
  J --> K["U4: Bubble sort and insertion sort"] --> L["U4: Quick Sort and Selection Sort"]
  L --> M["U5: Binary Search Tree — insert, search"] --> N["U5: inorder/preorder traversal trace"]
  N --> O["U5: adjacency list representation"] --> P["U5: BFS and DFS — trace on a small graph"]

Rapid recall

  1. U1 · Data structure — definition, need, classification
  2. U1 · Algorithm efficiency — Big O notation
  3. U1 · Array — insert, delete, search, traverse in C/Python
  4. U1 · 2D array — matrix addition and transpose
  5. U1 · NumPy array (AI/ML)
  6. U2 · Node — data + pointer
  7. U2 · why linked list over array
  8. U2 · Singly linked list — insert (head, tail, position), delete, traverse
  9. U2 · Doubly linked list — concept and structure
  10. U2 · Circular linked list — concept
  11. U3 · Stack — LIFO, push, pop, peek
  12. U3 · implementation using array in C/Python
  13. U3 · Application: balanced parentheses check
  14. U3 · function call stack
  15. U3 · Queue — FIFO, enqueue, dequeue
  16. U3 · Application: print spooler
  17. U3 · task scheduling queue
  18. U3 · Polish Notation
  19. U4 · Linear search
  20. U4 · binary search
  21. U4 · comparison of searching
  22. U4 · Bubble sort and insertion sort
  23. U4 · Quick Sort and Selection Sort — trace
  24. U4 · Merge sort — concept and trace
  25. U4 · Python built-in sort() and sorted()
  26. U5 · Binary tree — terminology and properties
  27. U5 · Binary Search Tree — insert, search
  28. U5 · inorder/preorder traversal trace
  29. U5 · Graph — vertex, edge, directed/undirected
  30. U5 · adjacency list representation
  31. U5 · BFS and DFS — trace on a small graph
  32. U5 · Decision Tree in ML = binary tree

Final-answer checklist

  • For ACS201, draw the array, node chain, stack, sorted list, or tree before writing the answer.
  • Show the state after each operation; the examiner awards step marks on intermediate traces.
  • State the complexity with the correct operation attached (access, search, insertion, or sort), not as an isolated buzzword.
  • For BST and graph questions, give inorder/preorder or BFS/DFS as an explicit comma-separated sequence.
  • End with the requested final value: top element, front element, sorted array, or traversal order.