MCS-208 · Unit 6

Trees

Section 4 of 8

Block 2 · Unit 6Trees10 min · Section 4 of 8

6.4Binary Trees and Their Properties

Covers IGNOU MCS-208 §6.5

A binary tree is the most important kind of tree in computing. Each node has at most two children, called left and right, which makes it ideal for yes/no decisions, comparisons and two-operand expressions.

Definition

Two consequences of this definition:

  • A binary tree may be empty, unlike a general tree.
  • Left and right are different: a root with only a left child and a root with only a right child are two different binary trees.

Special binary trees

NameMeaningExample (nodes)
Strictly binary (full, in many books)Every node has 0 or 2 childrenExpression trees
Perfect (called “full” in the study material)All levels completely filled: 2h−12^h - 1 nodes for height hh1, 3, 7, 15, … nodes
CompleteAll levels full except possibly the last, which is filled from the leftUsed by heaps (Unit 9)
SkewedEvery node has only a left (or only a right) childBehaves like a linked list

Properties

For a binary tree with nn nodes and height hh (number of levels):

  1. It has exactly n−1n - 1 edges.
  2. Level ℓ\ell has at most 2ℓ−12^{\ell - 1} nodes.
  3. It has at most 2h−12^h - 1 nodes.
  4. Its height satisfies ⌈log⁡2(n+1)⌉≤h≤n\lceil \log_2 (n + 1) \rceil \le h \le n: at least the height of a perfect tree with nn nodes, at most nn for a skewed tree.
  5. If n0n_0 is the number of leaves and n2n_2 the number of nodes with two children, then n0=n2+1n_0 = n_2 + 1.
  6. It has exactly n+1n + 1 NULL links.

Proof. Let n1n_1 be the number of nodes with one child. Counting nodes: n=n0+n1+n2n = n_0 + n_1 + n_2. Counting edges by children: n−1=n1+2n2n - 1 = n_1 + 2n_2. Subtracting the second equation from the first gives 1=n0−n21 = n_0 - n_2. ∎

Key takeaways

  • Binary tree: empty, or root + left binary subtree + right binary subtree; left and right are distinct.
  • Perfect: 2h−12^h - 1 nodes; complete: last level filled from the left; strict: 0 or 2 children.
  • ⌈log⁡2(n+1)⌉≤h≤n\lceil \log_2(n + 1) \rceil \le h \le n; at most 2ℓ−12^{\ell - 1} nodes on level ℓ\ell.
  • n0=n2+1n_0 = n_2 + 1; a binary tree with nn nodes has n+1n + 1 NULL links.

Practice questions

Answer on paper first, then check. The explanation matters more than the letter you chose.

Question 1 · multiple choice

What is the maximum number of nodes on level 5 of a binary tree (root at level 1)?

Show answer

Answer: C. 16

25−1=162^{5 - 1} = 16. 31 is the maximum for the whole tree of height 5.

Question 2 · multiple choice

A binary tree has 12 leaves. How many nodes have exactly two children?

Show answer

Answer: A. 11

n0=n2+1n_0 = n_2 + 1, so n2=12−1=11n_2 = 12 - 1 = 11. The number of one-child nodes cannot be found from this information.

Question 3 · multiple choice

How many NULL links does a binary tree with 25 nodes have?

Show answer

Answer: D. 26

2×25=502 \times 25 = 50 pointers, 2424 used for edges, 50−24=26=n+150 - 24 = 26 = n + 1.

Question 4 · multiple choice

What is the minimum height (number of levels) of a binary tree with 100 nodes?

Show answer

Answer: B. 7

⌈log⁡2101⌉=7\lceil \log_2 101 \rceil = 7, since 6 levels hold at most 63 nodes and 7 levels up to 127. 100 is the maximum height (a skewed tree).

Question 5 · multiple choice

Which tree is complete but not perfect?

Show answer

Answer: C. A root with two children, where only the left child has one (left) child

Levels 1 and 2 are full and the last level is filled from the left. A root with two children is perfect; a missing left node before a present right node is not complete.

Question 6 · short answer

Prove that a non-empty binary tree with n0n_0 leaves and n2n_2 nodes of degree 2 satisfies n0=n2+1n_0 = n_2 + 1.

Show worked answer

Let n1n_1 be the number of nodes of degree 1 and nn the total. Every node has degree 0, 1 or 2, so n=n0+n1+n2n = n_0 + n_1 + n_2 (1). Every node except the root has exactly one parent edge, so the number of edges is n−1n - 1; counting edges from the parents’ side, a degree-1 node contributes 1 edge and a degree-2 node 2 edges, so n−1=n1+2n2n - 1 = n_1 + 2n_2 (2). Subtracting (2) from (1): 1=n0−n21 = n_0 - n_2, i.e. n0=n2+1n_0 = n_2 + 1.

↑ ↓ move↵ openTab switch type