Context for question 3
A binary tree is designed to store up to 100 positive integers. The binary tree stores data in ascending numerical order.
The data in the binary tree is created as a global 2D array with the identifier BinaryTree.
Each node has three values:
- a pointer to the array index with the left node
- the integer data
- a pointer to the array index with the right node.
A null pointer is represented by the integer –1. All nodes are initialised with the null data –1.
The table has example data for a binary tree:
| index | left pointer | data | right pointer |
|---|---|---|---|
| 0 | 1 | 20 | 2 |
| 1 | 4 | 10 | –1 |
| 2 | 3 | 26 | –1 |
| 3 | –1 | 22 | –1 |
| 4 | –1 | 8 | –1 |
The binary tree has two global pointers:
- TreeRootPointer, initialised to –1, stores the index of the root node of the binary tree
- FirstFreeNodePointer, initialised to 0, stores the index of the first node that does not store any data.
Data cannot be removed or replaced from the tree. FirstFreeNodePointer is incremented each time a node is added to the binary tree.