Java 从给定数组构造树

Java 从给定数组构造树,java,algorithm,graph,tree,Java,Algorithm,Graph,Tree,我想从给定的数组和根构造一个图,其中节点如下所述 static class TreeNode { private int value; private ArrayList<TreeNode> children; public TreeNode(int nodeValue) { this.value = nodeValue; this.children = new ArrayList<TreeNode>();

我想从给定的数组和根构造一个图,其中节点如下所述

static class TreeNode {

    private int value;
    private ArrayList<TreeNode> children; 

    public TreeNode(int nodeValue) {
        this.value = nodeValue;
        this.children = new ArrayList<TreeNode>();
    }

    public int getValue() {
        return this.value;
    }

    public void addChild(TreeNode child) {
        this.children.add(child);
    }

    public ArrayList<TreeNode> getChildren() {
        return this.children;
    } 
} 
     2 - 3
    / \
   1   4
  / |  |
 0  5  6
如果T[p]=Q和p,则数组T描述城市网络≠ Q、 然后,在城市P和Q之间有一条直达道路。如果指数2为根,则下图所示

static class TreeNode {

    private int value;
    private ArrayList<TreeNode> children; 

    public TreeNode(int nodeValue) {
        this.value = nodeValue;
        this.children = new ArrayList<TreeNode>();
    }

    public int getValue() {
        return this.value;
    }

    public void addChild(TreeNode child) {
        this.children.add(child);
    }

    public ArrayList<TreeNode> getChildren() {
        return this.children;
    } 
} 
     2 - 3
    / \
   1   4
  / |  |
 0  5  6
显然,我可以为给定的数组手动执行

    final int N = 7;
    TreeNode[] nodes = new TreeNode[N];

    for (int i = 0; i < N; i++) {
        nodes[i] = new TreeNode(i);
    }


    TreeNode root = nodes[2];

    root.addChild(nodes[1]);
    root.addChild(nodes[3]);
    root.addChild(nodes[4]);


    nodes[1].addChild(nodes[0]);
    nodes[1].addChild(nodes[5]);

    nodes[4].addChild(nodes[6]);
final int N=7;
TreeNode[]节点=新的TreeNode[N];
对于(int i=0;i
在给定数组和K值后,如何以编程方式构造?请提供帮助。

迭代所有节点, 对于每个节点,获取节点的值,并将当前节点添加到该值处的节点

for (int i = 0; i < N; i++) {
    nodes[nodes[i].getValue()].addChild(nodes[i])
}
for(int i=0;i
构建
TreeNode[]
阵列后,很容易:

TreeNode root = null;
for (int i=0; i<T.length; ++i) {
    if (T[i] == i) { // if it's a root node
        //TODO: Test for multiple root nodes here
        root = nodes[i];
    } else {
        nodes[T[i]].addChild(nodes[i]);
    }
}
treenoderoot=null;

对于(int i=0;i我写了一个答案,但是,它并没有显示所有的孩子。下面提供了代码

public class App {

    static class TreeNode {

        private int value;
        private ArrayList<TreeNode> children;

        public TreeNode(int nodeValue) {
            this.value = nodeValue;
            this.children = new ArrayList<TreeNode>();
        }

        public int getValue() {
            return this.value;
        }

        public void addChild(TreeNode child) {
            this.children.add(child);
        }

        public ArrayList<TreeNode> getChildren() {
            return this.children;
        }
    }


    public static TreeNode buildGraph(int[] T, int K) {

        final int N = T.length;

        TreeNode[] nodes = new TreeNode[N];

        for (int i = 0; i < N; i++) {
            nodes[i] = new TreeNode(i);
        }

        /*
            T[0] = 1
            T[1] = 2
            T[2] = 3
            T[3] = 3
            T[4] = 2
            T[5] = 1
            T[6] = 4

                 2 - 3
                / \
               1   4
              / |  |
             0  5  6
        * */

        TreeNode root = nodes[K];

        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);

        boolean[] visited = new boolean[N];

        while (!queue.isEmpty()) {

            TreeNode node = queue.poll();
            int index = node.getValue();

            visited[index] = true;

            // T[3] = 3 is a leaf with no further connection to develop
            if (index == T[index]) {
                continue;
            }

            // 2 != 3 for the root node and we havent visited node 3 earlier
            if (index != T[index] && !visited[T[index]]) {

                node.addChild(nodes[T[index]]);
                queue.offer(nodes[T[index]]);
            }

            int left = 0, right = N - 1;

            while (left < index && right > index) {

                if (T[left] == index) {

                    node.addChild(nodes[left]);
                    queue.offer(nodes[left]);
                }

                if (T[right] == index) {

                    node.addChild(nodes[right]);
                    queue.offer(nodes[right]);
                }

                left++;
                right--;
            }
        }

        return root;
    }

    public static void main(String[] args) {

        int[] T = new int[7];

        T[0] = 1;
        T[1] = 2;
        T[2] = 3;
        T[3] = 3;
        T[4] = 2;
        T[5] = 1;
        T[6] = 4;

        TreeNode root = buildGraph(T, 2);

        System.out.println("The root = " + root.getValue());

        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);

        while(!queue.isEmpty()){

            TreeNode node = queue.poll();

            ArrayList<TreeNode> children = node.getChildren();

            for (int i = 0; i < children.size(); i++) {

                TreeNode child = children.get(i);
                queue.offer(child);

                System.out.println("Parent "+ node.getValue()+ " has children = "+ child.getValue());
            }
        }
    }
}
谁能帮我纠正一下我是怎么想念其他孩子的

更新 我是根据另一个似乎更简单的答案写的

public static TreeNode buildGraph1(int[] T, int K) {

        final int N = T.length;

        TreeNode[] nodes = new TreeNode[N];

        for (int i = 0; i < N; i++) {
            nodes[i] = new TreeNode(i);
        }

        /*
       T[children] = parent if the children != K

            T[0] = 1
            T[1] = 2
            T[2] = 3
            T[3] = 3
            T[4] = 2
            T[5] = 1
            T[6] = 4

                 2 - 3
                / \
               1   4
              / |  |
             0  5  6
        * */

        TreeNode root = nodes[K];
        int value = root.getValue();

        if (T[K] != K) {
            nodes[K].addChild(nodes[T[K]]);
        }

        for (int i = 0; i < T.length; ++i) {

            if (K == i) {
                continue;
            }

            if (T[i] != i) {
                nodes[T[i]].addChild(nodes[i]);
            }
        }

        return root;
    }

你试过什么?@nicomp我还在试着把它写好。等我写完后,我会在这里更新done@nicomp我尝试并提供了一个答案我希望接受您的答案,但输出不完全正确。我运行代码并得到:
根=3父3有子=2父2有子=1父2有子=4父1 has children=0父1 has children=5父4 has children=6
根为2,父3 has children=2不正确,实际上相反true@Arefe这实际上与您提供的数据不符。
T[2]=3
表示3是2的父级,因此2不能是根。并且
T[3]=3
表示3没有父项,因此它是根。是否有一些其他规则和参数使2成为根?如果是,如果3真的是树的一部分,例如
T[3]=7;T[7]=8;T[8]=8;T[9]=8;T[10]=9;
等等?是的,根索引
K
在您需要开始的地方给出。因此,即使T[children]=父,如果T[K]=V,那么K是V的父。现在如何更改代码
The root = 2
Parent 2 has children = 3
Parent 2 has children = 1
Parent 2 has children = 4




Parent 1 has children = 0
Parent 1 has children = 5


Parent 4 has children = 6