Improving depth-first algorithm

This commit is contained in:
gamerdonkey 2025-09-12 03:20:47 +00:00
parent b8cc999ffd
commit 7514e6bb22

View File

@ -4,10 +4,13 @@ from anytree.importer import DictImporter
from sys import argv
def build_table_depth_first(node, rows, leaf_count=0):
def build_table_depth_first(node, rows=None):
if rows is None:
rows = []
for child in sorted(node.children, key=lambda x: x.height, reverse=True):
leaf_count = build_table_depth_first(child, rows, leaf_count)
rows = build_table_depth_first(child, rows)
colspan = 0
if node.siblings:
@ -17,20 +20,17 @@ def build_table_depth_first(node, rows, leaf_count=0):
colspan = max_sibling_height - node.height + 1
if node.is_leaf:
cur_row = leaf_count
rows[cur_row].append(f'<th>{node.name}</th>')
rows.append([f'<th>{node.name}</th>'])
if colspan > 1:
rows[cur_row].append(f'<td colspan="{colspan-1}" class="filler"></td>')
leaf_count += 1
rows[-1].append(f'<td colspan="{colspan-1}" class="filler"></td>')
else:
child_leaves = len(node.leaves)
cur_row = leaf_count - child_leaves
cur_row = len(rows) - child_leaves
rows[cur_row].append(f"<td rowspan='{child_leaves}' colspan='{colspan}' title='{node.description}'>{node.name}</td>")
return leaf_count
return rows
def generate_steps_depth_first(node, steps=[]):
@ -108,12 +108,7 @@ for sub_recipe in sub_recipes:
print("<p>")
print("<table cellspacing='0' border=true>")
output_rows = []
for i in range(len(sub_recipe.leaves)):
output_rows.append([])
build_table_depth_first(sub_recipe, output_rows)
output_rows = build_table_depth_first(sub_recipe)
for row in output_rows:
print("<tr>")