Picture the registration desk of a training centre. Roll numbers are handed out one after another, 1001, 1002, 1003, and each one is typed into a binary search tree as it is issued. Every new roll number is larger than all the ones before it, so every new key goes to the right of the last one. You saw the result at the end of the last chapter: the tree becomes a chain, a thousand keys hanging one under the other to a height of 999, and finding a roll number takes as long as walking down a linked list. A structure that was supposed to find a key in about ten steps needs a thousand, and the only thing that changed was the order in which the keys arrived.
Now a second scene, in a different building. The emergency desk of a government hospital receives patients all day, but the doctor does not see them in the order they walked in. She sees the one in the worst condition first, and when a new patient arrives, that patient may jump the whole line. A bank's IT help desk does the same with tickets, and an operating system does it when it picks the next program to run. All three need a line that can always hand over its most urgent member at once, however many people are waiting and however they arrive.
This chapter answers both scenes with two trees. The first, the AVL tree, is a binary search tree that notices when it is leaning and rotates itself straight again, so that no order of arrival can spoil it. The second, the heap, is a tree that gives up searching altogether and does one job perfectly: it keeps the most important key at the top, and it does so inside a plain array. The structure built on it is the priority queue.