Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Problem with binary tree

  1. #1
    Junior Member
    Join Date
    Jan 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Problem with binary tree

    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:
    public 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;
    	  }
    }
    but i get the error cannot make a static reference to a non static method size from type BinaryNodeWithSize
    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.
    Last edited by Exoskeletor; January 7th, 2010 at 04:50 AM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Problem with binary tree

    You are trying to use the methods of BinaryNodeWithSize in a static context (eg BinaryNodeWithSize.insert()), which is why you are receiving the errors. Rather, you should be doing in the context of the object you created (eg instance methods)

    BinaryNodeWithSize root = new BinaryNodeWithSize(5);
     
    [COLOR="Red"]root[/COLOR].insert(root, 1);

    In this way, the methods should not be static so they may refer to the object specific variables. While it doesn't have to be, the method size() can be placed within the BinaryNodeWithSize as opposed to its own class.

    See Understanding Instance and Class Members (The Java™ Tutorials > Learning the Java Language > Classes and Objects) for more information
    Last edited by copeg; January 7th, 2010 at 03:37 PM.

  3. #3
    Junior Member
    Join Date
    Jan 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with binary tree

    yes you was right,. i have fix this.
    now i want with this code to be able to delete nodes. i have try to do it but i cant set value null. how i can delete a node? for example i want to delete the min and max node. this is what i have done:
    public class BinaryNodeWithSize {
     
    	BinaryNodeWithSize left;
    	BinaryNodeWithSize right;
    	int size;
    	int s;
        int value;
     
        public BinaryNodeWithSize(int value) {
          this.value = value;
        }
        public BinaryNodeWithSize() {
     
        	this.value = 0;
          }
        public int findMin(BinaryNodeWithSize node) {
        	if(node.left!=null){
        		findMin(node.left);
        	}else{s= node.value;}
        	return s;
     
        }
        public int findMax(BinaryNodeWithSize node) {
        	if(node.right!=null){
        		findMax(node.right);
        	}else{s= node.value;}
        	return s;
     
        }
        public void deleteMin(BinaryNodeWithSize node){
        	if(node.left!=null){
        		deleteMin(node.left);
        	}else{node.value=0;}
     
        }
        public void deleteMax(BinaryNodeWithSize node){
        	if(node.right!=null){
        		deleteMax(node.right);
        	}else{node.value=0;}
    }
    public class BinaryTreeTest {
     
      public static void main(String[] args) {
    	  BinaryNodeWithSize root = new BinaryNodeWithSize(5);
        System.out.println("Binary Tree Example");
        System.out.println(new BinaryNodeWithSize(1).size);
     
        root.size=1;
        System.out.println("Building tree with root value " + root.value);
        root.insert(root, 1);
        root.insert(root, 8);
        root.insert(root, 6);
        root.insert(root, 3);
        root.insert(root, 9);
        System.out.println("min is " + root.findMin(root));
        System.out.println("max is " + root.findMax(root));
        System.out.println("Traversing tree in order");
        root.print(root);
        root.deleteMin(root);
        root.deleteMax(root);
        System.out.println("Traversing tree in order");
        root.print(root);
        System.out.println("min is " + root.findMin(root));
        System.out.println("max is " + root.findMax(root));
    and i get this
    Binary Tree Example
    0
    Building tree with root value 5
      Inserted 1 to left of 5
      Inserted 8 to right of 5
      Inserted 6 to left of 8
      Inserted 3 to right of 1
      Inserted 9 to right of 8
    min is 1
    max is 9
    Traversing tree in order
    5 size 6
    (left child - 1) size 2
    (right child - 8) size 3
    1 size 2
    (right child - 3) size 1
    3 size 1
    8 size 3
    (left child - 6) size 1
    (right child - 9) size 1
    6 size 1
    9 size 1
    Traversing tree in order
    5 size 6
    (left child - 0) size 2
    (right child - 8) size 3
    0 size 2
    (right child - 3) size 1
    3 size 1
    8 size 3
    (left child - 6) size 1
    (right child - 0) size 1
    6 size 1
    0 size 1
    min is 0
    max is 0
    what am i doing wrong.. again?

Similar Threads

  1. B+ Tree
    By mikesir87 in forum Java Theory & Questions
    Replies: 0
    Last Post: November 20th, 2009, 10:52 AM
  2. Quick binary tree question.
    By Sinensis in forum Java Theory & Questions
    Replies: 1
    Last Post: November 15th, 2009, 09:28 PM
  3. B+ Tree implementation
    By programmer in forum Java Theory & Questions
    Replies: 2
    Last Post: November 15th, 2009, 05:47 PM
  4. Binary Search Tree
    By Koren3 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: November 12th, 2009, 09:27 AM
  5. Binary Search Tree implementation
    By Ceasar in forum Java Theory & Questions
    Replies: 3
    Last Post: October 9th, 2009, 12:23 AM