-
Notifications
You must be signed in to change notification settings - Fork 0
test 2 #11
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
test 2 #11
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,67 @@ | ||
| class NodeHeap: | ||
| def __init__(self, key, value): | ||
| self.priority = key | ||
| self.value = value | ||
| self.degree = 0 # колво детей узлов | ||
| self.parent = None | ||
| self.child = None # left | ||
| self.sibling = None # right | ||
|
|
||
|
|
||
| class Heap: | ||
| def __init__(self): | ||
| self.head = None | ||
|
|
||
| def is_empty(self): | ||
| return self.head is None | ||
|
|
||
| def insert(self, key, value): | ||
| new_heap = Heap() | ||
| new_node = NodeHeap(key, value) | ||
| new_heap.head = new_node | ||
| self.merge(new_heap) | ||
|
|
||
| return new_node | ||
|
|
||
| def get_minimum(self): | ||
| if not self.head: | ||
| return None | ||
| min_node = self.head | ||
| cur = self.head.sibling | ||
| while cur: | ||
| if cur.key < min_node.key: | ||
|
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. У Вас в |
||
| min_node = cur | ||
| cur = cur.sibling | ||
|
|
||
| return min_node.key | ||
|
|
||
| def merge(self, other_head): | ||
| if not self: | ||
| return other_head | ||
| if not other_head: | ||
| return self | ||
| result = None | ||
| curr = None | ||
| p1 = self | ||
|
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. Должно быть |
||
| p2 = other_head | ||
| if p1.degree <= p2.degree: | ||
| result = p1 | ||
| p1 = p1.sibling | ||
| else: | ||
| result = p2 | ||
| p2 = p2.sibling | ||
| curr = result | ||
| while p1 and p2: | ||
| if p1.degree <= p2.degree: | ||
| curr.sibling = p1 | ||
| p1 = p1.sibling | ||
| else: | ||
| curr.sibling = p2 | ||
| p2 = p2.sibling | ||
| curr = curr.sibling | ||
| if p1: | ||
| curr.sibling = p1 | ||
| else: | ||
| curr.sibling = p2 | ||
|
Comment on lines
+54
to
+65
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 result | ||
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.
В
mergeвозвращается результат, но не обновляется текущая куча, из-за этого, и того чтоinsertигнорирует то, что возвращаетmerge, вставка новых элементов не будет работать.