Read about heaps and priority queues in Chapter 9 of An Open Guide to Data Structures and Algorithms.