edubun 한국어

Binary search trees

A balanced binary search tree of 1,023 keys finds any of them in at most 10 comparisons; built from sorted input, the same keys can take 1,023.

Binary search, built into the shape

Binary search on a sorted array finds a key in about log₂ n checks, but inserting into a sorted array moves values. A hash table inserts quickly, but it forgets order: it cannot easily tell you the smallest key or the next key after 40. A binary search tree (BST) aims for both. It is made of nodes, like a linked list, but each node has two pointers, left and right. The top node is the root; a node with no children is a leaf.

One rule makes it a search tree: for every node, all keys in its left subtree are smaller, and all keys in its right subtree are larger. To search for a key, start at the root and compare. Equal means found. Smaller means go left, larger means go right. Each comparison throws away a whole subtree, just as binary search throws away half the list. Searching the tree 50, 30, 70, 20, 40, 60, 80 for 60 compares with 50, then 70, then 60: 3 comparisons.

Inserting follows the same path. Go left or right until the pointer you need is empty, and hang the new node there. Inserting 45 compares with 50 (go left), 30 (go right) and 40 (go right), and 40 has no right child, so 45 becomes it. Deleting a node that is not a leaf takes one more idea: MIT's notes swap its item with the next or previous key in order and delete from there instead. The counting rule in the lab is one comparison per node on the path.

Loading

Drag to turn. Scroll or pinch to zoom.

A binary search tree. Choose a key and insert or search; the path lights up and the counter shows comparisons. Change the starting tree to "sorted input" to see the same seven keys form a long chain.

Height is everything

How many comparisons can a search take? At most one per level, so the cost depends on the tree's height, the number of edges on the longest path from the root down to a leaf. MIT's notes put it this way: tree operations run in O(h) time. The tree 50, 30, 70, 20, 40, 60, 80 has height 2, so no search needs more than 3 comparisons. A perfectly balanced tree with 1,023 nodes has height 9 and needs at most 10.

But the shape depends on insertion order. Insert 10, 20, 30, 40, 50, 60, 70 in sorted order and each key is larger than everything before it, so each new node goes to the right of the last one. The tree becomes a chain of height 6, and searching for 70 takes 7 comparisons: a linked list with extra pointers. With 1,023 sorted keys the worst search takes 1,023 comparisons instead of 10.

Balanced trees fix this by reshaping themselves after each insert or delete. The first scheme was the AVL tree, from Georgy Adelson-Velsky and Evgenii Landis in 1962, which MIT's lecture 7 covers. It keeps, at every node, the heights of the two subtrees within 1 of each other, and repairs any break with small local moves called rotations. That keeps the height O(log n), so search, insert and delete stay O(log n) whatever order the keys arrive in.

Keys in order for free

A BST keeps more than membership. Visit the left subtree, then the node, then the right subtree, recursively, and the keys come out in sorted order. This in-order walk (MIT calls it traversal order) is how a BST answers "smallest key", "largest key" and "next key after 40", the order operations of MIT's Set interface that a hash table cannot do quickly.

Root and leafThe root is the top node; a leaf has no children.
BST propertyLeft subtree keys are smaller, right subtree keys are larger, at every node.
HeightEdges on the longest root-to-leaf path. Search takes at most height + 1 comparisons.
AVL treeA balanced BST (1962): subtree heights differ by at most 1, fixed by rotations.
In-order walkLeft, node, right: lists the keys in sorted order.
  1. Which statement is the binary search tree property?
    • Every node has exactly two children
    • Left subtree keys are smaller and right subtree keys are larger, at every node
    • Keys are stored in insertion order
    • The root is the smallest key
  2. Tree built from 50, 30, 70, 20, 40, 60, 80. How many comparisons does searching for 60 take?
  3. Same tree: searching for 45, which is missing, takes how many comparisons?
  4. Insert 45 into that tree. Where does it go?
    • Left child of 50
    • Right child of 40
    • Left child of 60
    • Right child of 30
  5. What is the height of the tree 50, 30, 70, 20, 40, 60, 80?
  6. Insert 10, 20, 30, 40, 50, 60, 70 in that order. How many comparisons does searching for 70 take?
  7. A perfectly balanced BST has 1,023 nodes (height 9). What is the most comparisons a search can take?
  8. Put the keys of the tree 50, 30, 70, 20, 40 in the order an in-order walk visits them.
  9. Who proposed the first balanced tree scheme, and when?
    • Adelson-Velsky and Landis, 1962
    • Luhn, 1953
    • Bauer and Samelson, 1957
    • Knuth, 1998
  10. Which question can a BST answer quickly but a plain hash table cannot?
    • Is 40 stored?
    • What is the next key after 40?
    • How many buckets are there?
    • What is 40 mod 7?
Key points
  • In a BST, smaller keys go left and larger keys go right at every node.
  • Search and insert follow one path: at most height + 1 comparisons.
  • Sorted input makes a chain (7 keys, height 6); balanced trees like AVL (1962) keep height O(log n).
  • An in-order walk lists the keys in sorted order.
Sources for this course (5)
  • MIT OpenCourseWare 6.006 Introduction to Algorithms, Spring 2020 (Demaine, Ku, Solomon), lecture notes 2 Data Structures (interface vs data structure, Sequence and Set interfaces, stack = insert_last/delete_last, queue = insert_last/delete_first, array, linked list, dynamic array, amortized Θ(1) append), 4 Hashing (chaining, pigeonhole collisions, chain length n/m), 6 Binary Trees Part 1 (height, traversal order, O(h) operations) and 7 Binary Trees Part 2: AVL (Adelson-Velsky and Landis, 1962) at ocw.mit.edu/courses/6-006-introduction-to-algorithms-spring-2020/pages/lecture-notes
  • MDN Web Docs, Array.prototype.shift (shifts all values left by 1), Array.prototype.unshift, Map (insertion order, Map vs Object, spec requires sublinear average access) and Set (developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects)
  • ECMAScript 2025 Language Specification (tc39.es/ecma262), 24.1 Map Objects and 24.2 Set Objects ("must be implemented using either hash tables or other mechanisms that, on average, provide access times that are sublinear on the number of elements"), Array.prototype.shift steps (Repeat, while k < len: move element k to k - 1)
  • D. E. Knuth, The Art of Computer Programming vol. 3 (2nd ed. 1998) section 6.4 History (H. P. Luhn, IBM internal memorandum January 1953, hashing with chaining; digit pairs added mod 10, 31415926 -> 4548) via gwern.net copy, and IEEE Spectrum, "Hans Peter Luhn and the Birth of the Hashing Algorithm" (2018)
  • "A brief history of the stack", SIGCIS (sigcis.org/files/A brief history.pdf: Bauer and Samelson applied for the patent in 1957, US 3,047,228 granted July 31, 1962; IPL list processing by Newell, Simon and Shaw at RAND 1956) and Wikipedia, Information Processing Language (Newell, Shaw, Simon, RAND and Carnegie Institute of Technology, about 1956).

Checked: 2026-10-18