Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. class Node:
  2. def __init__(self, data):
  3. self.data = data
  4.  
  5. class Heap:
  6. def __init__(self):
  7. self.heap_list = []
  8.  
  9. def balance(self):
  10. current_element = len(self.heap_list) - 1
  11. while True:
  12. if current_element <= 0:
  13. break
  14. if current_element % 2 == 0:
  15. parent = current_element // 2 - 2
  16. else:
  17. parent = current_element // 2 - 1
  18. if self.heap_list[parent] > self.heap_list[current_element] or self.heap_list[parent] == self.heap_list[current_element]:
  19. break
  20. elif self.heap_list[parent] < self.heap_list[current_element]:
  21. tmp = self.heap_list[parent]
  22. self.heap_list[parent] = self.heap_list[current_element]
  23. self.heap_list[current_element] = tmp
  24.  
  25. def insert(self,append_element):
  26. self.heap_list.append(append_element)
  27. self.balance()
  28.  
  29. def remove():
  30. pass
  31.  
  32. h = Heap()
  33. h.insert(2)
  34. h.insert(5)
  35. h.insert(40)
  36. h.insert(100)
  37. h.insert(200)
  38. print(h.heap_list)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement