Everything on the site.

Big-O notationAlgorithms · Count the steps in real loops, then compare n, n log n, n² and 2ⁿ on a live chart.RecursionAlgorithms · Watch frames push and pop on the call stack for factorial and Fibonacci.BFS vs DFSAlgorithms · A queue against a stack: draw a graph and see which order each search visits it in.Sorting algorithmsAlgorithms · Seven sorts on the same input, with comparison, swap and write counts side by side.Binary searchAlgorithms · Halve the range, probe the middle, repeat: with lo, mid and hi drawn on the array.Sets, relations and functionsDiscrete maths · Shade any set expression on a Venn diagram and test a relation for its properties.Proof by inductionDiscrete maths · Four worked proofs revealed one step at a time, and a broken one to diagnose.CombinatoricsDiscrete maths · An exact nPr and nCr calculator, and a Pascal’s triangle that shows its own addition rule.Boolean algebraDiscrete maths · The laws, De Morgan, and normal forms, with a truth-table generator built in.Finite-state machinesTheory · Build a DFA in a table, see it drawn, and run strings through it one symbol at a time.Domain theoryTheory · Why recursion needs least fixed points, and how CPOs, Scott continuity and powerdomains supply them.Truth table generatorTools · Expression in, truth table out: with DNF, CNF, a minimal form and LaTeX.Graph traversal visualiserTools · Draw a graph, then animate BFS, DFS and Dijkstra with the frontier shown.Recursion tree visualiserTools · Paste a recursive function or a recurrence and see its call tree and call count.Gradient descentML maths · Drag the starting point, set the learning rate, and watch it converge or blow up.Vectors and embeddingsML maths · Bend the plane with a 2×2 matrix and measure how close toy word vectors are.Softmax and cross-entropyML maths · Edit the logits and watch the probabilities, the loss and the gradient update live.Attention mechanismML maths · Q, K and V for three tokens, a live score heatmap, and the weighted sum of values.Image to video attentionML maths · One photo, 16 frames: switch reference attention on or off and watch the colours hold or drift.Formatting a CS paper in LaTeXLaTeX · Page limits, \documentclass, front matter, theorem environments and the bibliography.Camera-ready checklistLaTeX · Fonts embedded, PDF/A, copyright form, source bundle, and why final versions get rejected.LaTeX templatesLaTeX · Where the official classes live, plus a minimal article template you can copy.Boolean algebra calculatorTools · A Boolean expression simplifier that names the law used at every step.K-map solverTools · Fill in a Karnaugh map and see the groups, the minimal SOP and POS, and the essential terms.Logic gate simulatorTools · Wire up gates, flip the switches, and get the truth table and expression of your circuit.Regex to NFA converterTools · Type a regular expression and watch Thompson’s construction build its NFA, fragment by fragment.NFA to DFA converterTools · Enter an NFA or a regex and step through the subset construction, then minimise the DFA.DFA simulatorTools · Edit a DFA, run strings through it step by step, batch-test them and share the machine as a link.Regular expressions to automataTheory · Regex to NFA to DFA to minimal DFA, worked through on one example with every set shown.The halting problemTheory · Why no program can tell whether every program halts, with the paradox program played out.Pigeonhole principleDiscrete maths · n pigeons, k holes: why some hole gets ⌈n/k⌉, and the surprising things that follows from it.Dijkstra’s algorithmAlgorithms · Shortest paths with a priority queue: a worked table, a proof, and a graph you can redraw.Hash functionsAlgorithms · Keys in, buckets out: hash a word four ways, then watch chaining and probing handle collisions.Sorting algorithm visualizerTools · Up to seven sorts race on the same data, then step through any one of them line by line.LaTeX table generatorTools · Type or paste a table, merge cells, pick booktabs, and copy clean tabular code.Combination calculatorTools · Exact nCr with the cancelling shown, combinations with repetition, and a full list for small n.Permutation calculatorTools · nPr, nʳ, anagrams like MISSISSIPPI and round-table seatings, with exact working.AVL tree visualizerTools · Insert, delete and search, then replay every balance factor and rotation step by step.IEEE 754 converterTools · Decimal to float bits and back, with the exact stored value and why 0.1 isn’t exact.Two’s complement calculatorTools · Signed integers to bits and back, with the working, other encodings and overflow checks.Power set calculatorTools · Every subset of your set, sorted by size or bitmask, with 2ⁿ counted exactly.Huffman coding calculatorTools · Text or frequencies in, Huffman tree and codes out, with every merge shown in order.LaTeX matricesLaTeX · pmatrix, bmatrix, augmented and large matrices, each with copyable code and its result.AlgorithmsSection · Step-through visualisations of sorting, searching, recursion, graph traversal, Dijkstra’s algorithm, hash functions and Big-O growth, with pseudocode that follows along.Discrete mathsSection · Sets, relations and functions, proof by induction, counting, the pigeonhole principle and Boolean algebra, explained with worked examples and interactive checkers.TheorySection · Finite-state machines, regular expressions and automata, the halting problem, and the mathematical semantics of programming languages, explained from first principles.ML mathsSection · Gradient descent, vectors and embeddings, softmax, attention and image to video models, each with a formula, a worked numeric example and a live version.ToolsSection · Free browser tools: truth tables, K-maps, logic gates, nCr, power sets, regex to DFA, sorting, graph and AVL tree visualisers, IEEE 754, two’s complement and Huffman codes.LaTeXSection · How to format a CS conference or workshop paper in LaTeX, a camera-ready checklist, a minimal template you can adapt, and how to typeset matrices.
Free tool

AVL tree visualizer: insert, delete and watch every rotation

An AVL tree is a binary search tree that rebalances itself after every insertion and deletion, so its height stays within about 1.44 log₂ n. Type keys into the visualiser to watch each comparison, every balance factor that changes, and the left, right and double rotations that fix the tree.

Interactive: Animated AVL tree with step-through insert, delete and searchUpdated

AVL tree visualiserInsert, delete or search for a key, then step through every comparison and rotation.

  • Current node
  • Search path
  • Unbalanced (|balance| = 2)
  • Just rotated
  • Inserted or found

The small number beside each node is its balance factor: height of the left subtree minus height of the right.

Pseudocode

    Tree

    0Nodes
    0Height
    0Rotations
    0Plain BST height

    In-order traversal

    —

    What an AVL tree is

    A binary search tree keeps every key in a node’s left subtree smaller than the node and every key in its right subtree larger. Searching, inserting and deleting all walk one path from the root, so they cost time proportional to the tree’s height. The catch is that the height depends on the order the keys arrive in. Insert 1, 2, 3, …, 15 into a plain binary search tree and you get a chain of height 15, no better than a linked list.

    The AVL tree, published by Georgy Adelson-Velsky and Evgenii Landis in 1962 and named after them, was the first self-balancing binary search tree. It adds one rule:

    The AVL condition At every node, the heights of the left and right subtrees differ by at most one.

    The difference is the node’s balance factor,

    bf⁡(v)=h(v.left)−h(v.right)∈{−1,0,+1},\operatorname{bf}(v) = h(v.\text{left}) - h(v.\text{right}) \in \{-1, 0, +1\},

    where the height of an empty subtree is 0 and a leaf has height 1. (Some books count a leaf as height 0 and an empty tree as −1. Balance factors come out the same either way.) The visualiser prints each node’s balance factor beside it. A positive value means the node leans left, a negative value means it leans right.

    The four rotation cases

    An insertion or deletion changes heights only along the path it walked. On the way back up that path, each node’s height is recomputed. The first node whose balance factor reaches +2 or −2 is repaired with a rotation: a constant-time relinking of two or three nodes that keeps the in-order sequence of keys unchanged but moves height from the heavy side to the light side.

    Call the unbalanced node zz. Which rotation it needs depends on where the extra height is:

    CaseShapeFixTry it
    Left-left (LL)zz has balance +2 and its left child leans left or is levelOne right rotation at zz30, 20, 10
    Right-right (RR)zz has balance −2 and its right child leans right or is levelOne left rotation at zz10, 20, 30
    Left-right (LR)zz has balance +2 and its left child leans rightLeft rotation at the left child, then right rotation at zz30, 10, 20
    Right-left (RL)zz has balance −2 and its right child leans leftRight rotation at the right child, then left rotation at zz10, 30, 20

    Pick each case from the visualiser’s example list to see it happen. In a right rotation at zz, its left child yy moves up and zz becomes yy’s right child. yy’s old right subtree, whose keys lie between yy and zz, is reattached as zz’s new left subtree. Three links change and nothing else does. A left rotation is the mirror image.

    The double rotations exist because a single rotation can’t fix a zig-zag. If the extra height is in the inner grandchild (left child’s right subtree), a right rotation at zz just moves that grandchild to the other side and leaves the tree unbalanced the other way. Rotating the child first turns the zig-zag into a straight line, which one more rotation fixes. The middle key of the three ends up on top.

    Insertion, step by step

    1. Walk down from the root as in any binary search tree and attach the new key as a leaf.
    2. Walk back up the same path, recomputing each node’s height as 1+max⁡(h(left),h(right))1 + \max(h(\text{left}), h(\text{right})).
    3. At the first node with balance ±2, apply the matching rotation. After an insertion, one rotation (single or double) always restores the subtree to the height it had before the insertion, so nothing above it changes and the insertion is finished.

    Worked example: insert 10, 20, 30, 40, 50, 25

    1. 10, 20, 30. After 30, node 10 has an empty left subtree and a right subtree of height 2: balance −2, right-right case. A left rotation at 10 makes 20 the root with children 10 and 30.
    2. 40. It goes right of 30. Balance factors along the path are −1 (30) and −1 (20). No rotation.
    3. 50. It goes right of 40. Now 30 has balance −2, another right-right case. A left rotation at 30 lifts 40, giving 20 with children 10 and 40, and 40 with children 30 and 50.
    4. 25. Walk 20 → 40 → 30 and attach 25 as 30’s left child. Back at 20, the left subtree has height 1 and the right height 3: balance −2. The new key went into the right child’s left subtree, so this is the right-left case. Rotate right at 40, then left at 20.
    5. Result: 30 is the root. In level order the tree is 30, 20, 40, 10, 25, 50, its height is 3, and it took 4 rotations in total (two singles and one double).

    This is the visualiser’s starting example: press play to watch it, or use the arrow keys to step.

    Deletion

    Deletion starts like deletion from a plain binary search tree. A leaf is removed, a node with one child is replaced by that child, and a node with two children takes the key of its in-order successor (the smallest key in its right subtree), which is then deleted from the right subtree instead. That successor has at most one child, so the second deletion is easy.

    Then, as with insertion, walk back up recomputing heights and rotating where a balance factor hits ±2. There are two differences:

    • A deletion can leave the sibling subtree perfectly level (balance 0). That case is treated as a single rotation (LL or RR), which is why the delete pseudocode tests balance(left) ≥ 0 rather than comparing keys.
    • A rotation after a deletion can make the subtree one level shorter than it was, which may unbalance an ancestor. So deletion may rotate at several nodes on the way up, at most one per level, which is still O(log⁡n)O(\log n).

    Try the “bigger tree to delete from” example and delete 80, then 75: the first deletion triggers a rotation at the root.

    Why the height stays logarithmic

    Let N(h)N(h) be the fewest nodes an AVL tree of height hh can have. The sparsest such tree has a root, one subtree of height h−1h-1 and, to use as few nodes as possible, the other of height h−2h-2. Both subtrees must themselves be sparsest, so

    N(h)=N(h−1)+N(h−2)+1,N(1)=1,  N(2)=2.N(h) = N(h-1) + N(h-2) + 1, \qquad N(1) = 1,\; N(2) = 2.
    Height h12345678910
    Fewest nodes N(h)12471220335488143

    Adding 1 to both sides gives N(h)+1=(N(h−1)+1)+(N(h−2)+1)N(h) + 1 = (N(h-1) + 1) + (N(h-2) + 1), the Fibonacci recurrence, so N(h)=Fh+2−1N(h) = F_{h+2} - 1. Fibonacci numbers grow like φh\varphi^h with φ=(1+5)/2≈1.618\varphi = (1+\sqrt5)/2 \approx 1.618, and solving for hh gives

    h<log⁡φ(n+2)≈1.44log⁡2(n+2).h < \log_\varphi (n + 2) \approx 1.44 \log_2 (n + 2).

    So an AVL tree is never more than about 44% taller than a perfectly balanced tree. With a million keys the worst possible AVL height is 28, against 20 for a perfect tree, and a search needs at most that many comparisons. Inserting the sorted keys 1 to 1,023 gives a tree of height 10, the minimum possible, after 1,013 rotations; a plain binary search tree would have height 1,023.

    These numbers also answer two common exam questions: “What is the minimum number of nodes in an AVL tree of height 5?” (12) and “What is the maximum height of an AVL tree with 7 nodes?” (4, since N(4)=7N(4) = 7 but N(5)=12N(5) = 12).

    Running time

    OperationPlain BST (worst case)AVL tree (worst case)Rotations per operation
    SearchO(n)O(n)O(log⁡n)O(\log n)0
    InsertO(n)O(n)O(log⁡n)O(\log n)At most 1 (single or double)
    DeleteO(n)O(n)O(log⁡n)O(\log n)Up to O(log⁡n)O(\log n)
    In-order traversalO(n)O(n)O(n)O(n)0

    Each node stores its height (or just its balance factor, which needs two bits), so the space overhead is O(1)O(1) per node. See Big-O notation for what these bounds mean, and binary search for the same halving idea on a sorted array.

    AVL trees compared with red-black trees

    Red-black trees are the other classic balanced search tree, and the one most standard libraries use for ordered maps and sets. The trade-off is simple:

    • AVL trees are more tightly balanced (height at most about 1.44 log₂ n against 2 log₂ n for red-black trees), so lookups are slightly faster. They suit read-heavy workloads such as in-memory indexes.
    • Red-black trees rotate less on updates: at most two rotations per insertion and three per deletion, against a possible rotation at every level of an AVL deletion. They suit write-heavy workloads.

    Both guarantee O(log⁡n)O(\log n) for every operation. For data on disk, B-trees beat both, because they keep many keys per node and so touch far fewer disk blocks. A hash table is faster still for exact lookups, but it can’t answer ordered questions such as “the smallest key above 40” or list keys in order.

    Using the visualiser

    • Insert, Delete, Search act on the current tree and play the steps at once. Pause, then use the arrow keys, Home and End, or drag the slider to move through them.
    • Build from empty inserts a whole sequence, one key at a time, so you can replay every rotation. Up to 40 keys, each a whole number from −9999 to 9999.
    • Plain BST height is the height an ordinary binary search tree would have after the same insertions, which shows what the rotations saved.
    • The address bar updates as you go, so you can copy the link to share the current tree. It stores the keys in level order, which rebuilds exactly the same tree.

    References

    1. G. M. Adelson-Velsky and E. M. Landis, “An algorithm for the organization of information”, Soviet Mathematics Doklady 3 (1962), 1259–1263.
    2. D. E. Knuth, The Art of Computer Programming, Volume 3: Sorting and Searching, 2nd ed., Addison-Wesley, 1998. Section 6.2.3, balanced trees.
    3. T. H. Cormen, C. E. Leiserson, R. L. Rivest and C. Stein, Introduction to Algorithms, 4th ed., MIT Press, 2022. Chapter 13 and its AVL tree problem.
    4. R. Sedgewick and K. Wayne, Algorithms, 4th ed., Addison-Wesley, 2011. Section 3.3, balanced search trees.