6.2Binary Tree with an Array
A tree in an array: child and parent indexes
A binary tree can be kept in a plain array without pointers. The nodes are written into the array from the root down, level by level, left to right on each level. In this layout no node stores where its children are; a child's place is computed from the node's own index with a formula.
- How a binary tree is laid out in an array in level order
- The formulas left child
2*i+1, right child2*i+2and parent(i-1)/2 - How to tell whether a child exists by using
count
1Idea
The root goes into cell 0. Its two children go into 1 and 2, and the four nodes of the next level into 3, 4, 5 and 6. Each level takes twice the room of the one above it, so the node at index i has its left child at 2*i+1 and its right child at 2*i+2.
Going the other way, the parent of the node at index i is at (i-1)/2. This is integer division: nodes 5 and 6 both lead to node 2, through (5-1)/2 = 2 and (6-1)/2 = 2. The root has no parent, so the formula is not used for i = 0.
struct Tree holds the array and a count field. In this subtopic the tree always grows by inserting into the next free place, so it stays complete: the used cells run from 0 to count - 1 with no gaps. A child exists only if its index is less than count. An array with capacity 15 holds a full tree of height 3 exactly: 1 + 2 + 4 + 8 = 15.
Some books start the array at 1: the root is at 1, the children at 2*i and 2*i+1, the parent at i/2. In an exam, check which layout is meant. On this site indexes start at 0.
2Step by step
The drawing shows the tree on top and its array below. The number under a tree node is its index in the array. The child and parent indexes are computed first for 20, then for 30 and 60.
- Start. 10, 20, 30, 40, 50, 60 were added level by level.
countis 6; the used cells run from 0 to 5. i= 1, that is 20. In the tree 20 has two children; let us work out where they are in the array.left(1) = 2*1+1 = 3.items[3]is 40: the left child of 20.right(1) = 2*1+2 = 4.items[4]is 50: the right child of 20.parent(1) = (1-1)/2 = 0.items[0]is 10: the parent of 20, which is the root.i= 2, that is 30.left(2) = 5, anditems[5]is 60.right(2) = 6, but 6 ≥count: 30 has no right child.i= 5, that is 60.parent(5) = (5-1)/2 = 2.items[2]is 30. The division is integer division.i= 0, the root. The root has no parent.(0-1)/2gives 0 in C and Java and -1 in Python; neither makes sense, so the formula is never called for the root.
#define CAPACITY 15 struct Tree { int items[CAPACITY]; int count; // number of nodes in the tree}; void create(struct Tree* t) { t->count = 0;} int left(int i) { return 2 * i + 1;} int right(int i) { return 2 * i + 2;} int parent(int i) { return (i - 1) / 2; // never called for the root (i = 0)}class ArrayTree { int[] items; int count = 0; // number of nodes in the tree ArrayTree(int capacity) { items = new int[capacity]; } int left(int i) { return 2 * i + 1; } int right(int i) { return 2 * i + 2; } int parent(int i) { return (i - 1) / 2; // never called for the root (i = 0) }}class ArrayTree: def __init__(self, capacity): self.items = [None] * capacity self.count = 0 # number of nodes in the tree def left(self, i): return 2 * i + 1 def right(self, i): return 2 * i + 2 def parent(self, i): return (i - 1) // 2 # never called for the root (i = 0)The snippets focus on the operation in this lesson. The C examples need stdlib.h.
3Complexity
Each formula is one multiplication and one addition, or one subtraction and one division. The size of the tree does not matter.
The formulas ask for no memory. The tree itself holds as many cells as its capacity; that room is set aside at the start.
With linked nodes, moving to a child is O(1) too: root->left. But getting back to the parent needs an extra pointer. In an array the parent comes free with a formula.
4Common mistake
The most common mistake is using the formulas of a 1-based array on a 0-based one:
int left(int i) { return 2 * i; // wrong: left(0) = 0, the root becomes its own child}In a 0-based array the left child is 2*i+1 and the right child 2*i+2. Another mistake is computing a child's index and reading it without checking count. right(2) gives 6, but cell 6 is not part of the tree; it may hold an old or random value.
5Try it
In an array that starts at 0, at which indexes are the parent and the right child of node 7?
The answer is A. The parent is (7-1)/2 = 3. The right child is 2*7+2 = 16. 15 is the left child (2*7+1).
Show the answer
The answer is A. The parent is (7-1)/2 = 3. The right child is 2*7+2 = 16. 15 is the left child (2*7+1).
In a tree with count = 6, how many children does node 2 have?
The answer is B. The left child is 2*2+1 = 5, and since 5 < 6 it exists. The right child is 2*2+2 = 6, and since 6 < 6 is false it does not.
Show the answer
The answer is B. The left child is 2*2+1 = 5, and since 5 < 6 it exists. The right child is 2*2+2 = 6, and since 6 < 6 is false it does not.
Lessons you mark show up in the curriculum. If you are signed in, they are saved to your account too.