Advertisement
Guest User

Untitled

a guest
Aug 17th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.44 KB | None | 0 0
  1. class Heap():
  2.  
  3. sortedHeap = []
  4.  
  5. def __init__(self, heap, heapType='max'):
  6. """
  7. itype: heap: a list for complete binary tree
  8.  
  9. itype: heapType: max or min for heap type
  10. """
  11. self.heap = heap
  12. self.heapType = heapType
  13.  
  14. def heapify(self, i):
  15. """
  16. Itype: index to the node (0 based)
  17.  
  18. node = i
  19.  
  20. leftChild = 2*i
  21.  
  22. rightChild = 2*i+1
  23.  
  24. parent = i//2
  25. """
  26. if self.heapType == "max":
  27. while i<len(self.heap)//2:
  28. """
  29. comparing childs and spawing max to its parent if it exist,
  30. and doing same on swaped child node (were parent is (opposing nature))
  31. else, return
  32. """
  33. try:
  34. if self.heap[(i+1)*2-1]>self.heap[(i+1)*2] and self.heap[(i+1)*2-1]>self.heap[i]:
  35. self.heap[i], self.heap[(i+1)*2-1] = self.heap[(i+1)*2-1], self.heap[i]
  36. i = (i+1)*2-1
  37.  
  38. elif self.heap[(i+1)*2-1]<self.heap[(i+1)*2] and self.heap[(i+1)*2]>self.heap[i]:
  39. self.heap[i], self.heap[(i+1)*2] = self.heap[(i+1)*2], self.heap[i]
  40. i = (i+1)*2
  41.  
  42. elif self.heap[(i+1)*2-1] == self.heap[(i+1)*2] and self.heap[(i+1)*2-1]>self.heap[i]:
  43. self.heap[i], self.heap[(i+1)*2-1] = self.heap[(i+1)*2-1], self.heap[i]
  44. i = (i+1)*2-1
  45.  
  46. else:
  47. return
  48.  
  49. except:
  50. if self.heap[(i+1)*2-1]>self.heap[i]:
  51. self.heap[i], self.heap[(i+1)*2-1] = self.heap[(i+1)*2-1], self.heap[i]
  52. i = (i+1)*2-1
  53.  
  54. else:
  55. return
  56.  
  57. else:
  58. while i<len(self.heap)//2:
  59. """
  60. comparing childs and spawing min to its parent if it exist,
  61. and doing same on swaped child node (now were parent (opposing nature) is)
  62. else, return
  63. """
  64. try:
  65. if self.heap[(i+1)*2-1]<self.heap[(i+1)*2] and self.heap[(i+1)*2-1]<self.heap[i]:
  66. self.heap[i], self.heap[(i+1)*2-1] = self.heap[(i+1)*2-1], self.heap[i]
  67. i = (i+1)*2-1
  68.  
  69. elif self.heap[(i+1)*2-1]>self.heap[(i+1)*2] and self.heap[(i+1)*2]<self.heap[i]:
  70. self.heap[i], self.heap[(i+1)*2] = self.heap[(i+1)*2], self.heap[i]
  71. i = (i+1)*2
  72.  
  73. elif self.heap[(i+1)*2-1] == self.heap[(i+1)*2] and self.heap[(i+1)*2-1]<self.heap[i]:
  74. self.heap[i], self.heap[(i+1)*2-1] = self.heap[(i+1)*2-1], self.heap[i]
  75. i = (i+1)*2-1
  76.  
  77. else:
  78. return
  79.  
  80. except:
  81. if self.heap[(i+1)*2-1]<self.heap[i]:
  82. self.heap[i], self.heap[(i+1)*2-1] = self.heap[(i+1)*2-1], self.heap[i]
  83. i = (i+1)*2-1
  84.  
  85. else:
  86. return
  87.  
  88. def buildHeap(self):
  89. '''
  90. Builds heap
  91. '''
  92. for i in reversed(range(len(self.heap)//2)):
  93. self.heapify(i)
  94.  
  95. def insert(self, node):
  96. """
  97. Itype node: comaparable object
  98.  
  99. inserts a node in heap
  100. """
  101. self.heap.append(node)
  102. i = (len(self.heap))//2 - 1
  103. while i>0:
  104. self.heapify(i)
  105. i = i//2
  106. self.heapify(0)
  107.  
  108. def delete(self):
  109. """
  110. rtype: int (root node)
  111.  
  112. deletes root node from heap
  113. """
  114. root = self.heap[0]
  115. self.sortedHeap.append(root)
  116. self.heap[-1], self.heap[0] = self.heap[0], self.heap[-1]
  117. self.heap = self.heap[:-1]
  118. self.heapify(0)
  119. return root
  120.  
  121. def heapSort(self):
  122. """
  123. rtype: sorted heap in list
  124.  
  125. deletes roots and append to the sortedHeap
  126. """
  127. for _ in range(len(self.heap)):
  128. self.delete()
  129. return self.sortedHeap
  130.  
  131.  
  132. if __name__ == "__main__":
  133.  
  134. h1 = [16, 4, 10, 14, 7, 9, 3, 2, 8, 1, 2, 3]
  135.  
  136. print(h1)
  137. a = Heap(h1, 'max')
  138.  
  139. a.buildHeap()
  140. print('build', a.heap)
  141.  
  142. a.insert(35)
  143. print('inserted', a.heap)
  144.  
  145. max1 = a.delete()
  146. print(max1)
  147.  
  148. max2 = a.delete()
  149. print(max2)
  150.  
  151. print(a.heap)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement