| 158 | print("="*80 + "\n") |
| 159 | |
| 160 | def dfs_print(node_id: str, prefix: str, is_last: bool, depth: int): |
| 161 | if depth > max_depth: |
| 162 | return |
| 163 | |
| 164 | node = nodes[node_id] |
| 165 | |
| 166 | # 打印当前节点 |
| 167 | current_prefix = "└── " if is_last else "├── " |
| 168 | print(prefix + current_prefix + str(node)) |
| 169 | |
| 170 | # 更新前缀 |
| 171 | extension = " " if is_last else "│ " |
| 172 | new_prefix = prefix + extension |
| 173 | |
| 174 | # 递归打印子节点 |
| 175 | children = node.children_ids |
| 176 | for i, child_id in enumerate(children): |
| 177 | is_last_child = (i == len(children) - 1) |
| 178 | dfs_print(child_id, new_prefix, is_last_child, depth + 1) |
| 179 | |
| 180 | # 打印根节点 |
| 181 | dfs_print(root_id, "", True, 0) |