- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsegmenttreeArray.py
More file actions
Latest commit
executable file
·43 lines (37 loc) · 1.34 KB
/
Copy pathsegmenttreeArray.py
File metadata and controls
executable file
·43 lines (37 loc) · 1.34 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
importsys
classNodeBased(object):
arr= []
treeArray= []
defsetArray(self, arr):
self.arr=arr
self.treeArray= [None] *len(arr)
defminVal(self, i, j):
#print("i: %i j: %i" % (i, j))
node=TreeNode()
ifi==j:
node.val=self.arr[i]
returnnode
mid= (i+j) /2
node.left=self.minVal(i, mid)
node.right=self.minVal(mid+1, j)
node.val=min(node.left.val, node.right.val)
returnnode
defgenerateTreeArray(self, arr):
self.setArray(arr)
self.head=self.minVal(0, len(self.arr)-1)
defminRecursion(self, i, j, a, b, node):
#print("i: %i, j: %i, a: %i, b: %i" % (i,j,a,b))
if (i==aandj==b) ora==b: returnnode.val
ifj<aori>b: returnsys.maxint
mid= (a+b) /2
returnmin(self.minRecursion(i, j, a, mid, node.left), self.minRecursion(i, j, mid+1, b, node.right))
deffindMin(self, i, j):
ifi<0orj>=len(self.arr):
print("Recheck Index values!")
returnNone
returnself.minRecursion(i, j, 0, len(self.arr)-1, self.head)
if__name__=='__main__':
_list= [int(i) foriinraw_input().split()]
n=NodeBased()
n.generateTree(_list)
print(n.findMin(1, 3))