-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinked_list_cycle.py
157 lines (117 loc) · 3.88 KB
/
linked_list_cycle.py
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
from manimlib.imports import *
from logo import LogoMixin
from linked_list import Node, LinkedListVMobject
from math import sin, cos, pi
class LinkedListCycleBase(Scene, LogoMixin):
CONFIG = {"show_construction": False}
def construct(self):
LogoMixin.construct(self)
self.l1 = Node(
1,
Node(
2,
Node(3, Node(4, Node(5, Node(6, Node(7, Node(8, Node(9, Node(10)))))))),
),
)
node = self.l1
to_add = []
for i in range(-3, 7):
node.scale(0.5)
node.set_color(BLUE)
to_add.append(node)
if i < 0:
node.move_to(RIGHT * (i + 3) * 2.5 + LEFT * 6.4 + UP * 3)
else:
loc = (
3 * RIGHT * sin(i / 7 * pi * 2)
+ 3 * UP * cos(i / 7 * pi * 2)
+ RIGHT
+ UP * 0.5
)
node.move_to(loc + DOWN)
node = node.next
node = self.l1
tail = None
while node:
if node.next:
arrow = Arrow(node, node.next)
to_add.append(arrow)
self.add(arrow)
arrow.set_opacity(0.5)
node.arrow = arrow
tail = node
node = node.next
tail.arrow = Arrow(tail, self.l1.next.next.next)
self.add(tail.arrow)
tail.next = self.l1.next.next.next
tail.arrow.set_opacity(0.5)
to_add.append(tail.arrow)
if self.CONFIG["show_construction"]:
self.play(LaggedStart(*[Write(x) for x in to_add], lag_ratio=0.1))
else:
self.add(*to_add)
self.wait(1)
class LinkedListCyclePlain(LinkedListCycleBase):
CONFIG = {"show_construction": True}
def construct(self):
LinkedListCycleBase.construct(self)
class LinkedListCycleFastSlow(LinkedListCycleBase):
CONFIG = {"show_construction": False}
def construct(self):
LinkedListCycleBase.construct(self)
node = self.l1
dot1 = Dot()
dot1.move_to(self.l1)
dot1.set_fill(RED, opacity=0.8)
dot2 = Dot()
dot2.move_to(self.l1)
dot2.set_fill(RED, opacity=0.8)
def move(src, dest, dot):
return (
ApplyMethod(
dot.move_to,
dest.val_obj.get_center(),
path_arc=120 * DEGREES,
),
)
fast = node
for i in range(20):
if fast == node and i != 0:
break
single = move(node, node.next, dot1)
double = move(node, fast.next.next, dot2)
self.play(*(single + double))
fast = fast.next.next
node = node.next
self.play(Indicate(node))
self.wait(3)
class LinkedListCycleStartNode(LinkedListCycleBase):
CONFIG = {"show_construction": False}
def construct(self):
LinkedListCycleBase.construct(self)
node = self.l1
dot1 = Dot()
dot1.move_to(self.l1)
dot1.set_fill(RED, opacity=0.8)
dot2 = Dot()
dot2.move_to(self.l1.next.next.next.next.next.next.next)
dot2.set_fill(RED, opacity=0.8)
def move(src, dest, dot):
return (
ApplyMethod(
dot.move_to,
dest.val_obj.get_center(),
path_arc=120 * DEGREES,
),
)
fast = node.next.next.next.next.next.next.next
for i in range(20):
if fast == node and i != 0:
break
single = move(node, node.next, dot1)
double = move(node, fast.next, dot2)
self.play(*(single + double))
fast = fast.next
node = node.next
self.play(Indicate(node))
self.wait(3)