简体   繁体   English

java-如何从二维数组生成树

[英]java - How to generate Tree from two dimensional array

I want to generate a tree from an array(such as String), but I don't know how to do it. 我想从数组(例如String)生成树,但是我不知道该怎么做。

My input array: 我的输入数组:

[a, f, d, s]
[a, f, w]
[b, r]
[a, p]
[b, n, l]

I want to make a tree like this: 我想制作一棵这样的树:

Root 
 b 
  r 
  n 
   l 
 a 
  f
   w 
   d
    s 
  p

This is my code so far: 到目前为止,这是我的代码:

public class TreeGenerator {
    public TreeGenerator(E root, E[][] array){
        List list = Arrays.asList(array);//makes list
        Set set = new HashSet(list);//then set
        Node tree = new Node(root, set, 0);//makes whole tree

        System.out.println(tree.toString());//displays tree
    }

    public static void main(String[] args) {
        String[][] array = new String[][] { { "a", "f", "d", "s" }, { "a", "f", "w" }, { "b", "r" }, { "a", "p" }, { "b", "n", "l" } };
        for(String[] s : array){
            System.out.println(Arrays.toString(s));
        }
        new TreeGenerator("Root", array);
    }
}






public class Node {
    private final E nodeName;
    private final Node[] children;
    private final int depth; 
    /**
     * Constructs a Node and its children.
     *
     * @param name Node name
     * @param array Set of arrays
     * @param depth Index of arrays
     */
    public Node(E name, Set array, int depth) {
        nodeName = name;
        this.depth = depth;
        Map map = new HashMap();

        for (E[] line : array) { //iterates over arrays
            if (line.length > depth) { //checks if an element exists at this depth
                E common = line[depth]; //gets an element
                Set branch = map.get(common); //gets a branch for the element
                if (branch == null) { //if first such an element
                    branch = new HashSet(); //creates branch
                    map.put(common, branch); //adds for the element
                }
                branch.add(line); //adds the line for proper branch
            }
        }
        children = new Node[map.size()];
        int i = 0;
        depth++;//gets deeper
        for (Map.Entry entry : map.entrySet()) {//iterates over map
            children[i] = new Node(entry.getKey(), entry.getValue(), depth);//makes child
            i++;
        }
    }
}

You're code is a bit chaotic and you should most definitly not create the entire structure recursively in the constructor of Node . 您的代码有点混乱,您绝对不应该在Node的构造函数中递归地创建整个结构。 So i've provided some pseudocode 所以我提供了一些伪代码

define generateTree:
    input: string[][] struct
    output: tree

    node root

    //each string[] describes on path from the root to a leaf of the tree
    for string[] s in struct
        node tmp = root

        //complete a path
        for string n in s
            //check whether the next node is already part of the tree
            if hasChild(tmp , n)
                //continue with the next node in the path
                tmp = getChild(tmp , n)
            else
                //next node doesn't exist -> create it first
                node child = new node(n)
                add(tmp , child)
                tmp = child

    return tree(root)

Though this form of representation is rather inefficient and will produce gigantic amounts of data for bigger balanced trees. 尽管这种表示形式效率很低,并且将为更大的平衡树生成大量数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM