3.4.6 · D1Trees

Foundations — BST — worst case O(n) — motivation for balancing

3,245 words15 min readBack to topic

Before you can understand why a BST can secretly become slow, you need a small toolbox of ideas. The parent note tosses around words like "node", "height", "", "", and "geometric series" as if you already own them. Here we forge each one, in an order where every new idea only uses ideas already built.


1. What is a tree? (node, edge, root, leaf)

Forget code for a second. A tree is just dots connected by lines, drawn top-down, where each dot has exactly one line coming from above it — except the very top dot, which has none.

Figure — BST — worst case O(n) — motivation for balancing

Why the topic needs this: every cost in a BST is counted by walking along edges from the root downward. If you can't see nodes and edges clearly, you can't count that walk. Look at the picture: the root is the yellow dot at top, leaves are the green dots at the bottom, and every blue line is one edge.

Link to the fuller definitions: Binary Search Tree — definition and property and Tree height and depth.


2. What is a Binary Search Tree? (the data structure itself)

We have a binary tree (each node has children). A Binary Search Tree is that same shape plus one ordering rule that turns it into a fast lookup machine.

Figure — BST — worst case O(n) — motivation for balancing

Why the topic needs this: everything in the parent note — the cost , the sorted-insert disaster, the motivation for balancing — is a property of this structure. If the BST is not defined, none of it has meaning.


3. Less-than, greater-than, and equal keys: , ,

The BST rule uses two tiny symbols. Let's not assume them.

Why the topic needs this: the BST property ("left < node < right") is what makes searching directional — at each node you can decide "go left" or "go right" and throw away the other side. That throwing-away is where speed comes from. Equal keys would break the decision, which is why we fix a rule for them.


4. Levels, height , and depth (the star of the show)

First, one word the doubling arguments secretly depend on.

Now the two most important quantities.

Figure — BST — worst case O(n) — motivation for balancing

Why the topic needs this: the parent note's headline claim is "cost = ". Every search/insert/delete walks one path from the root down, and is the length of the longest such path — the worst walk you could be forced to take. So is the cost. Everything else on the page is about how big or small can be.

The symbol is just a nickname (" for height") so we don't have to write "the height of the tree" every single time.


5. The letter : how many nodes

Why the topic needs this: we want to know how bad things get as the tree grows. Asking "how big can get for a tree of nodes?" only makes sense once names the size. The two bounds in the parent note, , both compare against this count .


6. Worst-case height: deriving from scratch

Before quoting the parent note's bound, let's build it.

Every non-empty level must contain at least one node (if a level were empty, nothing could hang below it, so the tree would already have ended). To reach height , the tree needs levels — that is distinct levels, each holding at least one node. So:

Rearranging, . And this is achieved exactly when every level holds precisely one node — the "stick" tree, where each node has a single child.

Why the topic needs this: this is the villain of the whole topic. We just showed why no tree can be taller and which shape hits the limit.


7. Doubling per level, and powers

Now the opposite question — how short can we make it? — needs one tool: the fact that levels can grow fast.

Why the topic needs this: the fact "level holds at most nodes" is the seed of the best-case height. Doubling grows so fast that even a short tree holds an enormous crowd.


8. Adding up the doublings: the geometric series

To find the total capacity of a tree that is full up to height , we add up every level's maximum.

Why the topic needs this: this formula tells us the maximum number of nodes a tree of height can hold. Turning that around ("if I have nodes, how few levels can I get away with?") produces the best-case height next.


9. Undoing the exponent: (and the best-case height)

The exponent asks: " to the power of what gives me ?" The answer to that question is the logarithm.

Now the best-case height. To hold all nodes in the fewest levels, pack every level full. From the capacity formula, a tree of height holds at most nodes, so we need:

Since must be a whole number, we round the requirement up: the smallest legal height is , which equals the parent note's for every (both give: , , , ). Either form names the same integer — the shortest a BST on keys can be.

Why the topic needs this: is the opposite tool to the exponent . Since capacity grows like , the height needed for nodes shrinks like . That floor is the fast, "bushy" end of the height range — the target every balancing scheme aims for.


10. Big-O: the shape-of-growth language

Finally, , , . These are not exact counts — they describe how the cost grows as the tree gets bigger, ignoring constants.

Why the topic needs this: Big-O is the ruler we measure heights with. Saying "cost is , and is between and " is the parent note's whole thesis compressed into one sentence.


How these foundations feed the topic

How to read this diagram: arrows mean "is needed to build". Start at the top boxes (raw ideas) and follow the arrows downward; each box only uses ideas from the boxes pointing into it. The bottom box, "Motivation for balancing", is the parent topic — everything flows into it.

Nodes and edges

Height h counts edges

Empty tree h = -1

Less-than and greater-than

BST data structure

Search walks one path

Cost equals O of h

n number of nodes

Worst height n minus 1

Doubling and powers 2 to i

Geometric series sum

Logarithm undoes exponent

Best height floor log2 n

Height bounds

Big-O growth language

Motivation for balancing


Equipment checklist

Answer each before reading the parent note. If any stumps you, re-read its section above.

In a tree, what is the difference between a node and an edge?
A node is a dot holding a key; an edge is a line connecting a parent to a child.
What is the empty tree and what is its height?
A tree with zero nodes; by convention its height is (so a single node comes out to height ).
State the BST property in one sentence.
For every node, all keys in its left subtree are less than it and all keys in its right subtree are greater than it.
What is one valid rule for handling a duplicate (equal) key in a BST?
Forbid duplicates (insert of an existing key does nothing), or always send equals to one fixed side using — pick one and keep it.
What is a "level" in a tree?
The set of all nodes at the same depth ( edges below the root).
Does height count nodes or edges on the longest path?
Edges — a single node has height .
What does stand for in this topic?
The number of nodes (keys) in the tree.
Why can't a tree of nodes have height greater than ?
Each of the levels needs at least one node, so , giving .
What is the maximum number of nodes on level , and why?
, because each node has at most 2 children, so capacity doubles each level.
What does equal, and how do you derive it?
, via double-and-subtract: .
In plain words, what does measure?
The exponent to raise 2 to, to get — i.e. how many times you can halve down to 1.
What do and do?
Round down / round up to the nearest whole number.
What does mean for a BST operation?
Its cost grows in proportion to the tree's height (up to a constant).
Why does beat dramatically for large ?
grows far slower — a million nodes is ~20 vs ~1,000,000 steps.

Recall The one-line summary of this whole page

Cost follows a path; a path's length is the height ; height depends on shape; shape depends on insertion order. Every symbol on this page (, , the geometric sum, , floor/ceiling, Big-O) exists to pin down how small or large can be — which is exactly the question the parent note answers.