Predict the output
What does this print?
tree = {6: (2, 8), 2: (0, 4), 8: (7, 9), 4: (3, 5)}
a, b = 3, 5
node = 6
while True:
if a < node and b < node:
node = tree[node][0]
elif a > node and b > node:
node = tree[node][1]
else:
break
print(node)Choose an answer, then open Show answer below to check it.
Show answer
Answer: A 4
Right: 3 and 5 both go left at 6, both go right at 2, then split at 4.
The walk stops at the first value between the two targets, which is the deepest node holding both.
Why not the others?
- B 2. Both 3 and 5 are larger than 2, so the walk continues right.
- C 6. Both values are smaller than 6, so the answer is below it.