← Course home

Python reference · Reference

Binary search tree

Walk ordered keys through ordinary and balanced search trees, and relate height to cost.

ReferenceAbout 2 min

This page lists the library modules for Binary search tree. The lesson is Binary search tree operations.

This page is the module index for binary search tree.

Start with BST operations, then study traversals and balanced search trees.

  • trees/bst.py — insert, search, delete, and in-order walk. O(h) per operation for height h.
  • trees/traversals.py — preorder, postorder, and level order in O(n) time.
  • trees/llrb.py — left-leaning red-black tree with rank and select in O(log n).

Industry

A working engineer reaches for a B-tree when keys must answer equality and range comparisons. PostgreSQL's index documentation says CREATE INDEX builds a B-tree by default, and that a B-tree handles <, <=, =, >=, and >. The binary search tree in this lesson is that comparison walk in memory. Sorted insertion makes its height linear, which is why the left-leaning red-black tree keeps the height logarithmic.

Implementation and tests Optional reference

Library code 3

  • bitsandbytes.trees.bst
  • bitsandbytes.trees.traversals
  • bitsandbytes.trees.llrb

Use these references to compare the explanation with repository code and tests. A test link means it imports a lesson symbol; it does not by itself claim complete behavior coverage.