Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

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

Threaded View

  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.


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