Heap的讲解 - 介绍

A priority queue is an abstract data type similar to a regular queue or stack data structure in which each element additionally has a “priority” associated with it. In a priority queue, an element with high priority is served before an element with low priority.

In daily life, we would assign different priorities to tasks, start working on the task with the highest priority and then proceed to the task with the second highest priority. This is an example of a Priority Queue.

A common misconception is that a Heap is the same as a Priority Queue, which is not true. A priority queue is an abstract data type, while a Heap is a data structure. Therefore, a Heap is a way to implement a Priority Queue.

There are multiple ways to implement a Priority Queue, such as array linked list. However, these implementations only guarantee O(1) time complexity for either insertion or deletion, while the other operation will have a time complexity of O(N). On the other hand, implementing the priority queue with Heap will allow both insertion and deletion to have a time complexity of O(logN).


A Heap is a special type of binary tree. A heap is binary tree that meets the following criteria:

  • is a complete binary tree
  • the value of each node must be no greater than (or no less than) the value of its child nodes.

A Heap has the following properties:

  • Insertion of an element into the Heap has a time complexity of O(log N)
  • Deletion of an element from the Heap has a time complexity of O(log N)
  • The maximum/minimum value in the Heap can be obtained with O(1) time complexity

There are two kinds of heaps: Max Heap and Min Heap.

  • Max Heap: Each node in the Heap has a value no less than its child nodes. Therefore, the top element (root node) has the largest value in the Heap
  • Min Heap: Each node in the Heap has a value no larger than its child nodes. Therefore, the top element (root node) has the smallest value in the Heap.

这里说明一下,什么是complete binary tree
In a complete binary tree, all the levels of a tree are filled entirely except the last level. In the last level, nodes might or might not be filled fully. Also, let’s note that all the nodes should be filled from the left.

おすすめ

転載: blog.csdn.net/BSCHN123/article/details/121449201