如何将值存储在数组中,遍历树?

huangapple go评论75阅读模式
英文:

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;
&lt;T&gt;genericObject;
&lt;T&gt; ArrayList arrayOfObjs = new ArrayList&lt;&gt;();
&lt;T&gt;array_objects[];
node(){
left = null;
right = null;
}
class binarytree {
Node root;
binarytree() {
root = null;
}
public &lt;T&gt;[] 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 &lt;T&gt;[t.arrayOfObjs.size()];
for (int i = 0; i &lt; 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&lt;T&gt; {
    Node&lt;T&gt; left, right;
    T genericObject;
}

Based on the above, here is my implementation of method getAllElements().

public void getAllElements(Node&lt;T&gt; aNode, List&lt;T&gt; 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.

huangapple
  • 本文由 发表于 2020年10月2日 22:09:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/64173006.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定