hello guys. Im trying to make a searching binary tree. i have problem with my code. i have t classes
public class BinaryTreeTest { public static void main(String[] args) { BinaryNodeWithSize root = new BinaryNodeWithSize(5); System.out.println("Binary Tree Example"); System.out.println("Building tree with root value " + root.value); BinaryNodeWithSize.insert(root, 1); BinaryNodeWithSize.insert(root, 8); BinaryNodeWithSize.insert(root, 6); BinaryNodeWithSize.insert(root, 3); BinaryNodeWithSize.insert(root, 9); System.out.println("Traversing tree in order"); //BinaryNodeWithSize.printInOrder(root); BinaryNodeWithSize.print(root); Brt.size(root); //System.out.println("Traversing tree front-to-back from location 7"); //BinaryNodeWithSize.printFrontToBack(root, 7); } }public class BinaryNodeWithSize { BinaryNodeWithSize left; BinaryNodeWithSize right; int size; int value; public BinaryNodeWithSize(int value) { this.value = value; } public static void insert(BinaryNodeWithSize node, int value) { if (value < node.value) { if (node.left != null) { insert(node.left, value); } else { System.out.println(" Inserted " + value + " to left of " + node.value); node.left = new BinaryNodeWithSize(value); } } else if (value > node.value) { if (node.right != null) { insert(node.right, value); } else { System.out.println(" Inserted " + value + " to right of " + node.value); node.right = new BinaryNodeWithSize(value); } } } public static void printInOrder(BinaryNodeWithSize node) { if (node != null) { printInOrder(node.left); System.out.println(" Traversed " + node.value); printInOrder(node.right); } } public static void print(BinaryNodeWithSize node) { if (node != null) { System.out.println(node.value); if (node.left != null) { System.out.println("(left child - " + node.left.value + ")"); }if (node.right != null) { System.out.println("(right child - " + node.right.value + ")"); }//Brt.size(node); print(node.left); print(node.right); } } /** * uses in-order traversal when the origin is less than the node's value * * uses reverse-order traversal when the origin is greater than the node's * order */ public static void printFrontToBack(BinaryNodeWithSize node, int camera) { if (node == null) return; if (node.value > camera) { // print in order printFrontToBack(node.left, camera); System.out.println(" Traversed " + node.value); printFrontToBack(node.right, camera); } else if (node.value < camera) { // print reverse order printFrontToBack(node.right, camera); System.out.println(" Traversed " + node.value); printFrontToBack(node.left, camera); } else { // order doesn't matter printFrontToBack(node.left, camera); printFrontToBack(node.right, camera); } } }
its working but i want a method that can give me the depth of a node. i have try to create something like this:
but i get the error cannot make a static reference to a non static method size from type BinaryNodeWithSizepublic class Brt { int s; public int size(BinaryNodeWithSize node) { if (node.left == null && node.right == null) { } else if (node.left != null || node.right != null) { size(node); s+=1; }return s; } }
i have encounter this problem with insert delete etc also and thats which i made them static. can you tell where is the problem with the code? i want to be able to make non static methods and escpecially one with the size of the node.