so i have this code, and i want to write a reverse function to reverse the conversion that has been made to the tree, edit the function so that it takes the input from a file as in the format :Node A: Left -> null, Right -> D
Node B: Left -> null, Right -> F
Node C: Left -> B, Right -> G
Node D: Left -> C, Right -> J
Node F: Left -> E, Right -> null
Node G: Left -> null, Right -> null
Node J: Left -> H, Right -> null
, and convert it to generic tree correctly and handling all possible errros, and the output should be as A point to its children B C D and B point to E F , C points to G , D poinits to H I J and j points to K, this is my current code: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 Map<String, List<String>> binaryToGeneralized(Map<String, String[]> binaryTree) {
        Map<String, List<String>> generalizedTree = new HashMap<>();

        if (binaryTree == null || binaryTree.isEmpty()) {
            System.err.println("The binary tree is either null or empty. Cannot convert to generalized tree.");
            return generalizedTree;
        }

        for (Map.Entry<String, String[]> entry : binaryTree.entrySet()) {
            String node = entry.getKey();
            String[] children = entry.getValue();

            if (children == null || children.length != 2) {
                System.err.printf("Node %s has invalid children format. Skipping this node.%n", node);
                continue;
            }

            String leftChild = children[0];
            String rightSibling = children[1];

            generalizedTree.putIfAbsent(node, new ArrayList<>());

            if (leftChild != null && !leftChild.equals("null")) {
                List<String> siblings = new ArrayList<>();
                siblings.add(leftChild);
                String currentSibling = leftChild;
                while (currentSibling != null && !currentSibling.equals("null")) {
                    currentSibling = binaryTree.containsKey(currentSibling)
                            ? (binaryTree.get(currentSibling)[1] != null && !binaryTree.get(currentSibling)[1].equals("null"))
                            ? binaryTree.get(currentSibling)[1]
                            : null
                            : null;
                    if (currentSibling != null) siblings.add(currentSibling);
                }
                generalizedTree.get(node).addAll(siblings);
            }
        }

        return generalizedTree;
    }


    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();
    }
}