Skip to main content
← Advanced CS Study Guide

Code Scholars Tutoring · Advanced CS

6.4 Balance and Degeneration

How it works

Inserting sorted keys into an unbalanced BST produces a chain, and search degrades from logarithmic to linear. Self-balancing trees exist to prevent exactly this.

State the shape assumption whenever quoting logarithmic search.

Common error

Quoting log n for a BST unconditionally. That holds only while the tree stays balanced.

Worked example

Example

Insertion order in an unbalanced BST: 1, 2, 3, 4
Shape: each key becomes the right child of its predecessor
Search for 4: visits every node

A sorted insertion order can make an unbalanced BST behave like a linked list. Search time depends on height: balanced height supports logarithmic paths, while a chain can have linear height.

Keys 1, 2, 3, 4 form a chain of right children.
Sorted insertion without balance can create linear-height search paths.

Exercises

6.4.1 · Explain

How many nodes may a search for the last key visit in a chain of n nodes?

Show solution

It may visit all n nodes, giving linear worst-case search time.

6.4.2 · Explain

Why does balance improve worst-case search without changing the BST ordering rule?

Show solution

Balance limits height while preserving the smaller-left, larger-right ordering. A search therefore makes fewer sequential decisions.

Open Java compiler

Need help with this topic?

Work through questions with a tutor and get help with the steps you find difficult.

Get help with this topic