MCS-208 · Unit 6

Trees

Section 5 of 8

Block 2 · Unit 6Trees8 min · Section 5 of 8

6.5Representing Binary Trees in Memory

Covers IGNOU MCS-208 §6.6

A binary tree can be stored with pointers, as a set of linked nodes, or in an array, using positions instead of pointers. This page explains both and shows when each is suitable.

Linked representation

Each node holds a value and two pointers:

Node of a binary tree
struct node {
    struct node *left;    /* left subtree, or NULL  */
    int value;
    struct node *right;   /* right subtree, or NULL */
};

The tree is reached through a pointer to the root; an empty tree is a NULL root pointer. Leaves have both pointers NULL. This representation suits any shape of tree and allows nodes to be inserted and deleted freely.

Array (sequential) representation

Number the positions of a complete binary tree level by level, left to right, starting at 0. Store the node at position ii in A[i]. Then, for the node at index ii:

left child=2i+1,right child=2i+2,parent=⌊i−12⌋  (i>0).\text{left child} = 2i + 1, \qquad \text{right child} = 2i + 2, \qquad \text{parent} = \left\lfloor \frac{i - 1}{2} \right\rfloor \;(i > 0).

Positions where the tree has no node are marked as empty. For a complete tree no cell is wasted; for a skewed tree of height hh the array needs 2h−12^h - 1 cells for only hh nodes.

Comparison

LinkedArray (sequential)
MemoryTwo pointers per nodeNo pointers, but empty positions for missing nodes
Best forAny shape; frequent insertions and deletionsComplete trees (e.g. heaps)
Parent of a nodeNeeds an extra parent pointer⌊(i−1)/2⌋\lfloor (i - 1)/2 \rfloor
Size limitGrows with mallocFixed array size; a skewed tree of height hh needs 2h−12^h - 1 cells

Key takeaways

  • Linked: left, value, right; empty tree = NULL root.
  • Sequential (0-based): children at 2i+12i + 1, 2i+22i + 2; parent at ⌊(i−1)/2⌋\lfloor (i - 1)/2 \rfloor.
  • Empty positions must be kept; complete trees waste nothing, skewed trees waste a lot.
  • Program 6.1’s rule (smaller left, larger right) builds a binary search tree.

Practice questions

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

Question 1 · multiple choice

In the 0-based array representation, where is the right child of the node at index 6?

Show answer

Answer: C. 14

2⋅6+2=142 \cdot 6 + 2 = 14. 13 is its left child (2i+12i + 1).

Question 2 · multiple choice

Where is the parent of the node at index 9?

Show answer

Answer: A. 4

⌊(9−1)/2⌋=4\lfloor (9 - 1)/2 \rfloor = 4; node 9 is the left child of node 4 (2⋅4+1=92 \cdot 4 + 1 = 9).

Question 3 · multiple choice

A right-skewed binary tree (each node has only a right child) has 4 nodes. How many array cells does the sequential representation need?

Show answer

Answer: D. 15

The nodes are at positions 0, 2, 6, 14 (each right child is at 2i+22i + 2), so 15 cells are needed for 4 nodes. This is why the array form suits complete trees only.

Question 4 · multiple choice

What does this program print?

C · q6-array.c
#include <stdio.h>

int main(void)
{
    char t[] = "ABCDEFG";              /* a perfect tree in positions 0-6 */
    int i = 4;
    printf("%c parent=%c left=%s\n", t[i], t[(i - 1) / 2],
           (2 * i + 1 < 7) ? "exists" : "none");
    return 0;
}
Show answer

Answer: B. E parent=B left=none

Index 4 holds E; its parent is ⌊3/2⌋=1\lfloor 3/2 \rfloor = 1, which holds B; its left child would be at 9, outside the 7 positions, so E is a leaf.

Question 5 · multiple choice

Which application uses the array representation of a binary tree most naturally?

Show answer

Answer: B. A binary heap, which is always a complete binary tree

A complete tree fills the array without gaps. Trees that change shape freely are better stored with pointers.

Question 6 · short answer

Draw the sequential (array) representation of the binary tree in which A has children B and C, B has only a right child D, and C has children E and F.

Show worked answer

Number positions level by level from 0: A at 0; B at 1, C at 2; B’s left child position 3 is empty, D (B’s right child) at 4, E (C’s left) at 5, F (C’s right) at 6. Array: [A, B, C, –, D, E, F] (7 cells, one empty). Check: children of B (1) are at 3 and 4; children of C (2) at 5 and 6; parent of D (4) is ⌊3/2⌋=1\lfloor 3/2 \rfloor = 1 = B.

↑ ↓ move↵ openTab switch type