-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryTree.py
More file actions
64 lines (56 loc) · 1.51 KB
/
Copy pathBinaryTree.py
File metadata and controls
64 lines (56 loc) · 1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
class MaxHeap:
def __init__(self):
self.heap = []
def push(self,value):
self.heap.append(value)
self.float_up(len(self.heap)-1)
def float_up(self,index):
if index==0:
return
else:
if index%2==0:
parent_of_index=(index//2)-1
if self.heap[index]>self.heap[parent_of_index]:
self.swap(index,parent_of_index)
else:
parent_of_index = index//2
if self.heap[index]> self.heap[parent_of_index]:
self.swap(index,parent_of_index)
self.float_up(parent_of_index)
def peek(self):
print(self.heap[0])
def pop(self):
if len(self.heap)>2:
temp = self.heap[0]
self.heap[0]=self.heap[len(self.heap)-1]
self.heap[len(self.heap)-1]
self.heap.pop()
self.down_adj()
elif len(self.heap)==1:
self.heap.pop()
else:
print("Nothing to pop")
def swap(self,index1,index2):
temp = self.heap[index1]
self.heap[index1] = self.heap[index2]
self.heap[index2] = temp
H = MaxHeap()
print("*****Pushing Values***")
print("pushing 165")
H.push(165)
print(H.heap)
print("Pushing 60")
H.push(60)
print(H.heap)
print("Pushing 179")
H.push(179)
print(H.heap)
print("pushing 400")
H.push(400)
print(H.heap)
print("pushing 6")
H.push(6)
print(H.heap)
print("Pushing 275")
H.push(275)
print(H.heap)