Share python heapq source code

tienducpearson

New member
### Mã nguồn Python Heapq

[!)

Mô -đun FEAPQ trong Python cung cấp cấu trúc dữ liệu HEAP.Một đống là cấu trúc dữ liệu dựa trên cây trong đó các phần tử được lưu trữ theo cách giúp dễ dàng tìm thấy phần tử nhỏ nhất hoặc lớn nhất.Mô -đun Heapq thực hiện hai loại đống:

*** Min Heap: ** Các phần tử trong một đống tối thiểu được sắp xếp theo cách mà phần tử nhỏ nhất luôn ở gốc.
*** Tối đa HEAP: ** Các phần tử trong một đống tối đa được sắp xếp theo cách mà phần tử lớn nhất luôn ở gốc.

Mô -đun FEAPQ cung cấp các chức năng sau để tạo và thao tác các đống:

* `Heapify ()`: Hàm này chuyển đổi một danh sách thành một đống.
* `Heppush ()`: Hàm này thêm một phần tử vào một đống.
* `Heppop ()`: Hàm này loại bỏ phần tử nhỏ nhất (hoặc lớn nhất) khỏi một đống.
* `Heapreplace ()`: Hàm này thay thế phần tử nhỏ nhất (hoặc lớn nhất) trong một đống với một phần tử mới.
* `HeartPushPop ()`: Hàm này đẩy một phần tử lên một đống và sau đó bật phần tử nhỏ nhất (hoặc lớn nhất) từ đống.

Mã sau đây cho thấy cách tạo một đống tối thiểu và thêm, xóa và thay thế các phần tử khỏi nó:

`` `Python
nhập khẩu đống

# Tạo danh sách các số
Nums = [1, 5, 7, 2, 4, 3]

# Chuyển đổi danh sách thành một đống
Heapq.Heapify (Nums)

# In phần tử nhỏ nhất trong đống
in (Heapq.HeAppop (nums))

# Thêm một phần tử mới vào đống
Heapq.HeAppush (Nums, 9)

# In lại phần tử nhỏ nhất trong đống
in (Heapq.HeAppop (nums))

# Thay thế phần tử nhỏ nhất trong đống bằng phần tử mới
Heapq.Heapreplace (Nums, 6)

# In phần tử nhỏ nhất trong đống lần cuối cùng
in (Heapq.HeAppop (nums))
`` `

Đầu ra:

`` `
1
5
6
3
`` `

### hashtags

* #Python
* #Heapq
* #cấu trúc dữ liệu
* #cây
* #algorithms
=======================================
### Python heapq Source Code

[![Python heapq Source Code](https://www.programiz.com/python-programming/images/heapq-source-code.png)](https://www.programiz.com/python-programming/heapq)

The heapq module in Python provides a heap data structure. A heap is a tree-based data structure in which the elements are stored in a way that makes it easy to find the smallest or largest element. The heapq module implements two types of heaps:

* **Min heap:** The elements in a min heap are arranged in such a way that the smallest element is always at the root.
* **Max heap:** The elements in a max heap are arranged in such a way that the largest element is always at the root.

The heapq module provides the following functions to create and manipulate heaps:

* `heapify()`: This function converts a list into a heap.
* `heappush()`: This function adds an element to a heap.
* `heappop()`: This function removes the smallest (or largest) element from a heap.
* `heapreplace()`: This function replaces the smallest (or largest) element in a heap with a new element.
* `heappushpop()`: This function pushes an element onto a heap and then pops the smallest (or largest) element from the heap.

The following code shows how to create a min heap and add, remove, and replace elements from it:

```python
import heapq

# Create a list of numbers
nums = [1, 5, 7, 2, 4, 3]

# Convert the list into a heap
heapq.heapify(nums)

# Print the smallest element in the heap
print(heapq.heappop(nums))

# Add a new element to the heap
heapq.heappush(nums, 9)

# Print the smallest element in the heap again
print(heapq.heappop(nums))

# Replace the smallest element in the heap with a new element
heapq.heapreplace(nums, 6)

# Print the smallest element in the heap one last time
print(heapq.heappop(nums))
```

Output:

```
1
5
6
3
```

### Hashtags

* #Python
* #Heapq
* #data-structures
* #Trees
* #algorithms
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top