Пример использования python для модификации DFS и сбора всех путей от корня до листа. Код не полностью проверен.
class Node:
def __init__(self, index):
self.index = index
self.children = []
def is_leaf(self):
return len(self.children) == 0
def __str__(self):
return str(self.index)
def _remove_node_on_path_until_finding_parent_of_curr_node(curr_node, curr_path):
while len(curr_path) > 0 and curr_node not in curr_path[-1].children:
curr_path.pop(-1)
def modified_dfs(root):
all_paths = []
stack_node_to_visit = [root]
curr_path = []
while len(stack_node_to_visit) > 0:
node = stack_node_to_visit.pop(-1)
_remove_node_on_path_until_finding_parent_of_curr_node(node, curr_path)
curr_path.append(node)
if node.is_leaf():
all_paths.append(curr_path.copy())
else:
for child in node.children:
stack_node_to_visit.append(child)
return all_paths
################# example usage ####################
root = Node(0)
for i in [1, 2, 3]:
tmp_child = Node(i)
root.children.append(tmp_child)
for j in [100, 200, 300, 400]:
tmp_child.children.append(Node(j + i))
path_list = modified_dfs(root)
for path in path_list:
index_list = [str(node) for node in path]
print(','.join(index_list))
В основном мы выполняем DFS как обычно, но сохраняем curr_path
при переходе от корня к листу, обновляя curr_path
.
Я не думаю, что существует опция для этого, но поскольку работа вокруг Вас может использовать Расширять ярлык Выбора, который является CTRL+ALT+Right. Не идеальный, но быстрее, чем удар CTRL+SHIFT+Right многократно для символа, который имеет много горбов верблюда.