algoritmauzmanı
Sign in
TREN
6.3Binary Tree with Linked Nodes
Lesson 6.3.8

Array or linked nodes?

You coded the same binary tree in two ways: with an array and with linked nodes. The operations had the same names and came in the same order, and the traversals printed the same output. This lesson puts the differences side by side: memory, empty cells, insertion, reaching the parent and the shape of the tree. At the end you will know which one to pick and when.

What you will learn
  • Which operations cost the same in both versions and which do not
  • Why an array is efficient only for a complete tree
  • When to pick an array and when to pick linked nodes
5 min

1The same tree, two structures

In an array tree a node's place is an index and the children come from a formula: 2*i+1 and 2*i+2. With linked nodes every node keeps its children's addresses in its own left and right fields. For the code that uses the tree the difference is small: the traversals are the same three lines in both, only "go to the left child" is written differently.

// With an array (6.2)struct Tree {    int items[CAPACITY];    int count;          // number of nodes};// left child: items[2*i+1], right child: items[2*i+2] // With linked nodes (6.3)struct TreeNode {    int data;    struct TreeNode* left;    struct TreeNode* right;};

2Memory and sparse trees

In an array a node takes only the room of its value; there are no pointers. With linked nodes every node carries two pointers next to its value: on a 64-bit system an int and two pointers take 24 bytes with alignment. For a tree the array clearly uses less room.

If the tree is not complete, it is the other way round. In an array a node's place depends on its level and its position from the left; the gaps in between take cells too. A tree made only of right children with height h has h + 1 nodes but needs 2^(h+1) - 1 cells in an array: 1023 cells for 10 nodes. Linked nodes always take room for exactly n nodes. That is exactly the trouble with a BST in an array.

3The cost of the operations

The traversals and search visit every node once in both structures: O(n). In an array, is reading the array from start to end; with linked nodes it takes a queue. Inserting into the next free place is writing at index count in an array, O(1); linked nodes do not know where the last node is, so they search with a queue, O(n). The node count is ready in count in an array and has to be counted with linked nodes.

Reaching the parent is a formula in an array: (i-1)/2. A linked node does not know its parent; if needed, a parent pointer is added to the node or the parent is searched for from the root. On the other hand, with linked nodes moving a subtree to another place is a single pointer assignment; in an array every cell of the subtree has to be moved one by one.

4Summary table

PropertyWith an arrayWith linked nodes
Reaching children and parentFormula: 2i+1, 2i+2, (i-1)/2left, right; the parent needs an extra field
Memory per node (int)4 bytesAbout 24 bytes, plus the malloc record
Sparse, unbalanced treeMany empty cellsNo problem
CapacityFixedUntil memory runs out
Insert into the next free placeO(1)O(n), queue search
Preorder, inorder, postorderO(n)O(n)
Level orderRead the array, no queue neededWith a queue, O(n)
Moving a subtreeMove every cellOne pointer assignment
Fit for a binary search treePoor: empty cells pile upGood

5Which one to pick?

  • Array: when the tree always stays complete. The best-known example is the heap: it underlies priority queues and heapsort and is always coded with an array. Little memory, fast access, and reaching the parent is free.
  • Linked nodes: when the shape of the tree depends on the data. The binary search tree, expression trees and balanced trees are coded with linked nodes; subtrees move around as nodes are added and removed, and there are no empty cells to worry about.

In an exam the answer to "which is better" depends on the shape of the tree. An array for a complete tree; linked nodes for a sparse tree or one whose shape changes. Back your answer with the number of empty cells and the memory per node.

6Try it

A tree of 4 nodes made only of right children (height 3) needs at least how many cells in an array?

Show the answer

The answer is C. The root is at 0, its right child at 2, the next right child at 6 and the next at 14. To have room for index 14 the array needs 15 cells; 11 of them stay empty.

In which structure can you reach a node's parent in constant time (without adding a field)?

Show the answer

The answer is A. In an array the parent is at index (i-1)/2. A linked node knows only its children; for the parent you either add a parent field or search from the root.

Why is a heap coded with an array and a binary search tree with linked nodes?

Show the answer

The answer is A. A complete tree fits into an array with no gaps. A BST can grow unbalanced and sparse depending on insertion order; in an array that means many empty cells.

Lessons you mark show up in the curriculum. If you are signed in, they are saved to your account too.