diff --git a/output_html.py b/output_html.py index 0687ff3..8bf1d6b 100644 --- a/output_html.py +++ b/output_html.py @@ -2,6 +2,24 @@ from anytree import LevelOrderGroupIter from anytree.importer import JsonImporter from sys import argv + +def build_rows_depth_first(node, rows, leaf_count=0): + + for child in sorted(node.children, key=lambda x: x.size, reverse=True): + leaf_count = build_rows_depth_first(child, rows, leaf_count) + + if node.is_leaf: + cur_row = leaf_count + rows[cur_row].append(f'{node.name}') + leaf_count += 1 + else: + child_leaves = len(node.leaves) + cur_row = leaf_count - child_leaves + rows[cur_row].append(f'{node.name}') + + return leaf_count + + if len(argv) < 2: print(f"usage: {argv[0]} ") exit() @@ -9,62 +27,12 @@ if len(argv) < 2: with open(argv[1], 'r') as f: root = JsonImporter().read(f) -rows = [] -leaves = [] - -for i in range(len(root.leaves)): - rows.append([]) - leaves.append({"name":"wrong"}) - -cur_row = 0 -for level in LevelOrderGroupIter(root): - cur_row = 0 - - for node in sorted(level, key=lambda x: x.size, reverse=True): - leaf_count = len(node.leaves) - if False: #node in root.leaves: - leaves[cur_row] = node - else: - rows[cur_row].append(f'{node.name}') - - cur_row += max(leaf_count, 1) - last_depth = node.depth - -print(leaves) - -for i, leaf in enumerate(leaves): - #rows[i].append(f"{leaf.name}") - rows[i].reverse() - - print("") - for cell in rows[i]: - print(f" {cell}") - print("") - - - output_rows = [] -rows = [] for i in range(len(root.leaves)): output_rows.append([]) -def depth_first_search(node, rows, leaf_count): - - for child in sorted(node.children, key=lambda x: x.size, reverse=True): - leaf_count = depth_first_search(child, rows, leaf_count) - - if node.is_leaf: - cur_row = leaf_count - rows[cur_row].append(f'{node.name}') - leaf_count += 1 - else: - cur_row = leaf_count - len(node.leaves) - rows[cur_row].append(f'{node.name}') - - return leaf_count - -depth_first_search(root, output_rows, 0) +build_rows_depth_first(root, output_rows) for row in output_rows: print("")