Prerequisites:
- Cấu trúc dữ liệu nặng nhị phân
- Mô -đun Heapq trong Python
- Từ điển trong Python.
Từ điển có thể được duy trì trong đống dựa trên khóa hoặc dựa trên giá trị. Các quy ước được duy trì được liệt kê dưới đây:
- Cặp giá trị khóa tại Index I I được coi là cha mẹ của cặp giá trị khóa tại các chỉ số 2K+1 và 2K+2.i‘ is considered to be the parent of key-value pair at the indices 2k+1 and 2k+2.
- Đối với một min-heap, khóa/giá trị cha mẹ phải nhỏ hơn con cái.
- Đối với tối đa, khóa/giá trị cha mẹ phải lớn hơn con cái.
Examples:
Từ điển bình thường: {11: 2, 0: 4, 5: 9, 22: 7}
HEAP Dựa trên các khóa của từ điển: {0: 4, 1: 1, 5: 9, 22: 7, 11: 2}{0: 4, 1: 1, 5: 9, 22: 7, 11: 2}
HEAP Dựa trên các giá trị của từ điển: {11: 2, 0: 4, 5: 9, 22: 7}{11: 2, 0: 4, 5: 9, 22: 7}
Bài viết này cho thấy cách duy trì một từ điển trong một min-heap bằng mô-đun FEAPQ.
Từ điển bình thường như một đống
Từ điển bình thường với số nguyên/chuỗi là khóa có thể được duy trì trong cấu trúc heap với sự trợ giúp của mô -đun FEAPQ. Nhưng mô -đun này hy vọng một danh sách sẽ được thông qua. Vì vậy, cách tiếp cận được sử dụng ở đây là:
- Chuyển đổi các cặp giá trị khóa thành một danh sách các bộ dữ liệu.
- Chuyển danh sách các bộ dữ liệu cho hàm heapify [].heapify[] function.
- Chuyển đổi danh sách kết quả thành một từ điển một lần nữa.
LƯU Ý: Các heapify [] trên các bộ dữ liệu xem xét phần tử đầu tiên trong tuple cho quy trình. Do đó, theo mặc định, các từ điển được duy trì trong đống, chỉ dựa trên khóa.The heapify[] on tuples considers the first element in the tuple for the process. Thus, by default, the dictionaries are maintained in heap, based on the key only.
Ví dụ 1: Dựa trên khóa cho số nguyên
Hãy xem xét một từ điển trong đó các khóa là số nguyên dương và các giá trị là hình vuông của chúng. Bây giờ, điều này nên được duy trì trong một đống.
Python3
import
heapq as hq
dict_1
=
After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}5
=
After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}7
After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}8
After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}9
Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}0
Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}1
Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}2
Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}3
After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}9
Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}0
Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}6
Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}2
After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}5
=
import
0import
1After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}9
Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}0
import
4Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}2
Đầu ra
dictionary into list : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified list of tuples : [[2, 4], [3, 9], [5, 25], [11, 121]] Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121}
Ví dụ 2: Dựa trên khóa cho chuỗi & nbsp;
Hãy xem xét một từ điển có sự kết hợp của bảng chữ cái là khóa và đánh số của chúng là giá trị. Ví dụ: ABC ABC: 123. Điều này phải được duy trì trong Heap. & NBSP;abc” : 123. This has to be maintained in heap.
Python3
import
heapq as hq
dict_1
=
After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}5
=
After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}7
After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}8
After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}9
Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}0
Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}1
Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}2
Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}3
After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}9
Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}0
Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}6
Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}2
After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}5
=
import
0import
1After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}9
Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}0
import
4Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}2
Output:
Đầu ra
Heapified list of tuples : [[‘ab’, 12], [‘fg’, 67], [‘cd’, 34], [‘ij’, 910], [‘yz’, 2526]]
Dictionary as heap : {‘ab’: 12, ‘fg’: 67, ‘cd’: 34, ‘ij’: 910, ‘yz’: 2526}
Ví dụ 2: Dựa trên khóa cho chuỗi & nbsp; value
Hãy xem xét một từ điển có sự kết hợp của bảng chữ cái là khóa và đánh số của chúng là giá trị. Ví dụ: ABC ABC: 123. Điều này phải được duy trì trong Heap. & NBSP;
dict_1
=
- Từ điển vào danh sách: [['yz', 2526], ['ab', 12], ['cd', 34], ['ij', 910], ['fg', 67]] & nbsp; Tuples: [['ab', 12], ['fg', 67], ['cd', 34], ['ij', 910], ['yz', 2526]] & nbsp; từ điển như đống: {{ 'AB': 12, 'fg': 67, 'cd': 34, 'ij': 910, 'yz': 2526}heapify[].
- Ví dụ 3: Dựa trên giá trị
Cách tiếp cận hơi khác nhau ở đây. Các bước được thực hiện là:
Example:
Python3
import
heapq as hq
dict_1
=
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}13
=
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}15
After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}5
=
After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}7
After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}8
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}20
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}21
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}22
After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}9
Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}0
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}25
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}26
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}27
=
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}15
After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}5
=
import
0import
1Đầu ra
Ví dụ 2: Dựa trên khóa cho chuỗi & nbsp;
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}55
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}56
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}27
=
import
0Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}60
Hãy xem xét một từ điển có sự kết hợp của bảng chữ cái là khóa và đánh số của chúng là giá trị. Ví dụ: ABC ABC: 123. Điều này phải được duy trì trong Heap. & NBSP;
Đầu ra
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}
Ví dụ 2: Dựa trên khóa cho chuỗi & nbsp;
Hãy xem xét một từ điển có sự kết hợp của bảng chữ cái là khóa và đánh số của chúng là giá trị. Ví dụ: ABC ABC: 123. Điều này phải được duy trì trong Heap. & NBSP;
dict_1
=
- Từ điển vào danh sách: [['yz', 2526], ['ab', 12], ['cd', 34], ['ij', 910], ['fg', 67]] & nbsp; Tuples: [['ab', 12], ['fg', 67], ['cd', 34], ['ij', 910], ['yz', 2526]] & nbsp; từ điển như đống: {{ 'AB': 12, 'fg': 67, 'cd': 34, 'ij': 910, 'yz': 2526}heapify[].
- Ví dụ 3: Dựa trên giá trị
LƯU Ý: Các heapify [] trên các bộ dữ liệu xem xét phần tử đầu tiên trong tuple cho quy trình. Do đó, theo mặc định, các từ điển được duy trì trong đống, chỉ dựa trên khóa.The heapify[] on tuples considers the first element in the tuple for the process. Thus, by default, the dictionaries are maintained in heap, based on the key only.
Ví dụ: & nbsp;
Python3
import
heapq as hq
11
4=
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}69
11
11
8Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}1
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}73
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}3
11
8Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}5
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}73
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}7
11
8Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}9
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}73
After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}1
11
8After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}3
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}85
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}13
=
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}15
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}13
=
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}91
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}16
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}17
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}18
After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}9
Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}0
After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}02
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}26
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}22
After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}9
Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}0
After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}07
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}26
After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}09
=
import
0After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}12
After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}9
Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}0
After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}15
After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}16
Đầu ra
After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}
Từ điển lồng nhau
Trong trường hợp từ điển lồng nhau, nhiệm vụ thực hiện nhiều bước hơn để duy trì từ điển trong đống. Nếu từ điển phải được duy trì dựa trên khóa trong từ điển bên trong, thì phương pháp sau đây có thể được sử dụng.
- Chuyển đổi từ điển thành danh sách các bộ dữ liệu trong đó khóa của từ điển bên ngoài là tuple [0] và từ điển bên trong là tuple [1].
- Trích xuất các giá trị của khóa trong từ điển bên trong vào một danh sách.
- Áp dụng Heapify [] trong danh sách đó.heapify[] on that list.
- Xây dựng lại một từ điển mới bằng cách đặt hàng chúng dựa trên các kết quả được đặt.
Ví dụ, hãy xem xét một hồ sơ của nhân viên là một từ điển lồng nhau. Hồ sơ xuất hiện như được đưa ra dưới đây: & nbsp;
{
& nbsp; & nbsp;
& nbsp; & nbsp; & nbsp; & nbsp; tên tên "
& nbsp; & nbsp; & nbsp; & nbsp; "tuổi": 22,
& nbsp; & nbsp; & nbsp; & NBSP; Chỉ định trực tuyến
& nbsp; & nbsp; & nbsp; & NBSP;
& nbsp; & nbsp;},
& nbsp; & nbsp;
& nbsp; & nbsp; & nbsp; & nbsp; tên của tên
& nbsp; & nbsp; & nbsp; & nbsp; "tuổi": 20,
& nbsp; & nbsp; & nbsp; & NBSP; Chỉ định của người Viking
& nbsp; & nbsp; & nbsp; & NBSP;
& nbsp; & nbsp;},
& nbsp; & nbsp;
& nbsp; & nbsp; & nbsp; & nbsp; tên của tên
& nbsp; & nbsp; & nbsp; & nbsp; "tuổi": 20,
& nbsp; & nbsp; & nbsp; & NBSP; Chỉ định của người Viking
& nbsp; & nbsp; & nbsp; & NBSP;
& nbsp; & nbsp;},
& nbsp; & nbsp;
& nbsp; & nbsp; & nbsp; & nbsp; tên của tên
& nbsp; & nbsp; & nbsp; & nbsp; "tuổi": 20,
& nbsp; & nbsp; & nbsp; & NBSP; Chỉ định của người Viking
& nbsp; & nbsp; & nbsp; & NBSP;
& nbsp; & nbsp;
& nbsp; & nbsp; & nbsp; & nbsp; tên của tên
& nbsp; & nbsp; & nbsp; & nbsp; "tuổi": 42,
& nbsp; & nbsp; & nbsp; & NBSP; Chỉ định của người Viking: Người quản lý trực tuyến,
Python3
& nbsp; & nbsp; & nbsp; & NBSP;
& nbsp; & nbsp;
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}20
After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}9
Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}0
After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}27
After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}28
After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}29
After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}30
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}20
After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}32
After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}33
& nbsp; & nbsp; & nbsp; & nbsp; tên tên "
Step 2: Define the function that performs heapification. Takes the list of tuples as parameter.
Python3
& nbsp; & nbsp; & nbsp; & NBSP;
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}20
After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}37
=
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}15
& nbsp; & nbsp;}
}
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}20
After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}9
Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}0
After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}59
After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}60
After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}29
After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}30
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}20
After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}64
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}20
After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}9
Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}0
After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}68
After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}60
After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}29
After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}30
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}20
After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}09
=
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}15
Bây giờ chúng ta hãy duy trì điều này trong một min-heap dựa trên các giá trị lương. Do đó, nhân viên có mức lương tối thiểu xuất hiện dưới dạng kỷ lục đầu tiên. Để dễ đọc và hiểu rõ hơn, chúng ta có thể chia mã thành các chức năng.
Bước 1: & nbsp; Xác định chức năng để chuyển đổi từ điển thành danh sách & nbsp;
After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}17
After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}18
Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}06
Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}07
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}20
After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}20
=
After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}7____
After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}23
& nbsp; Bước 2: Xác định chức năng thực hiện quá trình hấp thụ. Lấy danh sách các bộ dữ liệu làm tham số. & Nbsp;
After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]]
Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]]
Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}
17 After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]]
Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]]
Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}
35
Python3
Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}16
=
{
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}20
Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}20
Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}21
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}44
Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}23
11
8Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}25
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}00
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}44
Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}28
11
8Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}30
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}00
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}44
Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}33
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}0
Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}35
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}00
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}44
Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}38
11
8Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}40
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}20
Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}42
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}20
Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}44
Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}21
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}20
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}16
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}17
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}18
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}33
Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}0
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}35
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}00
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}37
After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}49
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}44
Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}28
11
8Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}54
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}00
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}44
Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}33
11
8Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}59
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}00
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}44
Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}38
11
8Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}64
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}20
Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}42
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}20
Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}68
Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}21
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}44
Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}23
11
8Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}73
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}00
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}44
Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}28
11
8Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}78
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}00
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}44
Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}33
11
8Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}83
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}00
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}44
Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}38
11
8Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}88
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}20
Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}42
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}20
Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}92
Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}21
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}44
Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}23
11
8Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}97
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}00
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}44
Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}28
11
8Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}78
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}00
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}44
Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}33
11
8Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}83
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}00
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}44
Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}38
11
8import
12Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}20
After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}4
After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}4
After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}20
=
import
18After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}09
=
import
21After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}9
Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}0
import
4After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}16
Bây giờ đặt tất cả các mã với nhau, chúng tôi nhận được một từ điển lồng nhau được duy trì trong đống, dựa trên các giá trị của mức lương.
Python3
import
heapq as hq
After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}17
After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}18
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}20
After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}20
=
After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}7____
After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}23
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}20
After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}9
Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}0
After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}27
After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}28
After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}29
After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}30
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}20
After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}32
After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}33
After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}17
After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}35
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}20
After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}37
=
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}15
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}20
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}16
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}17
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}18
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}33
Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}0
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}35
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}00
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}37
After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}49
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}44
After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}51
After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}52
After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}53
After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}54
After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}55
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}20
After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}9
Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}0
After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}59
After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}60
After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}29
After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}30
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}20
After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}64
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}20
After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}9
Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}0
import
79After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}60
After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}29
After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}30
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}20
After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}09
=
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}15
Is
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}44___
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}55
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}45
After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}98
After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}525
After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}53
After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}54
Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}022
Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}06
Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}07
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}20
After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}09
=
import
0Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}12
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}20
After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}32
After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}09
Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}16
=
{
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}20
Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}20
Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}21
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}44
Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}23
11
8Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}25
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}00
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}44
Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}28
11
8Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}30
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}00
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}44
Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}33
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}0
Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}35
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}00
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}44
Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}38
11
8Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}40
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}20
Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}42
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}20
Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}44
Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}21
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}44
Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}23
11
8Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}49
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}00
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}44
Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}28
11
8Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}54
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}00
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}44
Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}33
11
8Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}59
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}00
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}44
Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}38
11
8Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}64
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}20
Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}42
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}20
Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}68
Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}21
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}44
Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}23
11
8Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}73
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}00
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}44
Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}28
11
8Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}78
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}00
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}44___
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}44
Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}38
11
8Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}88
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}20
Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}42
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}20
Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}92
Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}21
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}44
Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}23
11
8Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}97
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}00
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}44
Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}28
11
8Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}78
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}00
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}44
Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}33
11
8Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}83
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}00
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}44
Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}38
11
8import
12Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}20
After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}4
After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}4
After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}20
=
import
18After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}09
=
import
21After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}9
Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}0
import
4After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}16
Đầu ra
Từ điển như danh sách [['Emp01, {' tên ':' Kate ',' Age ': 22,' Chỉ định ':' Nhà phân tích ',' Mức lương ': 30000}], [' Emp02 ′, {'Tên': 'Rina', 'tuổi': 20, 'chỉ định': 'lập trình viên', 'tiền lương': 25000}], ['emp03 ′, {' name ':' vikas ',' tuổi ': 42,' chỉ định ': 'Người quản lý', 'Mức lương': 35000}], ['Emp04 ′, {' Tên ':' Manish ',' Age ': 42,' Chỉ định ':' Người quản lý ',' Mức lương ': 15000}]]]
& nbsp; trước khi Heapify: [30000, 25000, 35000, 15000] & nbsp;
& nbsp; sau khi Heapify: [15000, 25000, 35000, 30000] & nbsp;
Từ điển như Heap: {'emp04 ′: {' name ':' manish ',' tuổi ': 42,' chỉ định ':' người quản lý ',' tiền lương ': 15000},' emp02 ′: {'name': 'rina ',' Tuổi ': 20,' Chỉ định ':' Lập trình viên ',' Mức lương ': 25000},' Emp03: {'Tên': 'Vikas', 'Tuổi': 42, 'Chỉ định': 'Người quản lý', 'Mức lương': 35000}, 'Emp01 ′: {' name ':' Kate ',' Age ': 22,' Chỉ định ':' Nhà phân tích ',' Mức lương ': 30000}}
Chèn vào từ điển được duy trì dưới dạng một đống
Việc chèn các giá trị mới có thể được thực hiện trực tiếp bằng phương thức Heppush [] trong mô -đun Heapq. Cú pháp của nó như sau.heappush[] method in the heapq module. Its syntax is as follows.
HEAPQ. Heppush [Danh sách, New_Value]
Bây giờ, danh sách các bộ dữ liệu cùng với một tuple mới có thể được chuyển đến hàm này để thêm cặp giá trị khóa mới.
Thí dụ :
Python3
import
heapq as hq
Các
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}13
=
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}15
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}13
=
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}91
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}16
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}17
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}18
After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}9
Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}0
dict_1
74Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}26
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}22
After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}9
Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}0
dict_1
79Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}26
After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}09
=
import
0After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}12
After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}9
Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}0
import
4After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}16
dict_1
89
After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}52
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}00
After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}52
dict_1
93After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}9
Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}0
dict_1
96Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}26
After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}09
=
import
0After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}12
After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}9
Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}0
=
04After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}16
Output:
Danh sách các bộ dữ liệu: [[11, 121], [2, 4], [5, 25], [3, 9]] & nbsp; sau khi tăng cường: [[2, 4], [3, 9], [5, 5,25], [11, 121]] & nbsp; từ điển dưới dạng heap: {2: 4, 3: 9, 5: 25, 11: 121} & nbsp; sau khi chèn & heapify, [5, 25], [11, 121], [3, 9]] & nbsp; từ điển mới: {1: 1, 2: 4, 5: 25, 11: 121, 3: 9}
After heapification : [[2, 4], [3, 9], [5, 25], [11, 121]]
Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121}
After insertion & heapification [[1, 1], [2, 4], [5, 25], [11, 121], [3, 9]]
New dictionary : {1: 1, 2: 4, 5: 25, 11: 121, 3: 9}
Một phương pháp khác có thể được thực hiện là có một chức năng tăng cường từ điển và gọi nó sau khi cập nhật từ điển. & NBSP;
Thí dụ :
Python3
import
heapq as hq
After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}17
=
09Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}20
=
11=
After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}7
=
14Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}20
=
16Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}20
=
11=
import
0=
21Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}20
After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}9
Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}0
import
4=
26=
27=
{
11
11
8
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}1
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}00
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}3
11
8Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}5
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}00
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}7
11
8Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}9
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}00
After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}1
11
8After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}3
After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}4
After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}9
Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}030748
After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}30
=
50
=
51
Values of the dict after heapification : [4, 9, 25, 121] Final dictionary : {2: 4, 3: 9, 5: 25, 11: 121}5
Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}02
=
=
55=
51
After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}52
Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}02
=
After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}52
After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}9
Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}0
=
63=
64After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}9
Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}0
=
67After extraction : [[11, 121], [2, 4], [5, 25], [3, 9]] Heapified key-value pairs : [[2, 4], [3, 9], [5, 25], [11, 121]] Heapified dictionaries : {2: 4, 3: 9, 5: 25, 11: 121}30
=
50
Đầu ra
Before adding new values Dictionary as heap : {2: 4, 3: 9, 5: 25, 11: 121} Updated dictionary : {11: 121, 2: 4, 5: 25, 3: 9, 4: 16, 1: 1} After adding new values Dictionary as heap : {1: 1, 2: 4, 5: 25, 3: 9, 4: 16, 11: 121}