Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 51k
Expand file tree
/
Copy pathwavelet_tree.py
More file actions
Latest commit
210 lines (185 loc) · 5.92 KB
/
Copy pathwavelet_tree.py
File metadata and controls
210 lines (185 loc) · 5.92 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
"""
Wavelet tree is a data-structure designed to efficiently answer various range queries
for arrays. Wavelets trees are different from other binary trees in the sense that
the nodes are split based on the actual values of the elements and not on indices,
such as the with segment trees or fenwick trees. You can read more about them here:
1. https://users.dcc.uchile.cl/~jperez/papers/ioiconf16.pdf
2. https://www.youtube.com/watch?v=4aSv9PcecDw&t=811s
3. https://www.youtube.com/watch?v=CybAgVF-MMc&t=1178s
"""
from __future__ importannotations
test_array= [2, 1, 4, 5, 6, 0, 8, 9, 1, 2, 0, 6, 4, 2, 0, 6, 5, 3, 2, 7]
classNode:
def__init__(self, length: int) ->None:
self.minn: int=-1
self.maxx: int=-1
self.map_left: list[int] = [-1] *length
self.left: Node|None=None
self.right: Node|None=None
def__repr__(self) ->str:
"""
>>> node = Node(length=27)
>>> repr(node)
'Node(min_value=-1 max_value=-1)'
>>> repr(node) == str(node)
True
"""
returnf"Node(min_value={self.minn} max_value={self.maxx})"
defbuild_tree(arr: list[int]) ->Node|None:
"""
Builds the tree for arr and returns the root
of the constructed tree
>>> build_tree(test_array)
Node(min_value=0 max_value=9)
"""
root=Node(len(arr))
root.minn, root.maxx=min(arr), max(arr)
# Leaf node case where the node contains only one unique value
ifroot.minn==root.maxx:
returnroot
"""
Take the mean of min and max element of arr as the pivot and
partition arr into left_arr and right_arr with all elements <= pivot in the
left_arr and the rest in right_arr, maintaining the order of the elements,
then recursively build trees for left_arr and right_arr
"""
pivot= (root.minn+root.maxx) //2
left_arr: list[int] = []
right_arr: list[int] = []
forindex, numinenumerate(arr):
ifnum<=pivot:
left_arr.append(num)
else:
right_arr.append(num)
root.map_left[index] =len(left_arr)
root.left=build_tree(left_arr)
root.right=build_tree(right_arr)
returnroot
defrank_till_index(node: Node|None, num: int, index: int) ->int:
"""
Returns the number of occurrences of num in interval [0, index] in the list
>>> root = build_tree(test_array)
>>> rank_till_index(root, 6, 6)
1
>>> rank_till_index(root, 2, 0)
1
>>> rank_till_index(root, 1, 10)
2
>>> rank_till_index(root, 17, 7)
0
>>> rank_till_index(root, 0, 9)
1
"""
ifindex<0ornodeisNone:
return0
# Leaf node cases
ifnode.minn==node.maxx:
returnindex+1ifnode.minn==numelse0
pivot= (node.minn+node.maxx) //2
ifnum<=pivot:
# go the left subtree and map index to the left subtree
returnrank_till_index(node.left, num, node.map_left[index] -1)
else:
# go to the right subtree and map index to the right subtree
returnrank_till_index(node.right, num, index-node.map_left[index])
defrank(node: Node|None, num: int, start: int, end: int) ->int:
"""
Returns the number of occurrences of num in interval [start, end] in the list
>>> root = build_tree(test_array)
>>> rank(root, 6, 3, 13)
2
>>> rank(root, 2, 0, 19)
4
>>> rank(root, 9, 2 ,2)
0
>>> rank(root, 0, 5, 10)
2
"""
ifstart>end:
return0
rank_till_end=rank_till_index(node, num, end)
rank_before_start=rank_till_index(node, num, start-1)
returnrank_till_end-rank_before_start
defquantile(node: Node|None, index: int, start: int, end: int) ->int:
"""
Returns the index'th smallest element in interval [start, end] in the list
index is 0-indexed
>>> root = build_tree(test_array)
>>> quantile(root, 2, 2, 5)
5
>>> quantile(root, 5, 2, 13)
4
>>> quantile(root, 0, 6, 6)
8
>>> quantile(root, 4, 2, 5)
-1
"""
ifindex> (end-start) orstart>endornodeisNone:
return-1
# Leaf node case
ifnode.minn==node.maxx:
returnnode.minn
# Number of elements in the left subtree in interval [start, end]
num_elements_in_left_tree=node.map_left[end] - (
node.map_left[start-1] ifstartelse0
)
ifnum_elements_in_left_tree>index:
returnquantile(
node.left,
index,
(node.map_left[start-1] ifstartelse0),
node.map_left[end] -1,
)
else:
returnquantile(
node.right,
index-num_elements_in_left_tree,
start- (node.map_left[start-1] ifstartelse0),
end-node.map_left[end],
)
defrange_counting(
node: Node|None, start: int, end: int, start_num: int, end_num: int
) ->int:
"""
Returns the number of elements in range [start_num, end_num]
in interval [start, end] in the list
>>> root = build_tree(test_array)
>>> range_counting(root, 1, 10, 3, 7)
3
>>> range_counting(root, 2, 2, 1, 4)
1
>>> range_counting(root, 0, 19, 0, 100)
20
>>> range_counting(root, 1, 0, 1, 100)
0
>>> range_counting(root, 0, 17, 100, 1)
0
"""
if (
start>end
ornodeisNone
orstart_num>end_num
ornode.minn>end_num
ornode.maxx<start_num
):
return0
ifstart_num<=node.minnandnode.maxx<=end_num:
returnend-start+1
left=range_counting(
node.left,
(node.map_left[start-1] ifstartelse0),
node.map_left[end] -1,
start_num,
end_num,
)
right=range_counting(
node.right,
start- (node.map_left[start-1] ifstartelse0),
end-node.map_left[end],
start_num,
end_num,
)
returnleft+right
if__name__=="__main__":
importdoctest
doctest.testmod()