Heap and Priority Queues

来源:互联网 发布:java写邮件qq邮箱 编辑:程序博客网 时间:2024/05/16 15:39

A heap is defined by two properties. First, it is a complete binary tree, so heaps are nearly always implemented using the array representation for complete binary trees. Second, the values stored in a heap  are partially ordered. This means that there is a relationship between the value stored at any node and values of its children. There are two variants of the heap, depending on the definition of this relationship.


A max-heap has the property that every node stores a value that is greater than or equal to the value of either of its children. It means the root stores the maximum of all values in the tree.


A min-heap has the property that every node stores a value that is less than or equal to that of its children. It means the root stores the minimum of all values in the tree. 


(Comp is a comparison class for comparing two elements. Comp defines method prior, a binary Boolean function that returns true if the first parameter should come before the second in the heap)





 

原创粉丝点击