May 2019
Intermediate to advanced
698 pages
17h 21m
English
The heap data structure is surprisingly simple to implement. There are no lengthy unwraps, borrows, or other calls, and the pointer is owned by the Vec and can easily be swapped. Other than that, the upheap operation is only a while loop, just like the (slightly more complex) downheap function.
There is another typical use case for a heap though: sorting! Consider a bunch of numbers going into the heap instead of MessageNotification objects—they would come out sorted. Thanks to the efficiency of the upheap/downheap operations, the worst-case runtime of that sorting algorithm is great—but more on that in Chapter 19, Ordering Things.
Read now
Unlock full access