2022-10-14 12:53:31 +02:00
|
|
|
from manim import *
|
|
|
|
|
|
|
|
import networkx as nx
|
|
|
|
|
|
|
|
class Tree(Scene):
|
|
|
|
def construct(self):
|
2022-10-14 21:38:22 +02:00
|
|
|
n = 2**6
|
2022-10-14 21:17:08 +02:00
|
|
|
l, r = 42, 59
|
|
|
|
#n, l, r = 4, 1, 3 # faster test
|
2022-10-14 21:38:22 +02:00
|
|
|
vertices = list(range(1, 2*n))
|
|
|
|
edges = [(v // 2, v) for v in vertices[1:]]
|
2022-10-14 12:53:31 +02:00
|
|
|
|
2022-10-14 21:38:22 +02:00
|
|
|
#graph_vis = Graph(vertices, edges, labels=True, layout="tree", layout_config={"vertex_spacing": (1.0, 1.0)}, root_vertex=1)
|
2022-10-14 21:17:08 +02:00
|
|
|
graph_vis = Graph(vertices, edges, labels=False, layout="tree", layout_config={"vertex_spacing": (.2, 1.1)}, root_vertex=1).set_color("#999999")
|
2022-10-14 21:38:22 +02:00
|
|
|
|
2022-10-14 21:17:08 +02:00
|
|
|
self.play(Create(graph_vis), Create(Brace(VGroup(graph_vis[n+l], graph_vis[n+r-1]))))
|
|
|
|
|
|
|
|
def color_edge(v, col):
|
|
|
|
anims = [ApplyMethod(graph_vis.edges[(v // 2, v)].set_color, col)] if v != 1 else []
|
|
|
|
anims.append(ApplyMethod(graph_vis[v].set_color, col))
|
|
|
|
|
|
|
|
self.play(*anims)
|
2022-10-14 21:38:22 +02:00
|
|
|
|
|
|
|
def query(v, tl, tr, ql, qr):
|
|
|
|
if tr <= ql or qr <= tl:
|
2022-10-14 21:17:08 +02:00
|
|
|
color_edge(v, RED)
|
2022-10-14 21:38:22 +02:00
|
|
|
return
|
|
|
|
if ql <= tl and tr <= qr:
|
2022-10-14 21:17:08 +02:00
|
|
|
color_edge(v, GREEN)
|
2022-10-14 21:38:22 +02:00
|
|
|
return
|
|
|
|
|
2022-10-14 21:17:08 +02:00
|
|
|
color_edge(v, YELLOW)
|
2022-10-14 21:38:22 +02:00
|
|
|
|
|
|
|
m = (tl + tr) // 2
|
|
|
|
query(2*v, tl, m, ql, qr)
|
|
|
|
query(2*v+1, m, tr, ql, qr)
|
|
|
|
|
|
|
|
|
2022-10-14 21:17:08 +02:00
|
|
|
query(1, 0, n, l, r)
|
2022-10-14 12:53:31 +02:00
|
|
|
self.wait(2)
|