Height and complete trees

Intermediate Algorithms and Data Structures English
Also available: Deutsch
Created by Best · 16.08.2026 at 09:13 UTC

The height of a tree is the length of the longest path from the root to a leaf (conventions vary by 1; here: a leaf has height 0). A complete binary tree fills levels from left to right.

In a balanced tree, the height grows slowly with the number of nodes. In a degenerate chain, it grows proportionally to the number of nodes.

Many algorithms depend on height (search, insertion).

Diagram

$$h_{\mathrm{complete}} = \lfloor \log_2 n \rfloor$$

$$h_{\mathrm{complete}}=\lfloor \log n \rfloor$$

Where used

Balancing, worst-case search, memory layout of heaps. The height controls the latency of search structures and the stack depth of recursive walks.

Depth

The height measures the longest root-to-leaf path, depending on the convention in edges or nodes. This convention changes base values for empty trees and leaves, but not the asymptotic statement.

A fully filled tree doubles the maximum number of nodes with each additional level. Therefore, a height that is small in relation to the number of nodes is sufficient. A heavily unbalanced tree loses this advantage and behaves structurally like a list.

Full, perfect, and complete denote different structural conditions. For heap implementations, it is particularly important that all levels are filled except for the last, and that the last is filled from the left.

Difficulty levels

  1. Calculate height according to a specified convention.
  2. Relate the number of nodes and levels of a perfect tree.
  3. Distinguish between perfect, complete, and unbalanced shapes.

Pitfalls

Without a specified convention, height values are ambiguous by one. A large number of nodes also does not guarantee a small height if there is no balance condition.

University approvals: 0
Tasks
Question 1

What is the asymptotic height of a degenerate tree with n nodes (chain)?

Question 2

Why must a height problem specify the convention used?

Question 3

Calculate the tree height, where zero has a height of -1.

Hint

Math.max(a, b) returns the maximum of two int values. You check the empty reference with node == null.

Starter code is prefilled; replace TODO blocks with your solution.
1 test case will be used for grading
Run checks runtime behavior only. Final correctness is evaluated when you submit.
Card Info
  • Topic: Algorithms and Data Structures
  • Difficulty: Intermediate
  • Completed: 0 users
Creator
Best
Best
BestBuddy