From 78a667b42c4798eee73c4fd6f592026be67e0982 Mon Sep 17 00:00:00 2001 From: Pavel 'LEdoian' Turinsky Date: Wed, 18 Mar 2020 23:33:59 +0100 Subject: [PATCH] =?UTF-8?q?Treelib:=20gener=C3=A1tor=20a=20funkce=20na=20h?= =?UTF-8?q?led=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