liveBook · Manning

Tree height

As explained in Grokking Algorithms, Second Edition

Tree height is the maximum number of edges or steps on a downward path from the root to a leaf or other node.

Tree height measures the longest downward route from the root to a node. A height of 2 means every node is at most two steps from the root. section 8.3

Two trees, seven nodes

Two trees can contain the same seven nodes yet have very different heights. A short tree has height 2, while a tree whose nodes form a line has height 6. The short tree supports O(log n) searches; the tall tree behaves like a linked list and takes O(n). section 8.3

Height connects tree shape to search speed. A binary search tree is fastest when its height stays O(log n); inserting values in increasing order can stretch it to O(n). Balancing keeps the tree short. section 8.2 section 8.3

AVL trees use height-related information to decide when to rebalance:

  • Store each node’s height or its balance factor; either is sufficient. section 8.6
  • After insertion, update the height and balance factor for the inserted node’s ancestors. section 8.6
  • Rotate when the balance factor falls below −1 or rises above 1. section 8.6

Compare the shapes and heights of equally sized trees visually. section 8.3

How Grokking Algorithms, Second Edition covers it

How deep it goes
Explains height through concrete trees and complexity, then connects it to AVL updates without detailing rotation mechanics.
Examples
Uses seven-node trees, a degenerate right-leaning tree, and AVL insertion with updated heights and balance factors.
What it assumes
Assumes familiarity with binary search trees, nodes, subtrees, insertion, and asymptotic search time.
What you can do after
Evaluate tree shape and search cost, and diagnose when AVL height imbalance requires rebalancing.
How it connects
Connects tree height to binary search trees, binary search, linked lists, AVL trees, balance factors, and rotations.

Also explained in

Compare how 3 books explain tree height →

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.