Sub topic 2
Binary trees and their special forms
This chapter assumes you know basic recursion, object references, Big-O notation, and the vocabulary from the previous chapter: root, parent, child, leaf, depth, height, level, and degree. Here we restrict to binary trees and name the shapes that GATE questions rely on.
What a binary tree is
A binary tree is a tree in which every node has at most two children. The two children are ordered: one is the left child and the other is the right child. A node with one child therefore has two possible shapes: the child is on the left, or it is on the right. These are different binary trees.
This ordering is the main difference from a general tree. In a general tree, children are an unordered set, or at best a list. In a binary tree, "left" and "right" are part of the structure.
The empty tree (no nodes) is also a binary tree. Recursive code depends on this. A binary tree is either empty, or it is a root with a left subtree and a right subtree, and each subtree is itself a binary tree.
class Node:
def __init__(self, key):
self.key = key
self.left = None
self.right = None
def height(node):
# Height counted in edges: an empty tree is -1, a single node is 0.
if node is None:
return -1
return 1 + max(height(node.left), height(node.right))
root = Node(1)
root.left = Node(2)
root.right = Node(3)
root.left.left = Node(4)
print(height(root)) # 2The height convention
Before you use any formula, check how the question defines height. There are two conventions.
| Convention | Single node | Empty tree | Used when |
|---|---|---|---|
| Height in edges | 0 | -1 | The question says "longest path from root to a leaf" |
| Height in nodes (levels) | 1 | 0 | The question says "number of levels" or counts nodes |
The formulas in this chapter use height in edges, written h. If a question uses the node-count convention, replace h with h − 1 in the formulas, or equivalently add 1 to the results where levels appear. Many GATE answers differ only by this off-by-one error.
Special forms
Four shapes appear repeatedly. They are defined by which nodes are allowed to be missing.
Full binary tree
A full binary tree (sometimes called a strict or proper binary tree) is one where every node has either 0 or 2 children. No node has exactly one child.
Perfect binary tree
A perfect binary tree is one where every internal node has exactly two children and all leaves are at the same depth. Every level is completely filled.
Complete binary tree
A complete binary tree is one where every level is completely filled except possibly the last, and the last level has its nodes packed as far left as possible. Binary heaps use this shape.
Skewed binary tree
A skewed binary tree is one where every internal node has exactly one child. If all children are left children, it is left-skewed. If all are right children, it is right-skewed. It is effectively a linked list. A tree with a mixture of left and right single children (a zigzag) is also a degenerate tree, though some books reserve "skewed" for the pure left or pure right case.
How the forms relate
The forms overlap, but none implies all the others.
- Every perfect tree is full and complete.
- A complete tree need not be full. The six-node tree above has a node (3) with only a left child.
- A full tree need not be complete. The five-node tree above has a leaf at depth 1 while other leaves are at depth 2, and node 2 sits to the left of node 3 with no children, so the last level is not packed to the left.
- A skewed tree with more than one node is never full. A skewed tree with one or two nodes can be complete, for example a root with only a left child.
Checking a shape in code
You can test these definitions directly. The functions below return booleans and use the Node class from earlier.
from collections import deque
def is_full(node):
if node is None:
return True
if (node.left is None) != (node.right is None):
return False
return is_full(node.left) and is_full(node.right)
def is_complete(root):
# Level-order scan: once a gap appears, no real node may follow.
queue = deque([root])
seen_gap = False
while queue:
node = queue.popleft()
if node is None:
seen_gap = True
else:
if seen_gap:
return False
queue.append(node.left)
queue.append(node.right)
return True
def is_perfect(root):
h = height(root)
def count(node):
return 0 if node is None else 1 + count(node.left) + count(node.right)
return count(root) == 2 ** (h + 1) - 1is_perfect uses the fact that a perfect tree of height h has exactly 2^(h+1) − 1 nodes. A tree with fewer nodes at that height has a missing node somewhere. A tree cannot have more.
Shape and height bounds
For a binary tree with n nodes (n ≥ 1) and height h in edges:
| Form | Height h | Notes |
|---|---|---|
| Any binary tree, minimum height | ⌈log₂(n + 1)⌉ − 1 | Reached by perfect and complete trees |
| Any binary tree, maximum height | n − 1 | Reached by a skewed tree |
| Complete tree | ⌊log₂ n⌋ | Fully determined by n |
| Perfect tree | log₂(n + 1) − 1 | Only exists when n + 1 is a power of 2 |
| Skewed tree | n − 1 | One node per level |
The complete tree is the most compact shape for a given n, so its height is as small as any binary tree can achieve. This is why heaps and balanced trees stay shallow. The skewed tree is the worst case, and it is the reason a plain binary search tree can degrade to linear time later in this tutorial.
A worked check: a complete tree with 10 nodes has height ⌊log₂ 10⌋ = 3. Levels 0 to 2 hold 1 + 2 + 4 = 7 nodes, and the last level holds the remaining 3 nodes in its leftmost positions.
Common errors
- Treating a node with one child as having "two children, one empty" when applying the full-tree definition. A full tree forbids exactly one child.
- Calling any tree with all levels filled "complete" without checking left-packing of the last level.
- Using the height formulas without confirming whether height counts edges or nodes.
- Assuming left and right are interchangeable. A tree with a single left child and a tree with a single right child are different binary trees, even though they look alike as general trees.
- Assuming a full tree is balanced. A full tree can be long and thin: a spine where every node also has one leaf child is full but has height about n/2.
Practice
Exercise
Which of these is true of a binary tree with 7 nodes that is both full and complete?
Show answer
It must be perfect. Its height is 2, because a complete tree with 7 nodes has height ⌊log₂ 7⌋ = 2. Levels 0 and 1 hold 3 nodes, so level 2 holds 4 nodes, which is all of level 2. Every level is filled, so the tree is perfect.
Exercise
A complete binary tree has 12 nodes. What is its height in edges, and how many nodes are on its last level?
Show answer
Height is ⌊log₂ 12⌋ = 3. Levels 0 to 2 hold 1 + 2 + 4 = 7 nodes, so the last level holds 12 − 7 = 5 nodes, packed on the left.
Exercise
Give a binary tree with 5 nodes that is full but not complete, and explain why it is not complete.
Show answer
Take a root with a leaf as its left child and an internal node as its right child, where that internal node has two leaf children. Every node has 0 or 2 children, so it is full. The last level contains nodes only under the right child while the left child has no children, so the last level is not packed to the left. It is therefore not complete.