英文:
How to store values in an array, traversing the tree?
问题
以下是您要翻译的代码部分:
所以我有一棵二叉树,我想将每个节点内的值存储在一个数组中。我尝试以这种方式解决我的问题,但是每次使用函数getAllElements()时,this.root.arrayOfObjs的大小都会增加。这不应该发生。或者如果应该发生,那应该是因为我已经添加或删除了二叉树中的节点。我尝试不使用根节点的数组,而是创建一个局部变量,但是这样我无法保存树中所有节点的所有对象。
class node {
node left, right;
<T> genericObject;
<T> ArrayList arrayOfObjs = new ArrayList<>();
<T> array_objects[];
node(){
left = null;
right = null;
}
class binarytree {
Node root;
binarytree() {
root = null;
}
public <T>[] getAllElements(Node t) {
this.root.array_objects = null;
if (t == null) {
return this.root.array_objects;
} else {
this.root.arrayOfObjs.add(t.genericObject);
}
getAllElements(t.left);
getAllElements(t.right);
this.root.array_objects = new <T>[t.arrayOfObjs.size()];
for (int i = 0; i < t.arrayOfObjs.size(); i++) {
this.root.array_object[i] = this.root.arrayOfObjs.get(i);
}
return this.root.array_objects;
}
}
希望这对您有所帮助。
英文:
So I have a binaryTree and I would like to store the values inside of each nodes, in an array. I tried to solve my problem in this way, however, everytime the function getAllElements() is used, the size of this.root.arrayOfObj increases. It shouldn't. Or if it should, it should because I have added or removed a node in the binaryTree. I tried to not use the arrays from the root and instead creating a local variable, but in this way I am not able to save all the objects from all the nodes in the tree.
class node {
node left, right;
<T>genericObject;
<T> ArrayList arrayOfObjs = new ArrayList<>();
<T>array_objects[];
node(){
left = null;
right = null;
}
class binarytree {
Node root;
binarytree() {
root = null;
}
public <T>[] getAllElements(Node t) {
this.root.array_objects = null;
if (t == null) {
return this.root.array_objects;
} else {
this.root.arrayOfObjs.add(t.genericObject);
}
getAllElements(t.left);
getAllElements(t.right);
this.root.array_objects = new <T>[t.arrayOfObjs.size()];
for (int i = 0; i < t.arrayOfObjs.size(); i++) {
this.root.array_object[i] = this.root.arrayOfObjs.get(i);
}
return this.root.array_objects;
}
}
答案1
得分: 0
以下是关于 Node
类的定义,对我来说是可以编译通过的。
class Node<T> {
Node<T> left, right;
T genericObject;
}
基于以上内容,这是关于 getAllElements()
方法的实现。
public void getAllElements(Node<T> aNode, List<T> list) {
if (aNode == null) {
return;
}
else {
getAllElements(aNode.left, list);
list.add(aNode.genericObject);
getAllElements(aNode.right, list);
}
}
你不能创建一个泛型数组,所以虽然这会通过编译...
T array_objects[];
但这不会通过编译:
array_objects = new T[50];
因此,你可以使用 List 而不是数组,因为你总是可以将 List
转换为数组。
英文:
Here is my definition of class Node
that does compile for me.
class Node<T> {
Node<T> left, right;
T genericObject;
}
Based on the above, here is my implementation of method getAllElements()
.
public void getAllElements(Node<T> aNode, List<T> list) {
if (aNode == null) {
return;
}
else {
getAllElements(aNode.left, list);
list.add(aNode.genericObject);
getAllElements(aNode.right, list);
}
}
You cannot create a generic array, so while this will compile...
T array_objects[];
This will not:
array_objects = new T[50];
Hence you can use a List rather than an array since you can always convert a List
into an array.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论