如何向树中添加多个子节点

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

How to add multiple child nodes to a tree

问题

class Node:
    def __init__(self, data):
        self.data = data
        self.children = []
        self.parent = None

class Tree:
    def __init__(self, root_data):
        self.root = Node(root_data)

    def add_child(self, parent_node, child_data):
        child_node = Node(child_data)
        child_node.parent = parent_node
        parent_node.children.append(child_node)

# Example usage
root_data = 1
tree = Tree(root_data)

parent_node = tree.root
child_data_1 = 2
tree.add_child(parent_node, child_data_1)

child_data_2 = 3
tree.add_child(parent_node, child_data_2)

请注意,上述是你提供的代码的 Python 翻译版本。如果你有任何进一步的问题,或者需要关于代码的解释,请随时提问。

英文:

I'm working on a project to create a tree with more than 2 child nodes. I get that when creating a binary tree, we can just create a left node and a right node to act as children, but when I've looked online for help in creating a tree, every solution I've found talks about creating a binary tree. I understand that part of creating a tree means you need to create an array or arraylist of children nodes, but I don't understand how I'm going to put data into that array or how I'm going to 'connect' the array of children nodes to my parent node?

Here's the code I have at the moment. I know it's not a lot, but I'm struggling just starting this project.

class Node
{
    public int data; //data for storage
    public Node[] children;//array will keep children
    public Node parent;//parent to start the tree

    public Node(, int data)//constructor will store data and children(I think?)
    {
    }
}

public class Tree //main class will implement everything in Node
{
}

How and where do I start this process of connecting my children nodes to my parent/root node?

答案1

得分: 2

在列表中存储子节点以避免与数组重建的问题,但您也可以使用数组。您还可以在构造函数中直接初始化数组,或在添加第一个子节点时初始化数组。

public class Main {

    public static void main(String[] args) {
        Node node = new Node(1)
            .addChild(new Node(2)
                    .addChild(new Node(4))
                    .addChild(new Node(5)))
            .addChild(new Node(3));
    }
}

class Node {
    public int data; // 用于存储数据
    public List<Node> children; // 数组将保持子节点
    public Node parent; // 树的起始父节点

    public Node(int data) {
        children = new ArrayList<>();
        this.data = data;
    }

    public Node addChild(Node node) {
        children.add(node);
        node.parent = this;
        return this;
    }
}
英文:

Storing children in list not to have problems with array recreation, but you can use array too.
You may also init array directly in the constructor, or when adding first child.

public class Main {

    public static void main(String[] args) {
        Node node = new Node(1)
            .addChild(new Node(2)
                    .addChild(new Node(4))
                    .addChild(new Node(5)))
            .addChild(new Node(3));
    }
}

class Node {
    public int data; //data for storage
    public List&lt;Node&gt; children;//array will keep children
    public Node parent;//parent to start the tree

    public Node(int data) {
        children = new ArrayList&lt;&gt;();
        this.data = data;
    }

    public Node addChild(Node node) {
        children.add(node);
        node.parent = this;
        return this;
    }
}

huangapple
  • 本文由 发表于 2020年7月25日 02:08:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/63079217.html
匿名

发表评论

匿名网友

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

确定