DAG全路径输出 (xmind转测试用例)

1 需求

旧版本xmind是xml格式, 11版本后变成json格式, 但数据层级一致

输出如下层级数据的全路径

节点A0
|---节点B01
|---|---节点C011
|---|---节点C012
|---节点B02
|---|---节点C021
|---|---|---节点D0211

2 样例数据

{
  "title": "节点A0",
  "children": [
    {
      "title": "节点B01",
      "children": [
        {
          "title": "节点C011",
          "children": []
        },
        {
          "title": "节点C012",
          "children": []
        }
      ]
    },
    {
      "title": "节点B02",
      "children": [
        {
          "title": "节点C021",
          "children": [
            {
              "title": "节点D0211",
              "children": []
            }
          ]
        }
      ]
    }
  ]
}

3 java代码

整体思路有点类似于汉诺塔

import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Stack;


public class App {

    public static void main(String[] args) throws IOException {

        InputStream is = App.class.getClassLoader().getResourceAsStream("dag.json");

        ObjectMapper mapper = new ObjectMapper();
        Node root = mapper.readValue(is, Node.class);

        Stack<Node> leftStack = new Stack<>();
        Stack<List<Node>> rightStack = new Stack<>();

        leftStack.push(root);
        rightStack.push(root.getChildren());

        while ( !leftStack.isEmpty()) {
            List<Node> rightNodes = rightStack.peek();

            if (rightNodes != null && rightNodes.size() > 0) {
                // 右栈内容不为空, 将节点左移
                Node moveLeftNode = rightNodes.remove(0);
                leftStack.push(moveLeftNode);
                rightStack.push(moveLeftNode.getChildren());

                continue;
            } else {
                // 右栈内容为空时, 表示已遍历到末尾
                // 提取左栈内容, 拼接形成完整路径
                String text = "";
                for (Node n: leftStack) {
                    text += n.getTitle() + " -> ";
                }
                if (text.length() > 0) text = text.substring(0, text.length() - 4);
                System.out.println(text);
            }

            // 输出完成后, 执行去栈操作
            while (true) {
                if (!rightStack.isEmpty() && (rightStack.peek() == null || rightStack.peek().size() == 0)) {
                    rightStack.pop();
                    leftStack.pop();
                    continue;
                }
                break;
            }
        }
    }
}
import java.util.List;

public class Node {

    private String title;
    private List<Node> children;

    public Node() {
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public List<Node> getChildren() {
        return children;
    }

    public void setChildren(List<Node> children) {
        this.children = children;
    }
}

输出结果

节点A0 -> 节点B01 -> 节点C011
节点A0 -> 节点B01 -> 节点C012
节点A0 -> 节点B02 -> 节点C021 -> 节点D0211
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值