|
1 | 1 | class BinaryHeap:
|
2 | 2 | """
|
3 | 3 | A max-heap implementation in Python
|
4 |
| - >>> sample = BinaryHeap() |
5 |
| - >>> sample.insert(6) |
6 |
| - >>> sample.insert(10) |
7 |
| - >>> sample.insert(15) |
8 |
| - >>> sample.insert(12) |
9 |
| - >>> print(sample.pop()) |
| 4 | + >>> binary_heap = BinaryHeap() |
| 5 | + >>> binary_heap.insert(6) |
| 6 | + >>> binary_heap.insert(10) |
| 7 | + >>> binary_heap.insert(15) |
| 8 | + >>> binary_heap.insert(12) |
| 9 | + >>> binary_heap.pop() |
10 | 10 | 15
|
11 |
| - >>> print(sample.pop()) |
| 11 | + >>> binary_heap.pop() |
12 | 12 | 12
|
13 |
| - >>> print(sample.get_list) |
| 13 | + >>> binary_heap.get_list |
14 | 14 | [10, 6]
|
15 |
| - >>> print(len(sample)) |
| 15 | + >>> len(binary_heap) |
16 | 16 | 2
|
17 | 17 | """
|
18 | 18 |
|
@@ -69,22 +69,19 @@ def __len__(self):
|
69 | 69 | return self.__size
|
70 | 70 |
|
71 | 71 |
|
72 |
| -# example |
73 |
| -# create an instance of BinaryHeap object |
74 |
| -sample = BinaryHeap() |
75 |
| -# insert values |
76 |
| -sample.insert(6) |
77 |
| -sample.insert(10) |
78 |
| -sample.insert(15) |
79 |
| -sample.insert(12) |
80 |
| -# pop root(max-values because it is max heap) |
81 |
| -print(sample.pop()) # 15 |
82 |
| -print(sample.pop()) # 12 |
83 |
| -# get the list and size after operations |
84 |
| -print(sample.get_list) |
85 |
| -print(len(sample)) |
86 |
| - |
87 | 72 | if __name__ == "__main__":
|
88 | 73 | import doctest
|
89 | 74 |
|
90 | 75 | doctest.testmod()
|
| 76 | + # create an instance of BinaryHeap |
| 77 | + binary_heap = BinaryHeap() |
| 78 | + binary_heap.insert(6) |
| 79 | + binary_heap.insert(10) |
| 80 | + binary_heap.insert(15) |
| 81 | + binary_heap.insert(12) |
| 82 | + # pop root(max-values because it is max heap) |
| 83 | + print(binary_heap.pop()) # 15 |
| 84 | + print(binary_heap.pop()) # 12 |
| 85 | + # get the list and size after operations |
| 86 | + print(binary_heap.get_list) |
| 87 | + print(len(binary_heap)) |
0 commit comments