从堆创建 JTree

发布于 2024-10-01 21:24:05 字数 2120 浏览 3 评论 0原文

设置


我有一个堆,其中包含 intLevels 级别和 e 元素(均为 int),存储在 Objects,heapArray,高度为 intLevels,宽度为 Math.pow(2, intLevels)。出于假设目的,假设我输入 1、2、3、4、5、6、7、8 和 9。堆将如下所示:

       9
   8       6
 7   3   2   5
1 4

如果您要使用一系列 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]

有人知道如何获取这些信息并从中创建一个 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));

这会导致树看起来像:

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

编辑


我发现实现了答案的 buildTree(List<; Object[]>) 方法:

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

好像还是不行;树仍然是空的。

问题


有谁知道一种相对简单而灵活的方法来将这个 2D 数组变成具有给定信息的 JTree 吗?如果正确实现,向该树/堆/数组输入 1、2、3、4、5、6、7、8 和 9 应该得到与我上面展示的具体方式相同的结果。

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:

       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:

[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 = 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:

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

荒岛晴空 2024-10-08 21:24:06

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

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;
    }

}

结果是:

9
    8
        7
            1
            2
        3
    6
        2
        5

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文