Skip to content

Binary search tree

storing

Level 2 of 3

Also called Sorted binary tree.

fresh numbers

The test's questions that a run answers, asked again on new numbers whenever you like. The answer, its working and the reason for every other choice come from binary search tree's own run on the numbers shown.

choose one

question 1

An empty tree puts in 37, 38, 28, 75, 51 and 23, in that order. How many comparisons do the six puts make in all?

the answer, and why

the answer is D

Each put meets every number on its way down from the top: put by put, 0 + 1 + 1 + 2 + 3 + 2, 9 in all.

if you chose another

  • That counts every number before each put, as if the tree were a path.
  • That counts 1 comparison a put after the first, but some puts go deeper.
  • That counts only the last three puts.

new numbersthe test page's numbers

question 2

Which order of these seven numbers, put into an empty tree, costs the fewest comparisons?

the answer, and why

the answer is C

Putting in 32, 97, 27, 86, 64, 28, 2 makes 11 comparisons, the fewest of the four.

if you chose another

  • That order makes 12 comparisons, more than 11.
  • That order makes 14 comparisons, more than 11.
  • That order makes 19 comparisons, more than 11.

new numbersthe test page's numbers

question 3

An empty tree puts in 44, 8, 72, 51 and 95, then takes 44 out. How many comparisons do the six operations make?

the answer, and why

the answer is D

The puts make 0, 1, 1, 2 and 2, which is 6. The take meets 44 at the top, 1. 51, the smallest number to its right, takes its place, reached by following links with no comparison. That is 7.

if you chose another

  • That leaves out the take's comparison with 44.
  • That counts the walk to 72 and 51 as 2 comparisons; it only follows links.
  • That counts the walk to 51 as 1 comparison; it only follows links.

new numbersthe test page's numbers