从堆创建JTree

时间:2022-10-27 12:37:16

Setup


I have a heap with intLevels levels and e elements (both ints) stored in a 2D array of Objects, heapArray, which is intLevels tall and Math.pow(2, intLevels) wide. For hypothetical purposes, let's say I input 1, 2, 3, 4, 5, 6, 7, 8, and 9. The heap will look something like this:

我有一个堆有intLevels级别和e元素(两个整数)存储在一个对象的2D数组,heapArray,它是intLevels高和Math.pow(2,intLevels)宽。出于假设目的,假设我输入1,2,3,4,5,6,7,8和9.堆将看起来像这样:

       9
   8       6
 7   3   2   5
1 4

and if you were to print it with a series of java.util.Arrays.toString(Object[] a)s, it would look like this:

如果你用一系列java.util.Arrays.toString(Object [] a)s打印它,它看起来像这样:

[9, null, null, null, null, null, null, null]
[8, 6, null, null, null, null, null, null]
[7, 3, 2, 5, null, null, null, null]
[1, 4, null, null, null, null, null, null]

does anyone have any idea how to take this information and create a JTree out of it? For anyone who doesn't know, a JTree works much like a linked list. You have a root node which you add more nodes to, and you can add additional nodes on those. I know for a fact that if the only heap I was dealing with was this one, I would be able to make the tree this way:

有没有人知道如何获取这些信息并从中创建一个JTree?对于任何不知道的人来说,JTree的工作方式与链表非常相似。您有一个根节点可以添加更多节点,您可以在这些节点上添加其他节点。我知道一个事实,如果我正在处理的唯一堆是这个,我将能够以这种方式制作树:

jTree = new javax.swing.JTree();

treeNode1 = new javax.swing.tree.DefaultMutableTreeNode(9);
treeNode2 = new javax.swing.tree.DefaultMutableTreeNode(8);
treeNode3 = new javax.swing.tree.DefaultMutableTreeNode(7);
treeNode4 = new javax.swing.tree.DefaultMutableTreeNode(1);
treeNode3.add(treeNode4);
treeNode4 = new javax.swing.tree.DefaultMutableTreeNode(4);
treeNode3.add(treeNode4);
treeNode2.add(treeNode3);
treeNode3 = new javax.swing.tree.DefaultMutableTreeNode(3);
treeNode2.add(treeNode3);
treeNode1.add(treeNode2);
treeNode2 = new javax.swing.tree.DefaultMutableTreeNode(6);
treeNode3 = new javax.swing.tree.DefaultMutableTreeNode(2);
treeNode2.add(treeNode3);
treeNode3 = new javax.swing.tree.DefaultMutableTreeNode(5);
treeNode2.add(treeNode3);
treeNode1.add(treeNode2);
jTree.setModel(new javax.swing.tree.DefaultTreeModel(treeNode1));

which results in a tree looking like:

这导致树看起来像:

9
├8
│├7
││├1
││└4
│└3
└6
 ├2
 └5

Edit


I found implemented the answer's buildTree(List<Object[]>) method:

我发现实现了答案的buildTree(List )方法:

java.util.List<Object[]> objectArrays = new java.util.ArrayList<Object[]>();
objectArrays.addAll(Arrays.asList(heapArray));
jTree1 = buildTree(objectArrays);

it still doesn't seem to work; the tree remains empty.

它似乎仍然无效;树仍然是空的。

Question


Does anyone know a relatively easy yet flexible way to make this 2D array into a JTree with the given information? If implemented correctly, inputting 1, 2, 3, 4, 5, 6, 7, 8, and 9 to this tree/heap/array should end up with the same result as the concrete way I showed above.

有没有人知道一种相对简单而灵活的方法,可以将这个二维阵列变成一个具有给定信息的JTree?如果正确实现,向这个树/堆/数组输入1,2,3,4,5,6,7,8和9应该与我上面显示的具体方法结果相同。

1 个解决方案

#1


1  

This class should do the trick. I've used recursion to allow any number of child levels (assuming there are enough object arrays and objects to support the tree).

这个课应该可以解决问题。我已经使用递归来允许任意数量的子级别(假设有足够的对象数组和对象来支持树)。

import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

public class TreeBuilder {

    public static void main(final String[] args) {

        // build the list of object arrays
        List<Object[]> objectArrays = new ArrayList<Object[]>();
        Object[] array1 = {9, null, null, null, null, null, null, null};
        Object[] array2 = {8, 6, null, null, null, null, null, null};
        Object[] array3 = {7, 3, 2, 5, null, null, null, null};
        Object[] array4 = {1, 2, null, null, null, null, null, null};
        objectArrays.add(array1);
        objectArrays.add(array2);
        objectArrays.add(array3);
        objectArrays.add(array4);

        // call the method under test
        JTree result = buildTree(objectArrays);

        // print the results
        if(result == null) {
            System.out.println("FAIL: returned null.");
        } else {
            recursePrintTree((DefaultMutableTreeNode)result.getModel().getRoot(), 0);
        }
    }


    public static void recursePrintTree(
            final DefaultMutableTreeNode treeNode,
            final int indentation) {

        // print the indentation spaces
        for(int index = 0; index < indentation; index++) {
            System.out.print("    ");
        }
        // print the value of the node
        System.out.println(treeNode.getUserObject());

        // enumerate the children of the node
        Enumeration enumeration = treeNode.children();
        while(enumeration.hasMoreElements()) {
            DefaultMutableTreeNode child =
                (DefaultMutableTreeNode)enumeration.nextElement();
            recursePrintTree(child, indentation + 1);
        }
    }


    public static JTree buildTree(final List<Object[]> objectArrays) {
        JTree jTree = new JTree();

        DefaultMutableTreeNode node = recurseBuildTree(objectArrays, 0, 0);
        if(node != null) {
            jTree.setModel(new DefaultTreeModel(node));
        }

        return jTree;
    }


    private static DefaultMutableTreeNode recurseBuildTree(
            final List<Object[]> objectArrays,
            final int objectArrayIndex,
            final int itemIndex) {

        DefaultMutableTreeNode node = null;

        if(objectArrayIndex < objectArrays.size()) {
            Object[] objectArray = objectArrays.get(objectArrayIndex);
            if(itemIndex < objectArray.length) {
                Object obj = objectArray[itemIndex];
                if(obj != null) {
                    node = new DefaultMutableTreeNode(obj);

                    DefaultMutableTreeNode childNode = recurseBuildTree(
                        objectArrays, objectArrayIndex + 1, itemIndex * 2);

                    if(childNode != null) {
                        node.add(childNode);
                    }

                    childNode = recurseBuildTree(
                        objectArrays, objectArrayIndex + 1, (itemIndex * 2) + 1);

                    if(childNode != null) {
                        node.add(childNode);
                    }
                }
            }
        }

        return node;
    }

}

The results are:

结果是:

9
    8
        7
            1
            2
        3
    6
        2
        5

#1


1  

This class should do the trick. I've used recursion to allow any number of child levels (assuming there are enough object arrays and objects to support the tree).

这个课应该可以解决问题。我已经使用递归来允许任意数量的子级别(假设有足够的对象数组和对象来支持树)。

import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

public class TreeBuilder {

    public static void main(final String[] args) {

        // build the list of object arrays
        List<Object[]> objectArrays = new ArrayList<Object[]>();
        Object[] array1 = {9, null, null, null, null, null, null, null};
        Object[] array2 = {8, 6, null, null, null, null, null, null};
        Object[] array3 = {7, 3, 2, 5, null, null, null, null};
        Object[] array4 = {1, 2, null, null, null, null, null, null};
        objectArrays.add(array1);
        objectArrays.add(array2);
        objectArrays.add(array3);
        objectArrays.add(array4);

        // call the method under test
        JTree result = buildTree(objectArrays);

        // print the results
        if(result == null) {
            System.out.println("FAIL: returned null.");
        } else {
            recursePrintTree((DefaultMutableTreeNode)result.getModel().getRoot(), 0);
        }
    }


    public static void recursePrintTree(
            final DefaultMutableTreeNode treeNode,
            final int indentation) {

        // print the indentation spaces
        for(int index = 0; index < indentation; index++) {
            System.out.print("    ");
        }
        // print the value of the node
        System.out.println(treeNode.getUserObject());

        // enumerate the children of the node
        Enumeration enumeration = treeNode.children();
        while(enumeration.hasMoreElements()) {
            DefaultMutableTreeNode child =
                (DefaultMutableTreeNode)enumeration.nextElement();
            recursePrintTree(child, indentation + 1);
        }
    }


    public static JTree buildTree(final List<Object[]> objectArrays) {
        JTree jTree = new JTree();

        DefaultMutableTreeNode node = recurseBuildTree(objectArrays, 0, 0);
        if(node != null) {
            jTree.setModel(new DefaultTreeModel(node));
        }

        return jTree;
    }


    private static DefaultMutableTreeNode recurseBuildTree(
            final List<Object[]> objectArrays,
            final int objectArrayIndex,
            final int itemIndex) {

        DefaultMutableTreeNode node = null;

        if(objectArrayIndex < objectArrays.size()) {
            Object[] objectArray = objectArrays.get(objectArrayIndex);
            if(itemIndex < objectArray.length) {
                Object obj = objectArray[itemIndex];
                if(obj != null) {
                    node = new DefaultMutableTreeNode(obj);

                    DefaultMutableTreeNode childNode = recurseBuildTree(
                        objectArrays, objectArrayIndex + 1, itemIndex * 2);

                    if(childNode != null) {
                        node.add(childNode);
                    }

                    childNode = recurseBuildTree(
                        objectArrays, objectArrayIndex + 1, (itemIndex * 2) + 1);

                    if(childNode != null) {
                        node.add(childNode);
                    }
                }
            }
        }

        return node;
    }

}

The results are:

结果是:

9
    8
        7
            1
            2
        3
    6
        2
        5