Repository: TheAlgorithms/Java
File: src/main/java/com/thealgorithms/datastructures/trees/GenericTree.java (Java)

Code before:

/**
 * A generic tree is a tree which can have as many children as it can be It
 * might be possible that every node present is directly connected to root node.
 *
 * <p>
 * In this code Every function has two copies: one function is helper function
 * which can be called from main and from that function a private function is
 * called which will do the actual work. I have done this, while calling from
 * main one have to give minimum parameters.
 */
public class GenericTree {

    private static class Node {

        int data;
        ArrayList<Node> child = new ArrayList<>();
    }

    private final Node root;
    public GenericTree() { // Constructor
        Scanner scn = new Scanner(System.in);
        root = create_treeG(null, 0, scn);
    }

    private Node create_treeG(Node node, int childIndx, Scanner scn) {
        // display
        if (node == null) {
            System.out.println("Enter root's data");
        } else {
            System.out.println("Enter data of parent of index " + node.data + " " + childIndx);
        }
        // input
        node = new Node();
        node.data = scn.nextInt();
        System.out.println("number of children");
        int number = scn.nextInt();
        for (int i = 0; i < number; i++) {
            Node child = create_treeG(node, i, scn);
            node.child.add(child);
        }
        return node;
    }

    /**
     * Function to display the generic tree
     */
    public void display() { // Helper function
        display_1(root);
    }


Review comment (suggestion):
```suggestion
    private Node create_treeG(Node node, int childIndex, Scanner scanner) {
```

Code after:

/**
 * A generic tree is a tree which can have as many children as it can be It
 * might be possible that every node present is directly connected to root node.
 *
 * <p>
 * In this code Every function has two copies: one function is helper function
 * which can be called from main and from that function a private function is
 * called which will do the actual work. I have done this, while calling from
 * main one have to give minimum parameters.
 */
public class GenericTree {

    private static class Node {

        int data;
        ArrayList<Node> child = new ArrayList<>();
    }

    private final Node root;
    public GenericTree() { // Constructor
        Scanner scn = new Scanner(System.in);
        root = create_treeG(null, 0, scn);
    }

    private Node create_treeG(Node node, int childIndex, Scanner scanner) {
        // display
        if (node == null) {
            System.out.println("Enter root's data");
        } else {
            System.out.println("Enter data of parent of index " + node.data + " " + childIndex);
        }
        // input
        node = new Node();
        node.data = scanner.nextInt();
        System.out.println("number of children");
        int number = scanner.nextInt();
        for (int i = 0; i < number; i++) {
            Node child = create_treeG(node, i, scanner);
            node.child.add(child);
        }
        return node;
    }

    /**
     * Function to display the generic tree
     */
    public void display() { // Helper function
        display_1(root);
    }