Array Representation of a Tree
Representing a tree with an array
You've seen two approaches to implementing a sequence data structure: either using an array, or using linked nodes. We extended our idea of linked nodes to implement a tree data structure. It turns out we can also use an array to represent a tree.
Here's how we implement a binary tree:
Self-test: ArrayBST representation
For each of the following self-tests, we will give you an instance of an ArrayBST. Match it to one of the four possible binary search trees below.
Tree 1 Tree 2 Tree 3 Tree 4
1 2 3 4
Self-test 1
ArrayBST
Which of tree options above could be represented by the ArrayBST shown here?
Tree 1
Correct! The ArrayBST suggests there is a root with one left child and no right child, and the left child has a left child.
Tree 2
Incorrect. The ArrayBST shows the root has no right child.
Tree 3
Incorrect. The ArrayBST shows the root has no right child.
Tree 4
Incorrect. The ArrayBST shows the root has no right child.
Check Solution
Self-test 2
ArrayBST
Which of tree options above could be represented by the ArrayBST shown here?
Tree 1
Incorrect. The ArrayBST shows that the root has a right child.
Tree 2
Correct! The ArrayBST shows that the root has both a left and right child.
Tree 3
Incorrect. The ArrayBST shows that the root has a left child.
Tree 4
Incorrect. The ArrayBST shows that the root has a left child.
Check Solution
Self-test 3
ArrayBST
Which of tree options above could be represented by the ArrayBST shown here?
Tree 1
Incorrect. The ArrayBST shows the root has no left child.
Tree 2
Incorrect. The ArrayBST shows the root has no left child.
Tree 3
Correct! The ArrayBST shows the root has a right child which has a left child.
Tree 4
Incorrect. Because there is nothing in position 7, the right child of the root can't have a right child.
Check Solution