Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 用字符串和计数列表构造树_Java_Algorithm_Tree - Fatal编程技术网

Java 用字符串和计数列表构造树

Java 用字符串和计数列表构造树,java,algorithm,tree,Java,Algorithm,Tree,我有一个映射,其中每个条目都是一个路径(列表)和一个计数 例: 我想输出一个每个节点都有计数的树 树: JSON结构: { "name": "", "count": "", "children": [ { "name": "", "count": "", "children": [] } ] } 什么是最有效的数据结构,以及在这种情况下如何使用它(然后将树序列

我有一个
映射
,其中每个条目都是一个路径(
列表
)和一个计数

例:

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

树:

JSON结构:

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

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

我个人会创建一个包含子节点和我的计数值的节点类,然后告诉JSON序列化程序如何根据需要正确序列化它。

我会将您的映射更改为:

Map<String[], Integer>
Map
如果您想保留列表,请使用
ArrayList
trim(
)将其删除

然后,您必须决定是使用
HashMap
,还是确实需要一个树(
TreeMap
)。 在后一种情况下,您应该创建一个路径对象,其中包含一个字段
String[]
List

然后,此路径对象必须实现您必须实现的
可比

要执行
序列化
(Json或其他系统),通常需要写出键值对。再次阅读后,您将通过输入(键、值)对再次构建地图或树


在json或其他序列化文件中使用树结构不是一个好主意。您几乎永远不会需要它。

我将使用节点创建一个树结构,然后使用XStream序列化该结构。下面的例子,希望对您有所帮助

到节点结构的转换

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 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;
    }
}
{"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": [
              ]
            }
          ]
        }
      ]
    }
  ]
}}

如何在JSON中序列化树。你能举个例子吗?为什么键是列表而不是单个值?为什么在不同的键前面有不同数量的破折号?你原来的结构似乎很复杂,你为什么选择这种结构而不是使用节点的更传统的树结构?你能给出一个预期JSON的例子吗?@atomman是的,因为我有一个路径列表,我不能依赖分隔符(因为我不知道路径的内容),所以我保留了list@fgeJSON应该简单地表示一棵树,其中每个节点都包含一个子节点数组和一个计数
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;
    }
}
{"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": [
              ]
            }
          ]
        }
      ]
    }
  ]
}}