@@ -31,7 +31,7 @@ def __repr__(self) -> str:
31
31
return f"min_value: { self .minn } , max_value: { self .maxx } "
32
32
33
33
34
- def build_tree (arr : list [int ]) -> Node :
34
+ def build_tree (arr : list [int ]) -> Node | None :
35
35
"""
36
36
Builds the tree for arr and returns the root
37
37
of the constructed tree
@@ -51,7 +51,10 @@ def build_tree(arr: list[int]) -> Node:
51
51
then recursively build trees for left_arr and right_arr
52
52
"""
53
53
pivot = (root .minn + root .maxx ) // 2
54
- left_arr , right_arr = [], []
54
+
55
+ left_arr : list [int ] = []
56
+ right_arr : list [int ] = []
57
+
55
58
for index , num in enumerate (arr ):
56
59
if num <= pivot :
57
60
left_arr .append (num )
@@ -63,7 +66,7 @@ def build_tree(arr: list[int]) -> Node:
63
66
return root
64
67
65
68
66
- def rank_till_index (node : Node , num : int , index : int ) -> int :
69
+ def rank_till_index (node : Node | None , num : int , index : int ) -> int :
67
70
"""
68
71
Returns the number of occurrences of num in interval [0, index] in the list
69
72
@@ -79,7 +82,7 @@ def rank_till_index(node: Node, num: int, index: int) -> int:
79
82
>>> rank_till_index(root, 0, 9)
80
83
1
81
84
"""
82
- if index < 0 :
85
+ if index < 0 or node is None :
83
86
return 0
84
87
# Leaf node cases
85
88
if node .minn == node .maxx :
@@ -93,7 +96,7 @@ def rank_till_index(node: Node, num: int, index: int) -> int:
93
96
return rank_till_index (node .right , num , index - node .map_left [index ])
94
97
95
98
96
- def rank (node : Node , num : int , start : int , end : int ) -> int :
99
+ def rank (node : Node | None , num : int , start : int , end : int ) -> int :
97
100
"""
98
101
Returns the number of occurrences of num in interval [start, end] in the list
99
102
@@ -114,7 +117,7 @@ def rank(node: Node, num: int, start: int, end: int) -> int:
114
117
return rank_till_end - rank_before_start
115
118
116
119
117
- def quantile (node : Node , index : int , start : int , end : int ) -> int :
120
+ def quantile (node : Node | None , index : int , start : int , end : int ) -> int :
118
121
"""
119
122
Returns the index'th smallest element in interval [start, end] in the list
120
123
index is 0-indexed
@@ -129,7 +132,7 @@ def quantile(node: Node, index: int, start: int, end: int) -> int:
129
132
>>> quantile(root, 4, 2, 5)
130
133
-1
131
134
"""
132
- if index > (end - start ) or start > end :
135
+ if index > (end - start ) or start > end or node is None :
133
136
return - 1
134
137
# Leaf node case
135
138
if node .minn == node .maxx :
@@ -155,10 +158,10 @@ def quantile(node: Node, index: int, start: int, end: int) -> int:
155
158
156
159
157
160
def range_counting (
158
- node : Node , start : int , end : int , start_num : int , end_num : int
161
+ node : Node | None , start : int , end : int , start_num : int , end_num : int
159
162
) -> int :
160
163
"""
161
- Returns the number of elememts in range [start_num, end_num]
164
+ Returns the number of elements in range [start_num, end_num]
162
165
in interval [start, end] in the list
163
166
164
167
>>> root = build_tree(test_array)
@@ -175,6 +178,7 @@ def range_counting(
175
178
"""
176
179
if (
177
180
start > end
181
+ or node is None
178
182
or start_num > end_num
179
183
or node .minn > end_num
180
184
or node .maxx < start_num
0 commit comments