Skip what cannot be inside
transcript
A tree holds a hundred thousand ids. You need the total between two ids. Must you read every id? No. The order lets you skip.
The range is four to eight. Start at eight. Eight is inside. Add it. Look at both sides. Ten is above. Its right side is larger still. So skip fourteen. Go only to the left of ten.
Three is below four. Its left side is smaller still. So skip one. Go only to the right of three. Six is inside. Four is inside. Add them. The total is eighteen. Five of seven nodes were read.
Below the range, the whole left side is lower. Above the range, the whole right side is higher. Inside the range, read both sides.
Read the two edges and the ids between. On a balanced tree, that is about the height. Each id is read at most once. Full breakdown in the lecture.