CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jan 2010
    Posts
    3

    Problem with binary tree

    hello guys. Im trying to make a searching binary tree. i have problem with my code. i have t classes
    Code:
    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);
      
    
    }
    }
    Code:
    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.

  2. #2
    Join Date
    Feb 2008
    Posts
    966

    Re: Problem with binary tree

    The error is telling you that you have declared the method size() to not be static. However, you are trying to call it from another method that is static. You cannot do this in Java without calling it in a static way. You wither need to call it like:

    MyClass.size();

    Or you need to make ALL of the methods non-static and start your program normally by creating an instance of the class object from main().

  3. #3
    Join Date
    Jan 2010
    Posts
    3

    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:
    Code:
    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;}
    }
    Code:
    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
    Code:
    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?

  4. #4
    Join Date
    Feb 2008
    Posts
    966

    Re: Problem with binary tree

    Look at your code:
    Code:
    public void deleteMin(BinaryNodeWithSize node){
           if(node.left!=null){
              deleteMin(node.left);
          }else{node.value=0;}
         }
    This sets the value of the last node to 0, it does not delete it nor set it to null. If you want to set it to null, then you need to set the node.left BEFORE the last one equal to null.

    Example

    node1 -> node2 -> nodeLast

    You would set node2.left = null in order to delete the last node.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured