0
votes

Exact error: "Error: Main method not found in class Node, please define the main method as: public static void main(String[] args) or a JavaFX application class must extend javafx.application.Application"

class Node{
    int key;
    Node left, right;

    public Node(int item){
        key = item;
        left = right = null;
    }
}

class BinaryTree{
    Node root;

    BinaryTree(){
        root = null;
    }

    void printPostorder(Node node){
        if(node == null)
            return;
        
        printPostorder(node.left);
        printPostorder(node.right);
        System.out.print(node.key + " ");
    }

    void printPostorder(){ printPostorder(root);}

    public static void main(String[] args){
        BinaryTree tree = new BinaryTree();
        tree.root = new Node(1);
        tree.root.left = new Node(2);
        tree.root.right = new Node(3);

        System.out.println("\nPostorder: ");
        tree.printPostorder();
    }
}

But the main function has been defined.

1
"But the main function has been defined." yes, but not in the Node class, in what appears to be a file called Node.java - Federico klez Culloca
You can also run as java BinaryTree - Vijay

1 Answers

2
votes

I suspect you've named the java file as "Node.java" instead of "BinaryTree.java", the code throws error since there isn't any main function in class Node (that you're trying to run) but in class BinaryTree. The problem shall resolve if you rename your file to BinaryTree.