A tree that sorts itself
transcript
A search tree holds seven numbers. They went in out of order. Walk the tree in one special order. Out comes a sorted list. No sort runs at the end. Each insert already put its value in place. Watch how.
Go left as far as you can. Ten has nothing smaller. Take it first. Then its parent, twenty five. Then thirty, the right child. Now the root, forty. Then the right side, the same way.
Take ten, twenty five and thirty. Ten is below twenty five, and thirty is above it. At every node, the left side is smaller. The right side is larger. So left, node, right always climbs. That holds at every node.
The walk visits each node once. That costs n steps for n values. The sorting work was paid earlier. Each insert walked down to its place.
The tree keeps the order. Every insert pays a little to keep it. Use it to list every price in order. Try the walks in the sandbox. Full breakdown in the lecture.