Slide 28
Slide 28 text
1 56
2 31
3
32 55
23 30
3 22
6
33 34
7
35 52
8
53 54
4 5 6 11 12 21 24 29
25 26
18
27 28
7 8 9 10 13 20
13
36 51
19
37 38
20
39 44
21
45 46
22
47 50
26
40 41
27
42 43
28
48 49
14 15 16 17 18 19
Add node to a NS tree
? ?
from django.db.models.fields import PositiveIntegerField
from django.db.models.expressions import F, Case, When
def add_child(parent: Node, child: Node):
Node.objects.filter(right__gte=parent.right).update(
left=Case(
When(left__gte=node.left, then=F('left') + 2),
default=F('left'),
output_field=PositiveIntegerField(),
),
right=F('right') + 2,
)
child.update(left=parent.left + 1,
right=parent.right - 1)
parent = Node.objects.get(pk=14)
new_child = Node.objects.create(...) # -> pk=100
add_child(parent, new_child)