构造一个树字符串和计数的列表字符串、列表

2023-09-11 07:08:26 作者:他的十二个字不信你数

我有一个地图<列表<字符串>,整数GT; ,其中每个条目的路径(名单,其中,字符串> )和计数

I have a Map<List<String>,Integer> where each entry is a path (List<String>) and a count

例如:

["a1", "a2", "a3"] => 4
["a1", "a2"] => 2
["b1", "b2", "b3"] => 3
["b1"] => 4
["b1", "b2"] => 3
["c1", "c2", "c3", "c4"] => 5

我想输出计数树的每个节点

I want to output a tree with count for each node

树:

- ROOT
-- a1 : 6
--- a2 : 6
---- a3 : 4
-- b1 : 7
--- b2 : 3
-- c1 : 5
--- c2 : 5
---- c3 : 5
----- c4 : 5

JSON结构:

JSON Structure:

{
    "name": "",
    "count": "",
    "children": [
        {
            "name": "",
            "count": "",
            "children": []
        }
    ]
}

什么是最有效的数据结构,以及如何使用它在这种情况下,(树应然后序列化为JSON树)?

What can be the most efficient data-structure and then how to use it in this case (the tree should be then serialized into a JSON tree)?

推荐答案

我会使用节点创建一个树形结构,然后使用XStream序列结构。下面的例子,希望这可以帮助你。

I would create a tree structure using Nodes, then use XStream to serialize the structure. Examples below, hope this helps you out.

皈依到节点结构

public static Node createNodes(Map<List<String>, Integer> map) {
    Map<String, Node> namemap = new HashMap<String, Node>();
    Node root = new Node();
    Node current;
    for (Entry<List<String>, Integer> path : map.entrySet()) {
        current = root;
        for (String nodename : path.getKey()) {
            Node p;
            if (!namemap.containsKey(nodename)){
                p = new Node(nodename, path.getValue());
                namemap.put(nodename, p);
            }else {
                p = namemap.get(nodename);
                p.addCost(path.getValue());
            }
            current.addChild(p);
            current = p;
        }

    }

    return root;
}

序列化

public static String toXML(Node n) {
    XStream xstream = new XStream(new JsonHierarchicalStreamDriver());
    xstream.alias("node", Node.class);
    return xstream.toXML(n);
}

节点对象

public class Node {

    private String name;
    private int count;
    private List<Node> children;

    public Node() {
        this(null, 0);
    }

    public Node(String name, int count) {
        this.name = name; 
        this.count = count;
        this.children = new ArrayList<Node>();
    }


    public void addChild(Node n) {
        for (Node nn : children) {
            if (nn.name.equals(n.name)) {
                return;
            }
        }
        this.children.add(n);
    }

    public void addCost(int i) {
        this.count += i;
    }
}

JSON输出

{"node": {
  "count": 0,
  "children": [
    {
      "name": "c1",
      "count": 5,
      "children": [
        {
          "name": "c2",
          "count": 5,
          "children": [
            {
              "name": "c3",
              "count": 5,
              "children": [
                {
                  "name": "c4",
                  "count": 5,
                  "children": [
                  ]
                }
              ]
            }
          ]
        }
      ]
    },
    {
      "name": "b1",
      "count": 10,
      "children": [
        {
          "name": "b2",
          "count": 6,
          "children": [
            {
              "name": "b3",
              "count": 3,
              "children": [
              ]
            }
          ]
        }
      ]
    },
    {
      "name": "a1",
      "count": 6,
      "children": [
        {
          "name": "a2",
          "count": 6,
          "children": [
            {
              "name": "a3",
              "count": 4,
              "children": [
              ]
            }
          ]
        }
      ]
    }
  ]
}}