-1

Getting error message: "error: can't find main(String[]) method in class: TreeNode" I have it saved as "MySearchTree.java" It compiles just fine (javac), but as soon I as I hit java it shows this error message. I don't know how to fix it. The program is suppose to implement a binary search tree and have a generic class to store the value of the generic type. And display some information. for this program:

 import java.util.Stack;

// tree node (generic)
// BST Is based comparison so it needs Comparable<T>
class TreeNode<T extends Comparable<T>> {
        T x;
        // left right
        TreeNode<T> left = null, right = null;
        TreeNode<T> parent; // parent node
        boolean leaf = true;
        int height; // height associated with it
        
        public TreeNode(T x) {
                this.x = x;
        }
}

// unbalanced BST (generic)
public class MySearchTree<T extends Comparable<T>> {
        private int parentNodes = 0, leaves = 0; // parent leaf count
        private TreeNode<T> root = null; // root node
        int height = 0; // total height
        
        // add to BST
        public void add(T x) {
                if(root == null) {
                        // first time
                        root = new TreeNode<T>(x);
                        root.height = 1;
                        height = 1;
                } else {
                        // find insertion point
                        TreeNode<T> node = root;
                        while(true) {
                                int c = x.compareTo(node.x);
                                if(c <= 0) {
                                        // put it on left side
                                        if(node.left == null) {
                                                // insert right here
                                                if(node.leaf) {
                                                        parentNodes++; // become a parent node
                                                        leaves--;
                                                }
                                                node.left = new TreeNode<T>(x);
                                                node.left.height = node.height + 1;
                                                height = Math.max(node.left.height, height); // update height
                                                node.leaf = false; // mark as parent
                                                node.left.parent = node;
                                                break;
                                        } else {
                                                // move
                                                node = node.left;
                                        }
                                } else {
                                        // on right side
                                        if(node.right == null) {
                                                // insert right here
                                                if(node.leaf) {
                                                        parentNodes++; // become a parent node
                                                        leaves--;
                                                }
                                                node.right = new TreeNode<T>(x);
                                                node.leaf = false; // mark as parent
                                                node.right.height = node.height + 1;
                                                height = Math.max(node.right.height, height); // update height
                                                node.right.parent = node;
                                                break;
                                        } else {
                                                // move
                                                node = node.right;
                                        }
                                }
                        }
                }
                leaves++;
        }
        
        // finds a key
        public boolean find(T x) {
                TreeNode<T> node = root;
                while(node != null) {
                        int c = x.compareTo(node.x);
                        if(c == 0) {
                                return true;
                        } else if(c < 0) {
                                // on left side
                                node = node.left;
                        } else {
                                // on right side
                                node = node.right;
                        }
                }
                return false;
        }
        
        // pre-computed values
        public int leafCount() {
                return leaves;
        }
        
        public int parentCount() {
                return parentNodes;
        }
        
        public int height() {
                return height;
        }
        
        boolean perfect(TreeNode<T> node) {
                if(node.leaf) {
                        // base case
                        return true;
                } else if(node.left != null && node.right != null) {
                        return perfect(node.left) && perfect(node.right);
                } else {
                        return false; // not perfect
                }
        }
        
        // complete full check
        public boolean isPerfect() {
                // solve recursively
                return perfect(root);
        }
        
        // print all parents
        public void ancestors(T x) {
                TreeNode<T> node = root;
                Stack<T> track = new Stack<>();
                while(node != null) {
                        track.add(node.x);
                        int c = x.compareTo(node.x);
                        if(c == 0) {
                                // found (TODO need to think in case of duplicate values)
                                track.pop();
                                break;
                        } else if(c < 0) {
                                // on left side
                                node = node.left;
                        } else {
                                // on right side
                                node = node.right;
                        }
                }
                System.out.println("Ancestors of " + x);
                while(!track.isEmpty()) {
                        System.out.print(track.pop() + " ");
                }
        }
        
        void inorder(TreeNode<T> node) {
                if(node == null) {
                        return;
                } else {
                        inorder(node.left);
                        System.out.print(node.x + " ");
                        inorder(node.right);
                }
        }
        
        // in order
        public void inOrderPrint() {
                System.out.println("Inorder.");
                inorder(root);
                System.out.println();
        }
        
        void preorder(TreeNode<T> node) {
                if(node == null) {
                        return;
                } else {
                        System.out.print(node.x + " ");
                        preorder(node.left);
                        preorder(node.right);
                }
        }
        
        // in order
        public void preOrderPrint() {
                System.out.println("Preorder.");
                preorder(root);
                System.out.println();
        }
        
        // driver program
        public static void main(String[] args) {
                MySearchTree<Integer> bst = new MySearchTree<Integer>();
                bst.add(50);
                System.out.println("Perfect: " + bst.isPerfect());
                bst.add(20);
                bst.add(80);
                System.out.println("Perfect: " + bst.isPerfect());
                bst.add(90);
                bst.add(75);
                bst.add(100);
                bst.add(120);
                bst.add(70);
                bst.add(85);
                System.out.println("Height: " + bst.height());
                System.out.println("Parents: " + bst.parentCount());
                System.out.println("leaves: " + bst.leafCount());
                System.out.println("Perfect: " + bst.isPerfect());
                System.out.println("Find 100: " + bst.find(100));
                System.out.println("Find 110: " + bst.find(110));
                bst.inOrderPrint();
                bst.preOrderPrint();
                bst.ancestors(100);
        }
}
I AM LUNA
  • 1
  • 1
  • Please provide a [mre], hundreds of lines of code doesn't help finding out what your problem is. Additionally please format your code *correctly*, this means correct indentation, currently it's just a mess – Lino Mar 08 '21 at 21:59
  • Also java tries to find the `main`-method in the first class in a file. Generally you should **never** place multiple classes into the same file in Java, that is discouraged, just place your `TreeNode` class in a `TreeNode.java` file. And then the error should disappear – Lino Mar 08 '21 at 22:01
  • At last please show the command you're using to execute your program. – Lino Mar 08 '21 at 22:06
  • How are you starting? `java MySearchTree` or `java MySearchTree.java`? (second does not run the compiled class from file, but launches in source-file mode, that is, compiles in memory and tries to start first class in that file (`TreeNode`)) –  Mar 08 '21 at 22:14

1 Answers1

0

Design notwithstanding, your file appears to compile and run. I just saved yours as MySearchTree.java in a folder and ran:

javac MySearchTree.java

java MySearchTree

It resulted in:

Perfect: true
Perfect: true
Height: 5
Parents: 5
leaves: 4
Perfect: false
Find 100: true
Find 110: false
Inorder.
20 50 70 75 80 85 90 100 120
Preorder.
50 20 80 75 70 90 85 100 120
Ancestors of 100
90 80 50
Atmas
  • 2,389
  • 5
  • 13
  • should fail with given error if started wrongly with `java MySearchTree.java` –  Mar 08 '21 at 22:19