Sub topic 1
Trees and Their Vocabulary
Prerequisites
This chapter assumes you know recursion, object references (pointers), Big-O notation, and arrays and linked lists. The code uses Python classes and the deque type from collections.
Trees and their vocabulary
A tree is a set of nodes connected by edges, with no cycles. One node is the root. Every other node has exactly one parent. The parent's direct descendants are its children.
| Term | Meaning |
|---|---|
| Root | The top node, with no parent |
| Leaf | A node with no children |
| Degree | The number of children of a node |
| Depth | Edges from the root down to a node |
| Height | Edges on the longest path from a node down to a leaf |
| Level | All nodes at the same depth |
Binary trees and their special forms
A binary tree is a tree where every node has at most two children, called left and right. Four special forms appear often in GATE questions:
- Full: every node has 0 or 2 children.
- Complete: every level is filled, except possibly the last, which fills from the left.
- Perfect: every internal node has 2 children, and all leaves share one level.
- Skewed: each node has one child, so the tree is a chain.
Node, leaf, and height formulas
Let the root be at level 0 and let height be measured in edges.
- A level
lholds at most2^lnodes. - A tree of height
hholds at most2^(h+1) - 1nodes. - A tree with
nnodes has height at leastfloor(log2 n). - A full binary tree with
iinternal nodes hasi + 1leaves.
Linked and array representations
A linked representation stores each node as an object with pointers to its children:
class Node:
def __init__(self, key, left=None, right=None):
self.key = key
self.left = left
self.right = rightA complete tree can use an array instead. Index 0 holds the root. For a node at index i, the left child is at 2i + 1, the right child is at 2i + 2, and the parent is at (i - 1) // 2. This saves pointer space, but gaps in the array waste memory for skewed trees.
Preorder, inorder, postorder, and level order
The three depth-first traversals differ only in when the node is visited. Level order visits nodes level by level using a queue.
from collections import deque
def preorder(node, out):
if node:
out.append(node.key)
preorder(node.left, out)
preorder(node.right, out)
def inorder(node, out):
if node:
inorder(node.left, out)
out.append(node.key)
inorder(node.right, out)
def postorder(node, out):
if node:
postorder(node.left, out)
postorder(node.right, out)
out.append(node.key)
def level_order(root):
out, queue = [], deque([root] if root else [])
while queue:
node = queue.popleft()
out.append(node.key)
if node.left:
queue.append(node.left)
if node.right:
queue.append(node.right)
return out
root = Node(1, Node(2, Node(4), Node(5)), Node(3))
for name, fn in [("pre", preorder), ("in", inorder), ("post", postorder)]:
seq = []
fn(root, seq)
print(name, seq)
print("level", level_order(root))Output:
pre [1, 2, 4, 5, 3]
in [4, 2, 5, 1, 3]
post [4, 5, 2, 3, 1]
level [1, 2, 3, 4, 5]Exercise
Exercise
What is the minimum height of a binary tree with 10 nodes?
Show answer
floor(log2 10) = 3. A tree of height 3 can hold up to 15 nodes, so 10 nodes fit.