mirror of
https://github.com/TheAlgorithms/Python.git
synced 2026-03-13 09:50:19 +08:00
psf/black code formatting (#1277)
This commit is contained in:
committed by
Christian Clauss
parent
07f04a2e55
commit
9eac17a408
@@ -26,9 +26,7 @@ class Node:
|
||||
In-place merge of two binomial trees of equal size.
|
||||
Returns the root of the resulting tree
|
||||
"""
|
||||
assert (
|
||||
self.left_tree_size == other.left_tree_size
|
||||
), "Unequal Sizes of Blocks"
|
||||
assert self.left_tree_size == other.left_tree_size, "Unequal Sizes of Blocks"
|
||||
|
||||
if self.val < other.val:
|
||||
other.left = self.right
|
||||
@@ -36,9 +34,7 @@ class Node:
|
||||
if self.right:
|
||||
self.right.parent = other
|
||||
self.right = other
|
||||
self.left_tree_size = (
|
||||
self.left_tree_size * 2 + 1
|
||||
)
|
||||
self.left_tree_size = self.left_tree_size * 2 + 1
|
||||
return self
|
||||
else:
|
||||
self.left = other.right
|
||||
@@ -46,9 +42,7 @@ class Node:
|
||||
if other.right:
|
||||
other.right.parent = self
|
||||
other.right = self
|
||||
other.left_tree_size = (
|
||||
other.left_tree_size * 2 + 1
|
||||
)
|
||||
other.left_tree_size = other.left_tree_size * 2 + 1
|
||||
return other
|
||||
|
||||
|
||||
@@ -132,9 +126,7 @@ class BinomialHeap:
|
||||
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self, bottom_root=None, min_node=None, heap_size=0
|
||||
):
|
||||
def __init__(self, bottom_root=None, min_node=None, heap_size=0):
|
||||
self.size = heap_size
|
||||
self.bottom_root = bottom_root
|
||||
self.min_node = min_node
|
||||
@@ -165,10 +157,7 @@ class BinomialHeap:
|
||||
combined_roots_list = []
|
||||
i, j = self.bottom_root, other.bottom_root
|
||||
while i or j:
|
||||
if i and (
|
||||
(not j)
|
||||
or i.left_tree_size < j.left_tree_size
|
||||
):
|
||||
if i and ((not j) or i.left_tree_size < j.left_tree_size):
|
||||
combined_roots_list.append((i, True))
|
||||
i = i.parent
|
||||
else:
|
||||
@@ -176,29 +165,17 @@ class BinomialHeap:
|
||||
j = j.parent
|
||||
# Insert links between them
|
||||
for i in range(len(combined_roots_list) - 1):
|
||||
if (
|
||||
combined_roots_list[i][1]
|
||||
!= combined_roots_list[i + 1][1]
|
||||
):
|
||||
combined_roots_list[i][
|
||||
0
|
||||
].parent = combined_roots_list[i + 1][0]
|
||||
combined_roots_list[i + 1][
|
||||
0
|
||||
].left = combined_roots_list[i][0]
|
||||
if combined_roots_list[i][1] != combined_roots_list[i + 1][1]:
|
||||
combined_roots_list[i][0].parent = combined_roots_list[i + 1][0]
|
||||
combined_roots_list[i + 1][0].left = combined_roots_list[i][0]
|
||||
# Consecutively merge roots with same left_tree_size
|
||||
i = combined_roots_list[0][0]
|
||||
while i.parent:
|
||||
if (
|
||||
(
|
||||
i.left_tree_size
|
||||
== i.parent.left_tree_size
|
||||
)
|
||||
and (not i.parent.parent)
|
||||
(i.left_tree_size == i.parent.left_tree_size) and (not i.parent.parent)
|
||||
) or (
|
||||
i.left_tree_size == i.parent.left_tree_size
|
||||
and i.left_tree_size
|
||||
!= i.parent.parent.left_tree_size
|
||||
and i.left_tree_size != i.parent.parent.left_tree_size
|
||||
):
|
||||
|
||||
# Neighbouring Nodes
|
||||
@@ -264,9 +241,7 @@ class BinomialHeap:
|
||||
next_node = self.bottom_root.parent.parent
|
||||
|
||||
# Merge
|
||||
self.bottom_root = self.bottom_root.mergeTrees(
|
||||
self.bottom_root.parent
|
||||
)
|
||||
self.bottom_root = self.bottom_root.mergeTrees(self.bottom_root.parent)
|
||||
|
||||
# Update Links
|
||||
self.bottom_root.parent = next_node
|
||||
@@ -337,9 +312,7 @@ class BinomialHeap:
|
||||
if bottom_of_new.val < min_of_new.val:
|
||||
min_of_new = bottom_of_new
|
||||
# Corner case of single root on top left path
|
||||
if (not self.min_node.left) and (
|
||||
not self.min_node.parent
|
||||
):
|
||||
if (not self.min_node.left) and (not self.min_node.parent):
|
||||
self.size = size_of_new
|
||||
self.bottom_root = bottom_of_new
|
||||
self.min_node = min_of_new
|
||||
@@ -348,9 +321,7 @@ class BinomialHeap:
|
||||
# Remaining cases
|
||||
# Construct heap of right subtree
|
||||
newHeap = BinomialHeap(
|
||||
bottom_root=bottom_of_new,
|
||||
min_node=min_of_new,
|
||||
heap_size=size_of_new,
|
||||
bottom_root=bottom_of_new, min_node=min_of_new, heap_size=size_of_new
|
||||
)
|
||||
|
||||
# Update size
|
||||
@@ -411,12 +382,8 @@ class BinomialHeap:
|
||||
"""
|
||||
if curr_node:
|
||||
preorder.append((curr_node.val, level))
|
||||
self.__traversal(
|
||||
curr_node.left, preorder, level + 1
|
||||
)
|
||||
self.__traversal(
|
||||
curr_node.right, preorder, level + 1
|
||||
)
|
||||
self.__traversal(curr_node.left, preorder, level + 1)
|
||||
self.__traversal(curr_node.right, preorder, level + 1)
|
||||
else:
|
||||
preorder.append(("#", level))
|
||||
|
||||
@@ -429,10 +396,7 @@ class BinomialHeap:
|
||||
return ""
|
||||
preorder_heap = self.preOrder()
|
||||
|
||||
return "\n".join(
|
||||
("-" * level + str(value))
|
||||
for value, level in preorder_heap
|
||||
)
|
||||
return "\n".join(("-" * level + str(value)) for value, level in preorder_heap)
|
||||
|
||||
|
||||
# Unit Tests
|
||||
|
||||
@@ -2,83 +2,85 @@
|
||||
|
||||
# This heap class start from here.
|
||||
class Heap:
|
||||
def __init__(self): # Default constructor of heap class.
|
||||
self.h = []
|
||||
self.currsize = 0
|
||||
def __init__(self): # Default constructor of heap class.
|
||||
self.h = []
|
||||
self.currsize = 0
|
||||
|
||||
def leftChild(self,i):
|
||||
if 2*i+1 < self.currsize:
|
||||
return 2*i+1
|
||||
return None
|
||||
def leftChild(self, i):
|
||||
if 2 * i + 1 < self.currsize:
|
||||
return 2 * i + 1
|
||||
return None
|
||||
|
||||
def rightChild(self,i):
|
||||
if 2*i+2 < self.currsize:
|
||||
return 2*i+2
|
||||
return None
|
||||
def rightChild(self, i):
|
||||
if 2 * i + 2 < self.currsize:
|
||||
return 2 * i + 2
|
||||
return None
|
||||
|
||||
def maxHeapify(self,node):
|
||||
if node < self.currsize:
|
||||
m = node
|
||||
lc = self.leftChild(node)
|
||||
rc = self.rightChild(node)
|
||||
if lc is not None and self.h[lc] > self.h[m]:
|
||||
m = lc
|
||||
if rc is not None and self.h[rc] > self.h[m]:
|
||||
m = rc
|
||||
if m!=node:
|
||||
temp = self.h[node]
|
||||
self.h[node] = self.h[m]
|
||||
self.h[m] = temp
|
||||
self.maxHeapify(m)
|
||||
def maxHeapify(self, node):
|
||||
if node < self.currsize:
|
||||
m = node
|
||||
lc = self.leftChild(node)
|
||||
rc = self.rightChild(node)
|
||||
if lc is not None and self.h[lc] > self.h[m]:
|
||||
m = lc
|
||||
if rc is not None and self.h[rc] > self.h[m]:
|
||||
m = rc
|
||||
if m != node:
|
||||
temp = self.h[node]
|
||||
self.h[node] = self.h[m]
|
||||
self.h[m] = temp
|
||||
self.maxHeapify(m)
|
||||
|
||||
def buildHeap(self,a): #This function is used to build the heap from the data container 'a'.
|
||||
self.currsize = len(a)
|
||||
self.h = list(a)
|
||||
for i in range(self.currsize//2,-1,-1):
|
||||
self.maxHeapify(i)
|
||||
def buildHeap(
|
||||
self, a
|
||||
): # This function is used to build the heap from the data container 'a'.
|
||||
self.currsize = len(a)
|
||||
self.h = list(a)
|
||||
for i in range(self.currsize // 2, -1, -1):
|
||||
self.maxHeapify(i)
|
||||
|
||||
def getMax(self): #This function is used to get maximum value from the heap.
|
||||
if self.currsize >= 1:
|
||||
me = self.h[0]
|
||||
temp = self.h[0]
|
||||
self.h[0] = self.h[self.currsize-1]
|
||||
self.h[self.currsize-1] = temp
|
||||
self.currsize -= 1
|
||||
self.maxHeapify(0)
|
||||
return me
|
||||
return None
|
||||
def getMax(self): # This function is used to get maximum value from the heap.
|
||||
if self.currsize >= 1:
|
||||
me = self.h[0]
|
||||
temp = self.h[0]
|
||||
self.h[0] = self.h[self.currsize - 1]
|
||||
self.h[self.currsize - 1] = temp
|
||||
self.currsize -= 1
|
||||
self.maxHeapify(0)
|
||||
return me
|
||||
return None
|
||||
|
||||
def heapSort(self): #This function is used to sort the heap.
|
||||
size = self.currsize
|
||||
while self.currsize-1 >= 0:
|
||||
temp = self.h[0]
|
||||
self.h[0] = self.h[self.currsize-1]
|
||||
self.h[self.currsize-1] = temp
|
||||
self.currsize -= 1
|
||||
self.maxHeapify(0)
|
||||
self.currsize = size
|
||||
def heapSort(self): # This function is used to sort the heap.
|
||||
size = self.currsize
|
||||
while self.currsize - 1 >= 0:
|
||||
temp = self.h[0]
|
||||
self.h[0] = self.h[self.currsize - 1]
|
||||
self.h[self.currsize - 1] = temp
|
||||
self.currsize -= 1
|
||||
self.maxHeapify(0)
|
||||
self.currsize = size
|
||||
|
||||
def insert(self,data): #This function is used to insert data in the heap.
|
||||
self.h.append(data)
|
||||
curr = self.currsize
|
||||
self.currsize+=1
|
||||
while self.h[curr] > self.h[curr/2]:
|
||||
temp = self.h[curr/2]
|
||||
self.h[curr/2] = self.h[curr]
|
||||
self.h[curr] = temp
|
||||
curr = curr/2
|
||||
def insert(self, data): # This function is used to insert data in the heap.
|
||||
self.h.append(data)
|
||||
curr = self.currsize
|
||||
self.currsize += 1
|
||||
while self.h[curr] > self.h[curr / 2]:
|
||||
temp = self.h[curr / 2]
|
||||
self.h[curr / 2] = self.h[curr]
|
||||
self.h[curr] = temp
|
||||
curr = curr / 2
|
||||
|
||||
def display(self): # This function is used to print the heap.
|
||||
print(self.h)
|
||||
|
||||
def display(self): #This function is used to print the heap.
|
||||
print(self.h)
|
||||
|
||||
def main():
|
||||
l = list(map(int, input().split()))
|
||||
h = Heap()
|
||||
h.buildHeap(l)
|
||||
h.heapSort()
|
||||
h.display()
|
||||
|
||||
if __name__=='__main__':
|
||||
main()
|
||||
l = list(map(int, input().split()))
|
||||
h = Heap()
|
||||
h.buildHeap(l)
|
||||
h.heapSort()
|
||||
h.display()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
Reference in New Issue
Block a user