From 8071cc255468b214213d4e84b3abf18aaee36bfc Mon Sep 17 00:00:00 2001 From: Pavel 'LEdoian' Turinsky Date: Wed, 18 Mar 2020 23:22:46 +0100 Subject: [PATCH 1/4] =?UTF-8?q?Treelib=20dal=C5=A1=C3=AD=20TODO?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- seminar/treelib.py | 1 + 1 file changed, 1 insertion(+) diff --git a/seminar/treelib.py b/seminar/treelib.py index 8ad33c9c..f9ba04df 100644 --- a/seminar/treelib.py +++ b/seminar/treelib.py @@ -1,6 +1,7 @@ from django.core.exceptions import ObjectDoesNotExist # NOTE: node.prev a node.succ jsou implementovány přímo v models.TreeNode # TODO: Všechny tyto funkce se naivně spoléhají na to, že jako parametr dostanou nějaký TreeNode (některé možná zvládnou i None) +# TODO: Chceme, aby všechno nějak zvládlo None jako parametr. # Slouží k debugování pro rychlé získání představy o podobě podstromu pod tímto TreeNode. def print_tree(node,indent=0): From 78a667b42c4798eee73c4fd6f592026be67e0982 Mon Sep 17 00:00:00 2001 From: Pavel 'LEdoian' Turinsky Date: Wed, 18 Mar 2020 23:33:59 +0100 Subject: [PATCH 2/4] =?UTF-8?q?Treelib:=20gener=C3=A1tor=20a=20funkce=20na?= =?UTF-8?q?=20hled=C3=A1n=C3=AD=20n=C3=A1sleduj=C3=ADc=C3=ADch=20nod=C5=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- seminar/treelib.py | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/seminar/treelib.py b/seminar/treelib.py index f9ba04df..6e531c63 100644 --- a/seminar/treelib.py +++ b/seminar/treelib.py @@ -107,6 +107,12 @@ def all_children(node): yield br # Generátor následníků v "the-right-order" +# Bez tohoto vrcholu +def all_following(node): + current = general_next(node) + while current is not None: + yield current + current = general_next(current) ## Filtrační hledání # Najdi dalšího bratra nějakého typu, nebo None. @@ -115,15 +121,32 @@ def get_next_brother_of_type(node, type): for current in right_brothers(node): if isinstance(current, type): return current + return None def get_prev_brother_of_type(node, type): - pass + # Na tohle není rozumný generátor, ani ho asi nechceme, prostě to implementujeme cyklem. + current = node + while safe_pred(current) is not None: + current = safe_pred(current) + if isinstance(current, type): + return current + return None # Totéž pro "the-right-order" pořadí -def get_next_node_of_type(current, type): - pass -def get_next_node_of_type(current, type): - pass +def get_next_node_of_type(node, type): + for cur in all_folowing(node): + if isinstance(cur, type): + return cur + return None + +def get_prev_node_of_type(node, type): + # Na tohle není rozumný generátor, ani ho asi nechceme, prostě to implementujeme cyklem. + current = node + while general_prev(current) is not None: + current = general_prev(current) + if isinstance(current, type): + return current + return None From 62d6c0df02130a0102d8da72d6a347a548f8b11b Mon Sep 17 00:00:00 2001 From: Pavel 'LEdoian' Turinsky Date: Thu, 19 Mar 2020 00:22:18 +0100 Subject: [PATCH 3/4] =?UTF-8?q?Treelib:=20vyr=C3=A1b=C3=ADme=20nov=C3=A9?= =?UTF-8?q?=20uzly...=20snad?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- seminar/treelib.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/seminar/treelib.py b/seminar/treelib.py index 6e531c63..0f21454c 100644 --- a/seminar/treelib.py +++ b/seminar/treelib.py @@ -153,11 +153,25 @@ def get_prev_node_of_type(node, type): # Editace stromu: def create_node_after(predecessor, type, **kwargs): - pass + new_node = type.objects.create(**kwargs) + new_node.save() + succ = predecessor.succ + predecessor.succ = new_node + predecessor.save() + new_node.succ = succ + new_node.save() # Vyrábí prvního syna, ostatní nalepí za (existují-li) def create_child(parent, type, **kwargs): - pass + new_node = type.objects.create(**kwargs) + new_node.save() + orig_child = parent.first_child + parent.first_child = new_node + parent.save() + if orig_child is not None: + # Přidáme původního prvního syna jako potomka nového vrcholu + new_node.succ = orig_child + new_node.save() def create_node_before(some, arguments, but, i, dont, know, which, yet): pass From ac0c616e1314e8208083864c392c9fb19c134b79 Mon Sep 17 00:00:00 2001 From: Pavel 'LEdoian' Turinsky Date: Thu, 19 Mar 2020 00:37:08 +0100 Subject: [PATCH 4/4] =?UTF-8?q?Treelib:=20obecn=C3=BD=20swap=20implementov?= =?UTF-8?q?at=20nebudu.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Moc edge-cases. --- seminar/treelib.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/seminar/treelib.py b/seminar/treelib.py index 0f21454c..f890868b 100644 --- a/seminar/treelib.py +++ b/seminar/treelib.py @@ -179,7 +179,7 @@ def create_node_before(some, arguments, but, i, dont, know, which, yet): # ValueError, pokud je (aspoň) jeden parametr None def swap(node, other): - pass + raise NotImplementedError("YAGNI (You aren't gonna need it).") def swap_pred(node): pass