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
1question 1
An empty tree puts in 50, 30, 70, 20, 40 and 60, in that order. How many comparisons do the six puts make in all?
the answer, and why
the answer is D
50 is the top, 0. 30 and 70 meet 50, 1 each. 20 and 40 meet 50 and 30, 2 each. 60 meets 50 and 70, 2. That is 8.
step 17 of 21
To put 60 in, start at the top.
numbers compared: 0
step 18 of 21
60 is bigger than 50, so go right.
numbers compared: 1
step 19 of 21
60 is smaller than 70, so go left.
numbers compared: 2
step 20 of 21
The way runs out, so hang 60 to the left of 70.
numbers compared: 2
Operation 6 puts 60 in. It meets 50, then 70, and hangs to the left of 70. 2 comparisons.
if you chose another
AThat counts every number before each put, as if the tree were a path.
BThat counts 1 comparison a put after the first, but 20, 40 and 60 hang on level 2.
Which order of these seven numbers, put into an empty tree, costs the fewest comparisons?
the answer, and why
the answer is B
45 is the middle, then 25 and 65 are the middles of each side. 45 costs 0, 25 and 65 cost 1 each, and the 4 numbers on level 2 cost 2 each, 8. That is 10.
if you chose another
AIn order, each number meets all before it, a path, 21.
C45 is the middle, but each side goes in order, a path on each side, 12.
D25 is not the middle, so the tree leans right, 12.
An empty tree puts in 30, 10, 50, 40 and 60, then takes 30 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 30 at the top, 1. 40, the smallest number to its right, takes its place, reached by following links with no comparison. That is 7.
step 18 of 24
Here is 30.
numbers compared: 1
step 20 of 24
Go right once, to 50.
numbers compared: 1
step 21 of 24
Go left, to 40.
numbers compared: 1
step 23 of 24
40 moves up to where 30 was, and 30 is out.
numbers compared: 1
Operation 6 takes 30 out. It is met at the top. Then the walk goes right to 50 and left to 40, and 40 moves up to where 30 was.
if you chose another
AThat leaves out the take's comparison with 30.
BThat counts the walk to 50 and 40 as 2 comparisons; it only follows links.
CThat counts the walk to 40 as 1 comparison; it only follows links.