MCPcopy Create free account
hub / github.com/acm-clan/algorithm-stone / SegmentTreeDiffScene

Class SegmentTreeDiffScene

animations/segmenttree.py:17–70  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

15 self.wait(100)
16
17class SegmentTreeDiffScene(AlgoScene):
18 def __init__(self, **kwargs):
19 super().__init__(**kwargs)
20 self.datas = np.array([1, 2, 3, 4, 5, 6])
21 self.group = []
22 for i in range(self.datas.shape[0]):
23 self.group.append(i)
24
25 def show_diff(self):
26 self.show_message("首先,让我们来看看区间问题的常见3个解法")
27
28 table = AlgoTable(self, np.array([
29 ["", "区间求和", "区间最大值", "区间修改", "单点修改"],
30 ["前缀和", AlgoCheckmark(), AlgoExmark(), AlgoExmark(), AlgoExmark(),],
31 ["树状数组", AlgoCheckmark(), AlgoCheckmark(), AlgoExmark(), AlgoCheckmark(),],
32 ["线段树", AlgoCheckmark(), AlgoCheckmark(), AlgoCheckmark(), AlgoCheckmark(),],
33 ], dtype=object))
34
35 self.play(ShowCreation(table), run_time=4)
36
37 rect = SurroundingRectangle(table)
38 self.play(ShowCreation(rect))
39
40 self.show_message("前缀和、树状数组、线段树")
41
42 self.show_message("前缀和一般用于固定的数组")
43
44 self.show_message("树状数组是一种很高效的数据结构")
45 self.show_message("功能能满足大部分需求")
46
47 self.show_message("线段树则是一种非常灵活的数据结构")
48 self.show_message("本动画重点讲解线段树")
49 self.play(FadeOut(table), FadeOut(rect))
50
51 def what_is_segment_tree(self):
52 self.show_message("线段树是一棵树")
53
54 self.show_message("从数组进行构造")
55
56 array = AlgoVector(self, self.datas)
57 self.play(ShowCreation(array))
58 self.play(array.to_edge, LEFT)
59
60 #
61 tree = AlgoSegTree(self, self.datas)
62 self.play(ShowCreation(tree))
63 self.play(tree.to_edge, RIGHT)
64
65 def construct(self):
66 self.start_logo()
67 self.init_message("线段树")
68 self.show_diff()
69 #
70 self.wait(1)
71
72class SegmentTreeWhatIs(AlgoScene):
73 def __init__(self, **kwargs):

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected