Skip to content

Commit dbff1f4

Browse files
committed
modified files to meet style guidelines, edited docstrings and added some doctests
1 parent cfc71df commit dbff1f4

File tree

2 files changed

+56
-26
lines changed

2 files changed

+56
-26
lines changed

data_structures/binary_tree/lazy_segment_tree.py

+25-10
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,32 @@
55
class SegmentTree:
66
def __init__(self, N: int) -> None:
77
self.N = N
8-
self.st: List[int] = [
9-
0 for i in range(0, 4 * N)
10-
] # approximate the overall size of segment tree with array N
11-
self.lazy: List[int] = [
12-
0 for i in range(0, 4 * N)
13-
] # create array to store lazy update
8+
# approximate the overall size of segment tree with array N
9+
self.st: List[int] = [0 for i in range(0, 4 * N)]
10+
# create array to store lazy update
11+
self.lazy: List[int] = [0 for i in range(0, 4 * N)]
1412
self.flag: List[int] = [0 for i in range(0, 4 * N)] # flag for lazy update
1513

1614
def left(self, idx: int) -> int:
15+
"""
16+
>>> left(1)
17+
2
18+
>>> left(2)
19+
4
20+
>>> left(12)
21+
24
22+
"""
1723
return idx * 2
1824

1925
def right(self, idx: int) -> int:
26+
"""
27+
>>> left(1)
28+
3
29+
>>> left(2)
30+
5
31+
>>> left(12)
32+
25
33+
"""
2034
return idx * 2 + 1
2135

2236
def build(
@@ -30,12 +44,13 @@ def build(
3044
self.build(self.right(idx), mid + 1, right_element, A)
3145
self.st[idx] = max(self.st[self.left(idx)], self.st[self.right(idx)])
3246

33-
# update with O(lg N) (Normal segment tree without lazy update will take O(Nlg N)
34-
# for each update)
3547
def update(
3648
self, idx: int, left_element: int, right_element: int, a: int, b: int, val: int
3749
) -> bool:
3850
"""
51+
update with O(lg N) (Normal segment tree without lazy update will take O(Nlg N)
52+
for each update)
53+
3954
update(1, 1, N, a, b, v) for update val v to [a,b]
4055
"""
4156
if self.flag[idx] is True:
@@ -87,7 +102,7 @@ def query(
87102
q2 = self.query(self.right(idx), mid + 1, right_element, a, b)
88103
return max(q1, q2)
89104

90-
def showData(self) -> None:
105+
def show_data(self) -> None:
91106
showList = []
92107
for i in range(1, N + 1):
93108
showList += [self.query(1, 1, self.N, i, i)]
@@ -105,4 +120,4 @@ def showData(self) -> None:
105120
segt.update(1, 1, N, 1, 3, 111)
106121
print(segt.query(1, 1, N, 1, 15))
107122
segt.update(1, 1, N, 7, 8, 235)
108-
segt.showData()
123+
segt.show_data()

data_structures/binary_tree/lowest_common_ancestor.py

+31-16
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,29 @@
22
# https://en.wikipedia.org/wiki/Breadth-first_search
33

44
import queue
5-
from typing import Tuple, List, Dict
5+
from typing import Dict, List, Tuple
66

77

88
def swap(a: int, b: int) -> Tuple[int, int]:
9+
"""
10+
Return a tuple (b, a) when given two integers a and b
11+
>>> swap(2,3)
12+
(3,2)
13+
>>> swap(3,4)
14+
(4,3)
15+
>>> swap(67, 12)
16+
(12, 67)
17+
"""
918
a ^= b
1019
b ^= a
1120
a ^= b
1221
return a, b
1322

1423

15-
# creating sparse table which saves each nodes 2^i-th parent
16-
def creatSparse(max_node: int, parent: List[List[int]]) -> List[List[int]]:
24+
def create_sparse(max_node: int, parent: List[List[int]]) -> List[List[int]]:
25+
"""
26+
creating sparse table which saves each nodes 2^i-th parent
27+
"""
1728
j = 1
1829
while (1 << j) < max_node:
1930
for i in range(1, max_node + 1):
@@ -23,7 +34,9 @@ def creatSparse(max_node: int, parent: List[List[int]]) -> List[List[int]]:
2334

2435

2536
# returns lca of node u,v
26-
def LCA(u: int, v: int, level: List[int], parent: List[List[int]]) -> List[List[int]]:
37+
def lowest_common_ancestor(
38+
u: int, v: int, level: List[int], parent: List[List[int]]
39+
) -> List[List[int]]:
2740
# u must be deeper in the tree than v
2841
if level[u] < level[v]:
2942
u, v = swap(u, v)
@@ -43,16 +56,18 @@ def LCA(u: int, v: int, level: List[int], parent: List[List[int]]) -> List[List[
4356

4457

4558
# runs a breadth first search from root node of the tree
46-
# sets every nodes direct parent
47-
# parent of root node is set to 0
48-
# calculates depth of each node from root node
49-
def bfs(
59+
def breadth_first_search(
5060
level: List[int],
5161
parent: List[List[int]],
5262
max_node: int,
5363
graph: Dict[int, int],
5464
root=1,
5565
) -> Tuple[List[int], List[List[int]]]:
66+
"""
67+
sets every nodes direct parent
68+
parent of root node is set to 0
69+
calculates depth of each node from root node
70+
"""
5671
level[root] = 0
5772
q = queue.Queue(maxsize=max_node)
5873
q.put(root)
@@ -87,14 +102,14 @@ def main() -> None:
87102
12: [],
88103
13: [],
89104
}
90-
level, parent = bfs(level, parent, max_node, graph, 1)
91-
parent = creatSparse(max_node, parent)
92-
print("LCA of node 1 and 3 is: ", LCA(1, 3, level, parent))
93-
print("LCA of node 5 and 6 is: ", LCA(5, 6, level, parent))
94-
print("LCA of node 7 and 11 is: ", LCA(7, 11, level, parent))
95-
print("LCA of node 6 and 7 is: ", LCA(6, 7, level, parent))
96-
print("LCA of node 4 and 12 is: ", LCA(4, 12, level, parent))
97-
print("LCA of node 8 and 8 is: ", LCA(8, 8, level, parent))
105+
level, parent = breadth_first_search(level, parent, max_node, graph, 1)
106+
parent = create_sparse(max_node, parent)
107+
print("LCA of node 1 and 3 is: ", lowest_common_ancestor(1, 3, level, parent))
108+
print("LCA of node 5 and 6 is: ", lowest_common_ancestor(5, 6, level, parent))
109+
print("LCA of node 7 and 11 is: ", lowest_common_ancestor(7, 11, level, parent))
110+
print("LCA of node 6 and 7 is: ", lowest_common_ancestor(6, 7, level, parent))
111+
print("LCA of node 4 and 12 is: ", lowest_common_ancestor(4, 12, level, parent))
112+
print("LCA of node 8 and 8 is: ", lowest_common_ancestor(8, 8, level, parent))
98113

99114

100115
if __name__ == "__main__":

0 commit comments

Comments
 (0)