Skip to content

Data Structures

College9 units and 33 lessons. Helio talks each one through and draws each step on a board.

Units

  1. Analysis of algorithms

    4 lessons
    1. Counting stepsCount the basic operations a function performs as a function of n, and compare two such counts, such as 3n + 2 and n², as n grows.
    2. Big-O, Big-Omega and Big-ThetaState O, Ω and Θ with the constants c and n₀, and prove with witnesses that 3n² + 5n is Θ(n²).
    3. Analyzing loopsFind the Θ running time of a loop from its code, by counting how many times its busiest line runs, for single, nested, dependent and halving loops.
    4. Dynamic arrays and amortized costExplain how a dynamic array doubles its capacity when it is full, and show that n appends copy fewer than 2n elements in all, so each append is O(1) amortized, while an insert at the front costs Θ(n).
  2. Linked lists

    3 lessons
    1. Singly linked listsBuild a singly linked list from Node objects with data and next, and show that adding at the head takes O(1) while finding a value takes O(n).
    2. Linked list insertion and deletionInsert a node after a given node and delete a node from a singly linked list by relinking next pointers, handling the head and the empty list, and say which of these operations cost O(1) and which O(n).
    3. Doubly linked lists and sentinelsLink nodes both ways with prev and next, use a header and a trailer sentinel so that insert and remove need no case for the ends, and say which operations cost O(1) here and which cost O(n) compared with a Python list.
  3. Stacks and queues

    3 lessons
    1. StacksImplement a stack with push, pop and peek on a Python list, each in O(1), explain why its top is the end of the list, and use it to check that brackets are balanced.
    2. QueuesImplement a queue as a circular array with a front index and a size, with rear = (front + size) % capacity, and as a linked list with a head and a tail, so that enqueue and dequeue are O(1) in both.
    3. Stack applications: postfix expressionsEvaluate a postfix expression with a stack, and convert an infix expression with + − * / and parentheses to postfix with the shunting-yard rules, keeping operators left associative.
  4. Recursion

    3 lessons
    1. Recursion and the call stackTrace a recursive function over a linked list as frames pushed and popped on the call stack, and use the trace to give its depth and space.
    2. Recurrences and divide and conquerWrite the recurrence for the running time of a recursive function, solve it by unrolling and by a recursion tree, and check the closed form.
    3. BacktrackingWrite a recursive function that generates all subsets or all permutations by choosing, exploring and undoing, and state how many results and calls it makes.
  5. Sorting

    4 lessons
    1. Selection and insertion sortTrace selection, insertion and bubble sort on one list, count each one's comparisons in the best and worst case, and say which are stable.
    2. Merge sortMerge two sorted lists in linear time, sort by splitting and merging, and give merge sort's cost, extra space and stability.
    3. QuicksortPartition a list around a pivot in place, trace quicksort, and give its comparisons, extra space and stability, best, average and worst.
    4. Sorting limits and counting sortShow with a decision tree that any sort that only compares items needs at least log₂(n!), which is Ω(n log n), comparisons, and sort small integer keys in Θ(n + k) with counting sort.
  6. Trees

    5 lessons
    1. Trees and binary treesBuild a binary tree from Node objects, using the words root, leaf, depth and height, and compute its size and height recursively.
    2. Tree traversalsList the nodes of a binary tree in preorder, inorder, postorder and level order, and give the time and extra space of each.
    3. Binary search trees: search and insertSearch and insert in a binary search tree by comparing keys from the root down, and give the cost as Θ(h), with the height h between log₂ n and n − 1.
    4. Binary search trees: deletionDelete a key from a binary search tree so that the search tree rule still holds, in the three cases of no child, one child and two children, and give the cost as Θ(h).
    5. Balanced trees: AVL rotationsCompute balance factors, find the lowest node that an insertion unbalances in an AVL tree, and fix it with a single or a double rotation, and say why this keeps every operation at O(log n).
  7. Heaps and priority queues

    3 lessons
    1. Binary heaps and insertionStore a complete binary tree in an array, using the index rules 2i + 1, 2i + 2 and (i − 1)//2, and insert into a min-heap by appending and sifting up, with a cost of O(log n).
    2. Removing the minimumRemove the smallest key of a min-heap by moving the last key to the root and sifting it down, tracing it on an array, and give its cost as O(log n).
    3. Building a heap and heapsortBuild a heap from any array in O(n) by sifting down from the last parent to the root, and sort an array in place with heapsort in O(n log n), tracing both.
  8. Hash tables

    3 lessons
    1. Hash functions and hash tablesMap keys to buckets with a hash function and the remainder by the table size, predict collisions, and say what makes a hash function good.
    2. Collisions: separate chainingResolve collisions with a list in each bucket, relate the load factor n/m to the expected cost of a search, and keep that cost constant by resizing and rehashing.
    3. Collisions: open addressingInsert and search with linear probing, see how clusters make probes grow with the load factor, and delete with tombstones so later searches still work.
  9. Graphs

    5 lessons
    1. Graphs and their representationsDefine vertices, edges, directed and weighted graphs and degree, and store one graph as an adjacency list and as an adjacency matrix, comparing their space and time.
    2. Breadth-first searchTraverse a graph level by level from a start vertex with a queue, find shortest paths counted in edges with distances and parents, and show that BFS runs in O(V + E).
    3. Depth-first searchTraverse a graph with recursive depth-first search and with an explicit stack, record the discovery order, and use DFS to find connected components and cycles.
    4. Topological sortOrder the vertices of a directed acyclic graph so every edge points forward, using Kahn's algorithm of repeatedly removing a vertex of in-degree 0, and detect a cycle when no such order exists.
    5. Shortest paths: Dijkstra's algorithmFind shortest paths from one vertex in a weighted graph with Dijkstra's algorithm, tracing its distance table with a priority queue.

A tutor you can talk to

30 minutes every month. No card.