-
Notifications
You must be signed in to change notification settings - Fork 0
kr 2 #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
kr 2 #8
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| class Node: | ||
| data = 0 | ||
| level = 0 | ||
| child = None | ||
| parent = None | ||
| right = None | ||
|
|
||
| def __init__(self, key): | ||
| self.data = key | ||
| self.level = 0 | ||
| self.child = None | ||
| self.parent = None | ||
| self.right = None | ||
|
|
||
| def __str__(self): | ||
| return str(self.data) | ||
|
|
||
|
|
||
| class BHeap: | ||
| trees = [] | ||
|
|
||
| def __init__(self, trees): | ||
| self.trees = trees | ||
|
|
||
| def insert(self, key): | ||
| node = Node(key) | ||
| self.merge(BHeap([node])) | ||
|
|
||
| def find_min(self): | ||
| mn = 10**10 | ||
| for tree in self.trees: | ||
| mn = min(tree.data, mn) | ||
|
|
||
| if mn == 10**10: | ||
| print("No min") | ||
| return | ||
|
|
||
| return mn | ||
|
|
||
| def merge(self, heap): | ||
| self.trees.extend(heap.trees) | ||
|
Comment on lines
+40
to
+41
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Это просто объединение двух списков |
||
|
|
||
|
|
||
| def extract_min(self): | ||
| mn = 10**10 | ||
| min_node = None | ||
| for tree in self.trees: | ||
| if tree.data < mn: | ||
| mn = tree.data | ||
| min_node = tree | ||
|
|
||
| if mn == 10**10: | ||
| print("No min") | ||
| return | ||
|
|
||
| self.trees.remove(min_node) | ||
|
|
||
| if min_node.child: | ||
| self.merge(BHeap(min_node.child)) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Comment on lines
+58
to
+59
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Это ведет к потере детей, мы добавляем только одного ребенка удаляемой ноды |
||
|
|
||
| return min_node.data | ||
|
|
||
| def __str__(self): | ||
| res = "" | ||
| for tree in self.trees: | ||
| res += str(tree) + " " | ||
| return res | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| heap = BHeap([]) | ||
|
|
||
| print(heap.find_min()) | ||
| print(heap.extract_min()) | ||
|
|
||
|
|
||
| heap.insert(10) | ||
| heap.insert(20) | ||
| heap.insert(30) | ||
|
|
||
| print(heap) | ||
| print(heap.find_min()) | ||
| print(heap.extract_min()) | ||
| print(heap) | ||
|
|
||
| print(heap.extract_min()) | ||
| print(heap) | ||
|
|
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| import pytest | ||
| import bheap | ||
|
|
||
| def test_min(): | ||
| heap = bheap.BHeap([]) | ||
|
|
||
| heap.insert(10) | ||
| heap.insert(20) | ||
| heap.insert(30) | ||
|
|
||
| assert heap.find_min() == 10 | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| pytest.main() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
trees = []создает общий для всех экземпляров класса список, ноself.trees = treesего перекрывает