Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Node:
- def __init__(self, data):
- self.data = data
- class Heap:
- def __init__(self):
- self.heap_list = []
- def balance(self):
- current_element = len(self.heap_list) - 1
- while True:
- if current_element <= 0:
- break
- if current_element % 2 == 0:
- parent = current_element // 2 - 2
- else:
- parent = current_element // 2 - 1
- if self.heap_list[parent] > self.heap_list[current_element] or self.heap_list[parent] == self.heap_list[current_element]:
- break
- elif self.heap_list[parent] < self.heap_list[current_element]:
- tmp = self.heap_list[parent]
- self.heap_list[parent] = self.heap_list[current_element]
- self.heap_list[current_element] = tmp
- def insert(self,append_element):
- self.heap_list.append(append_element)
- self.balance()
- def remove():
- pass
- h = Heap()
- h.insert(2)
- h.insert(5)
- h.insert(40)
- h.insert(100)
- h.insert(200)
- print(h.heap_list)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement