Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- public class Main {
- public static void main(String[] args) {
- Main main = new Main();
- main.start();
- }
- static class Node {
- public Node(String name) {
- this.name = name;
- }
- String name;
- ArrayList<Node> children = new ArrayList<>();
- }
- void start() {
- // Create an example tree of nodes:
- Node root = new Node("Root");
- Node root_child1 = new Node("Root child 1");
- root.children.add(root_child1);
- Node root_child2 = new Node("Root child 2");
- root.children.add(root_child2);
- Node parent = new Node("Parent");
- root.children.add(parent);
- Node parent_child1 = new Node("Parent child1");
- parent.children.add(parent_child1);
- Node parent_child2 = new Node("Parent child2");
- parent.children.add(parent_child2);
- // Print the tree:
- this.printNode(root);
- }
- void printNode(Node node) {
- System.out.println(node.name);
- for (Node child_node : node.children) {
- printNode(child_node);
- }
- }
- }
- Output:
- Root
- Root child 1
- Root child 2
- Parent
- Parent child1
- Parent child2
Advertisement
Add Comment
Please, Sign In to add comment