liveBook · Manning

Binary search tree

As explained in Grokking Algorithms, Second Edition

A binary search tree is a binary tree whose nodes are ordered so that values in the left subtree precede the node's value and values in the right subtree follow it.

A binary search tree (BST) is a binary tree whose nodes follow an ordering rule: every value in a node’s left subtree is smaller, and every value in its right subtree is larger. Each node has at most two children. section 8.2

Searching for a value

To find 7 in a tree rooted at 10, compare 7 with 10 and move left. At 5, compare again and move right to find 7. To search for 8, follow the same path; the missing value would occupy the next appropriate position. Each comparison discards an entire subtree. section 8.2

A BST combines the fast searching associated with sorted data and the flexible insertion associated with linked structures. Its performance depends on height: a short tree supports O(log n) searches, while repeatedly inserting larger values can produce a one-sided tree with height O(n). section 8.2 section 8.3

Use the tree diagrams to trace comparisons, subtree choices, and the path taken during a search. section 8.2

Balancing keeps the height near O(log n). AVL trees are self-balancing BSTs that update heights and balance factors after insertion, then use rotations when necessary; they provide O(log n) searches and insertions. section 8.6

How Grokking Algorithms, Second Edition covers it

How deep it goes
Explains node ordering, search paths, height-based performance, and the need for balancing, but defers most AVL rotation cases.
Examples
Uses a tree containing 10, 5, and 20 to trace searches for 7 and 8, then shows a one-sided tree formed by increasing insertions.
What it assumes
Assumes familiarity with basic trees and contrasts their performance with arrays and linked lists.
What you can do after
Evaluate BST search and insertion trade-offs and understand how AVL balancing preserves logarithmic performance.
How it connects
Connects binary trees with sorted arrays, linked lists, tree height, balanced trees, AVL trees, and rotations.

Also explained in

Compare how 5 books explain binary search tree →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Grokking Algorithms, Second Edition. The section links point to the parts of the book it draws on.