从堆创建 JTree
设置
我有一个堆,其中包含 intLevels
级别和 e
元素(均为 int
),存储在 Object
s,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 int
s) stored in a 2D array of Object
s, 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(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).
The results are: