there is an index out of bound exception in this code:import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;

class TreeNode {
    char data;
    TreeNode left, right;
    List<TreeNode> children;

    TreeNode(char data) {
        this.data = data;
        this.left = this.right = null;
        this.children = new ArrayList<>();
    }
}
public class Main {
    public static TreeNode genericToBinary(TreeNode root) {
        if (root == null || root.children.size() == 0) {
            return root;
        }

        root.right = genericToBinary(root.children.get(root.children.size() - 1));
        TreeNode curr = root.right;

        for (int i = root.children.size() - 2; i > -1; i--) {
            curr.left = genericToBinary(root.children.get(i));
            curr = curr.left;
        }

        return root;
    }
        public static TreeNode binaryToGeneric(TreeNode root) {
            if (root == null) {
                return null;
            }

            // Create a new generic tree node with the same value as the binary tree node
            TreeNode genericRoot = new TreeNode(root.data);

            // Convert the right child (last child in the generic tree) recursively
            TreeNode lastChild = binaryToGeneric(root.right);
            if (lastChild != null) {
                genericRoot.children.add(lastChild);
            }

            // Convert left children (siblings in the generic tree)
            TreeNode leftChild = binaryToGeneric(root.left);
            while (leftChild != null) {
                // Temporarily store the next left child
                TreeNode nextLeftChild = binaryToGeneric(leftChild.right);

                // Add the current left child to the beginning of the children list
                genericRoot.children.add(0, leftChild);

                // Move to the next left child
                leftChild = nextLeftChild;
            }

            return genericRoot;
        }













    public static void printTree(TreeNode root) {
        if (root == null) {
            return;
        }

        printTree(root.left);
        System.out.print(root.data + " ");
        printTree(root.right);
    }

    public static void printTreeAdvanced(TreeNode root) {
        if (root == null) {
            System.out.println("Tree is empty.");
            return;
        }

        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);

        while (!queue.isEmpty()) {
            int levelSize = queue.size();
            StringBuilder level = new StringBuilder();

            for (int i = 0; i < levelSize; i++) {
                TreeNode node = queue.poll();
                if (node != null) {
                    level.append(node.data).append(" ");
                    queue.add(node.left);
                    queue.add(node.right);
                } else {
                    level.append("null ");
                }
            }
            System.out.println(level.toString().trim());
        }
    }
// alfjlajflkafaljldaakfhdjdklfsjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj
    public static void main(String[] args) {

        TreeNode root = importGeneralizedTree("input.txt");

        TreeNode binaryTree = genericToBinary(root);
        TreeNode genericroot=binaryToGeneric(binaryTree);
        System.out.println(genericroot.children.get(1).data);
    }


    public static TreeNode importGeneralizedTree(String input) {
        Map<Character, Set<Character>> treeMap = new HashMap<>();
        char root = 'a';

        // Step 1: Reading the file and building the tree map
        try (BufferedReader br = new BufferedReader(new FileReader(input))) {
            String line;
            while ((line = br.readLine()) != null) {
                if (line.trim().isEmpty()) continue; // Skip empty lines
                String[] parts = line.trim().split("->");
                char parent = parts[0].charAt(0);
                if (root =='a') {
                    root = parent;
                }
                Set<Character> children = new HashSet<>();
                if (parts.length > 1) {
                    for (char child : parts[1].toCharArray()) {
                        children.add(child);
                    }
                }
                treeMap.put(parent, children);
            }
        } catch (FileNotFoundException e) {
            System.err.println("The file " + input + " was not found");
            return null;
        } catch (IOException e) {
            System.err.println("An error occurred while reading the file " + input + ".");
            return null;
        }

        // Print debug info
        System.out.println("Tree Map:");
        for (Map.Entry<Character, Set<Character>> entry : treeMap.entrySet()) {
            System.out.print(entry.getKey() + " -> ");
            for (char child : entry.getValue()) {
                System.out.print(child + " ");
            }
            System.out.println();
        }

        // Step 2: Creating TreeNode objects based on the tree map
        Map<Character, TreeNode> nodes = new HashMap<>();
        for (Map.Entry<Character, Set<Character>> entry : treeMap.entrySet()) {
            char parent = entry.getKey();
            Set<Character> children = entry.getValue();

            TreeNode parentNode = nodes.computeIfAbsent(parent, TreeNode::new);
            for (char child : children) {
                TreeNode childNode = nodes.computeIfAbsent(child, TreeNode::new);
                parentNode.children.add(childNode);
            }
        }

        return nodes.get(root);
    }



}
, for the input:D->HIJ
H->
I->
J->