-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintervalTree.py
More file actions
173 lines (150 loc) · 5.8 KB
/
Copy pathintervalTree.py
File metadata and controls
173 lines (150 loc) · 5.8 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import interval
from graphviz import Digraph
class Node(object):
def __init__(self, range, max):
self.range = range
self.max = max
self.right = None
self.left = None
self.height = 1
def __str__(self):
return "[" + str(self.range.low) + ", " + str(self.range.high) + "] " + "max = " + str(self.max)
#"Introduction to Algorithms" by Cormen, Leiserson, Rivest, and Stein, augmented interval tree implementation
class Interval_Tree(object):
def insert(self, root, x):
if root == None:
return Node(x, x.high)
if x.low < root.range.low:
root.left = self.insert(root.left, x)
else:
root.right = self.insert(root.right, x)
if root.max < x.high:
root.max = x.high
root.height = 1 + max(self.getHeight(root.left), self.getHeight(root.right))
balanceFactor = self.getBalance(root)
if balanceFactor > 1:
if x.low < root.left.range.low:
return self.rightRotate(root)
else:
root.left = self.leftRotate(root.left)
return self.rightRotate(root)
if balanceFactor < -1:
if x.low >= root.right.range.low:
return self.leftRotate(root)
else:
root.right = self.rightRotate(root.right)
return self.leftRotate(root)
return root
def leftRotate(self, z):
y = z.right
T2 = y.left
# Perform rotation
y.left = z
z.right = T2
# Update heights
z.height = 1 + max(self.getHeight(z.left), self.getHeight(z.right))
y.height = 1 + max(self.getHeight(y.left), self.getHeight(y.right))
z.max = self.getMaxOfSubtree(z)
y.max = self.getMaxOfSubtree(y)
# Return the new root
return y
def rightRotate(self, z):
y = z.left
T3 = y.right
# Perform rotation
y.right = z
z.left = T3
# Update heights
z.height = 1 + max(self.getHeight(z.left), self.getHeight(z.right))
y.height = 1 + max(self.getHeight(y.left), self.getHeight(y.right))
z.max = self.getMaxOfSubtree(z)
y.max = self.getMaxOfSubtree(y)
# Return the new root
return y
def getMaxOfSubtree(self, root):
tempMax = [root.range.high]
if(root.right):
self.getMaxOfSubtree(root.right)
tempMax.append(root.right.max)
if(root.left):
self.getMaxOfSubtree(root.left)
tempMax.append(root.left.max)
return max(tempMax)
def getHeight(self, root):
if root == None:
return 0
return root.height
def getBalance(self, root):
if root == None:
return 0
return self.getHeight(root.left) - self.getHeight(root.right)
# By "Introduction to Algorithms" by Cormen, Leiserson, Rivest, and Stein
def searchInterval(self, root, qInterval):
if(root == None):
return
if(self.isOverlapping(root, qInterval)):
return root
elif(root.left and root.left.max >= qInterval.low):
return self.searchInterval(root.left, qInterval)
else:
return self.searchInterval(root.right, qInterval)
# By Michelle Bodnar, Andrew Lohr Rutgers University
def searchAllOvelaps(self, root, qInterval):
if(self.isOverlapping(root, qInterval)):
print("Found: " +str(root))
if(root.left and root.left.max >= qInterval.low):
self.searchAllOvelaps(root.left, qInterval)
if(root.right and root.right.max>=qInterval.low):
self.searchAllOvelaps(root.right, qInterval)
# By "Introduction to Algorithms" by Cormen, Leiserson, Rivest, and Stein
def isOverlapping(self, root, qInterval):
if(root == None or qInterval == None):
return False
if(root.range.low <= qInterval.high and qInterval.low <= root.range.high):
return True
return False
def preOrder(self, root):
if root == None:
return
print("{0} ".format(root.range) + str(self.getBalance(root)))
self.preOrder(root.left)
self.preOrder(root.right)
def inOrder(self, root):
if root == None:
return
self.inOrder(root.left)
print(root, end="")
# print imbalanced nodes
if (self.getBalance(root)<-1 or self.getBalance(root)>1):
print(self.getBalance(root), end="")
self.inOrder(root.right)
# By iq.opengenus.org
def printTreeInPdf(self, filename,root):
g = Digraph('G', filename=filename)
node_list = [root]
while(len(node_list) != 0):
current_node = node_list[0]
node_list.pop(0)
if(current_node.left):
g.edge(str(current_node.max)+"\n"+str(current_node.range), str(current_node.left.max)+"\n"+str(current_node.left.range))
node_list.append(current_node.left)
if(current_node.right is not None):
g.edge(str(current_node.max)+"\n"+str(current_node.range), str(current_node.right.max)+"\n"+str(current_node.right.range))
node_list.append(current_node.right)
g.view()
return
if __name__ == '__main__':
tree = Interval_Tree()
root = None
for i in range(10):
# (minLow, maxLow, minSize, maxSize)
x = interval.Interval(5,200,10,30)
root = tree.insert(root, x)
# print("PreOrder traversal of constructed Interval Tree is")
# tree.preOrder(root)
# print("InOrder traversal of constructed Interval Tree is")
# tree.inOrder(root)
queryInterval = interval.Interval(5,60,10,30)
print("Query Interval:"+str(queryInterval))
print(tree.searchInterval(root, queryInterval))
tree.printTreeInPdf("interval_tree.gv",root)