Heap in python

Heap in python

Heap (Heap) is a special complete binary tree data structure, there are two types: large top heap and small top heap. In a large top heap, the value of a parent node is greater than or equal to the value of its child nodes, while in a small top heap, the value of a parent node is less than or equal to the value of its child nodes.

Features:

  1. A heap is a complete binary tree, meaning that the heap is full when all levels except the last level are filled, and the nodes in the last level are all arranged sequentially from left to right.
  2. In a large top heap, the value of each node is greater than or equal to the value of its child nodes; while in a small top heap, the value of each node is less than or equal to the value of its child nodes.
  3. No node in the heap is guaranteed to be the largest or smallest value of nodes in its subtree.

Common operations:

Heaps are usually used in scenarios such as priority queues and sorting algorithms (such as heap sort). The following are common operations on the heap:

  • Insert operation: Insert an element into the heap and maintain the properties of the heap.
  • Delete operation: delete the root node in the heap, and maintain the nature of the heap.
  • Build a heap: The process of converting an input data set into a heap.
  • Heap operation: restore the nature of the heap by sinking (downward comparison and exchange) or floating (upward comparison and exchange).

Method to realize:

In Python, the heap can be implemented using heapqthe library . heapqThe library provides functions to manipulate the heap, such as insertion, deletion, and construction.

The following is a sample code showing how to create and manipulate a small top heap:

import heapq

heap = []  # 创建一个空堆

heapq.heappush(heap, 5)  # 插入元素5
heapq.heappush(heap, 2)  # 插入元素2
heapq.heappush(heap, 8)  # 插入元素8
print(heap)  # 输出: [2, 5, 8]

min_value = heapq.heappop(heap)  # 删除并返回最小值
print(min_value)  # 输出: 2
print(heap)  # 输出: [5, 8]

Using heapqthe library can easily perform heap-related operations. In addition, you can also customize the comparison function to implement a large top heap or a heap with specific requirements.

Application scenario:

Heaps are used extensively in many algorithms and data structures, including:

  • Heap sorting: The heap sorting algorithm uses the nature of the heap to sort, and the time complexity is O(nlogn).
  • Priority queues: Heaps are often used to implement priority queues, where the element with the highest (or lowest) priority is always at the root node.
  • Graph algorithms: Heaps can be used for shortest path algorithms (such as Dijkstra's algorithm) and minimum spanning tree algorithms (such as Prim and Kruskal algorithms), etc.
  • Median Find: Uses two heaps to implement fast finding the median of an unsorted dataset.

As an important data structure, the heap provides efficient solutions in many scenarios. It has good time complexity and flexible applicability, so it is widely used in algorithm and software development.

Guess you like

Origin blog.csdn.net/qq_51447496/article/details/131317895