make the convert function recursive and it start from the root and takes the gtree as input:import java.io.*;
import java.util.*;

public class Main {

    public static Map<String, List<String>> importGeneralizedTree(String input) {
        Map<String, List<String>> tree = new HashMap<>();
        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.replaceAll("\\s+", "").split("->");
                String parent = parts[0];
                List<String> children = new ArrayList<>();
                if (parts.length > 1) {
                    children = Arrays.asList(parts[1].split(","));
                }
                tree.put(parent, children);
            }
        } catch (FileNotFoundException e) {
            System.err.println("The file " + input + " was not found");
        } catch (IOException e) {
            System.err.println("An error occurred while reading the file " + input + ".");
        }
        return tree;
    }

    public static void exportBinaryTree(Map<String, String[]> binaryTree, String filename) {
        try (BufferedWriter bw = new BufferedWriter(new FileWriter(filename))) {
            for (Map.Entry<String, String[]> entry : binaryTree.entrySet()) {
                String node = entry.getKey();
                String[] children = entry.getValue();
                String leftChild = (children[0] != null) ? children[0] : "null";
                String rightChild = (children[1] != null) ? children[1] : "null";
                bw.write("Node " + node + ": Left -> " + leftChild + ", Right -> " + rightChild);
                bw.newLine();
            }
        } catch (IOException e) {
            System.err.println("An error occurred while writing to the file " + filename + ".");
        }
    }

    public static Map<String, String[]> generalizedToBinary(Map<String, List<String>> gTree) {
        Map<String, String[]> binaryTree = new HashMap<>();

        for (String key : gTree.keySet()) {
            binaryTree.putIfAbsent(key, new String[2]);
        }

        gTree.forEach((parent, children) -> {
            if (!children.isEmpty()) {
                String rightChild = children.get(children.size() - 1);
                binaryTree.get(parent)[1] = rightChild;
                binaryTree.putIfAbsent(rightChild, new String[2]);

                for (int i = children.size() - 1; i > 0; i--) {
                    binaryTree.putIfAbsent(children.get(i), new String[2]);
                    binaryTree.get(children.get(i))[0] = children.get(i - 1);
                }
                for (String child : children) {
                    if (gTree.containsKey(child)) {
                        generalizedToBinaryHelper(child, gTree.get(child), binaryTree);
                    }
                }
            }
        });

        return binaryTree;
    }

    private static void generalizedToBinaryHelper(String parent, List<String> children, Map<String, String[]> binaryTree) {
        if (children.isEmpty()) {
            return;
        }

        binaryTree.putIfAbsent(parent, new String[2]);
        String rightChild = children.get(children.size() - 1);
        binaryTree.get(parent)[1] = rightChild;
        binaryTree.putIfAbsent(rightChild, new String[2]);

        for (int i = children.size() - 1; i > 0; i--) {
            binaryTree.putIfAbsent(children.get(i), new String[2]);
            binaryTree.get(children.get(i))[0] = children.get(i - 1);
        }
    }

    public static void main(String[] args) {
        String inputFilename = "input.txt";
        String outputFilename = "output.txt";

        if (args.length > 0) {
            inputFilename = args[0];
        }
        if (args.length > 1) {
            outputFilename = args[1];
        }

        // Import generalized tree from the input file
        Map<String, List<String>> gTree = importGeneralizedTree(inputFilename);

        if (gTree.isEmpty()) {
            System.err.println("The generalized tree is empty or could not be loaded. Exiting.");
            return;
        }

        System.out.println("Generalized Tree: " + gTree);

        // Convert the generalized tree to a binary tree
        Map<String, String[]> bTree = generalizedToBinary(gTree);
        System.out.println("Binary Tree: " + binaryTreeToString(bTree));

        // Export the binary tree to the output file
        exportBinaryTree(bTree, outputFilename);
        System.out.println("Binary Tree has been exported to " + outputFilename);
    }

    private static String binaryTreeToString(Map<String, String[]> binaryTree) {
        StringBuilder sb = new StringBuilder();
        sb.append("{");
        for (Map.Entry<String, String[]> entry : binaryTree.entrySet()) {
            String node = entry.getKey();
            String[] children = entry.getValue();
            String leftChild = (children[0] != null) ? children[0] : "null";
            String rightChild = (children[1] != null) ? children[1] : "null";
            sb.append(node).append("=[").append(leftChild).append(", ").append(rightChild).append("], ");
        }
        if (sb.length() > 1) {
            sb.setLength(sb.length() - 2); // Remove the last comma and space
        }
        sb.append("}");
        return sb.toString();
    }
}