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