liveBook · Manning

Self-balancing binary search tree

As explained in Clojure: The Essential Reference

A self-balancing tree is a search tree that maintains bounded height through structural adjustments as elements are inserted or removed.

A balanced tree keeps its height shallow through structural organization. Here, balanced trees support persistent vectors: updates copy only the changed leaf, root, and connecting path while sharing other nodes. section 12.1

The demonstrated design has these practical properties:

  • Uses 32-way branching and leaves containing up to 32 values, producing very shallow trees. section 12.1
  • Modifies persistent vectors by copying the path from an element to the root rather than copying the entire structure. section 12.1
  • Takes O(log n) time for element modification while behaving almost constantly in practice because the tree remains shallow. section 12.1
  • Extends to Relaxed Radix Balanced Trees, which support concatenation and insertion at O(log n) rather than O(n). section 12.1

Persistent vector update

A vector such as [:a :b :c] can be updated with (assoc [:a :b :c] 2 :d), producing [:a :b :d] while retaining structural sharing internally. section 12.1

How Clojure: The Essential Reference covers it

How deep it goes
Explains branching, path copying, shallow height, and asymptotic behavior, but does not describe binary-search ordering or rebalancing algorithms.
Examples
Illustrates persistent vectors with vector literals, assoc updates, a Pride and Prejudice tree figure, and billion-element depth estimates.
What you can do after
Evaluate how balanced-tree branching and path copying make persistent-vector updates efficient.
How it connects
Connects balanced trees with persistent data structures, vectors, structural sharing, and Relaxed Radix Balanced Trees.

Also explained in

Compare how 3 books explain self-balancing binary search tree →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Clojure: The Essential Reference. The section links point to the parts of the book it draws on.