-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsegmenttree.py
More file actions
executable file
·51 lines (43 loc) · 1.47 KB
/
Copy pathsegmenttree.py
File metadata and controls
executable file
·51 lines (43 loc) · 1.47 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
import sys
class TreeNode:
def __init__(self):
self.val = None
self.left = None
self.right = None
# look into overloading constructors
def _print(self):
print(self.val)
class NodeBased(object):
arr = []
head = None
def setArray(self, arr):
self.arr = arr
def minVal(self, i, j):
#print("i: %i j: %i" % (i, j))
node = TreeNode()
if i == j:
node.val = self.arr[i]
return node
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)
return node
def generateTree(self, arr):
self.setArray(arr)
self.head = self.minVal(0, len(self.arr)-1)
def minRecursion(self, i, j, a, b, node):
#print("i: %i, j: %i, a: %i, b: %i" % (i,j,a,b))
if (i == a and j == b) or a == b: return node.val
if j < a or i > b: return sys.maxint
mid = (a + b) / 2
return min(self.minRecursion(i, j, a, mid, node.left), self.minRecursion(i, j, mid+1, b, node.right))
def findMin(self, i, j):
if i < 0 or j >= len(self.arr):
print("Recheck Index values!")
return None
return self.minRecursion(i, j, 0, len(self.arr)-1, self.head)
_list = [int(i) for i in raw_input().split()]
n = NodeBased()
n.generateTree(_list)
print(n.findMin(1, 3))