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 12 of 12

Thread: help me with this code

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default help me with this code

    I have two methods which are
    public int less(int n)
    public int nth (int n)
    I can not solve them can help me please??
    this is my code
    public class BST
    { private BTNode<Integer> root;
     
      public BST()
      { root = null;
      }
     
      public boolean find(Integer i)
      { BTNode<Integer> n = root;
        boolean found = false;
     
        while (n!=null && !found)
        { int comp = i.compareTo(n.data);
          if (comp==0)
            found = true;
          else if (comp<0)
            n = n.left;
          else
            n = n.right;
    	}
     
        return found;
      }
     
      public boolean insert(Integer i)
      { BTNode<Integer> parent = root, child = root;
        boolean goneLeft = false;
     
        while (child!=null && i.compareTo(child.data)!=0)
        { parent = child;
          if (i.compareTo(child.data)<0)
    	  { child = child.left;
    	    goneLeft = true;
    	  }
    	  else
    	  { child = child.right;
    	    goneLeft = false;
          }
    	}
     
        if (child!=null)
          return false;  // number already present
        else
        { BTNode<Integer> leaf = new BTNode<Integer>(i);
          if (parent==null) // tree was empty
            root = leaf;
          else if (goneLeft)
            parent.left = leaf;
          else
            parent.right = leaf;
          return true;
        }
      }
      public int less(int n) // return how many number are less than n appear in tree
      {
    	  if(parent == null)
    	  return 0;
    	  }
      public int nth(int n) // return the element that would be found at location n if the contents of the tree were stored in ascending order in an array; an exception of type NoSuchElementException should be thrown if such an element would not exist
      {
    	  }
    }
     
    class BTNode<T>
    { T data;
      BTNode<T> left, right;
     
      BTNode(T o)
      { data = o; left = right = null;
      }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: help me with this code

    I can not solve them
    What are those two methods supposed to do?

    How can the code be compiled and executed for testing? It needs import statements and a main() method.

    The code needs to be properly formatted. There should not be any code on the same line following a {
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: help me with this code

    Quote Originally Posted by Norm View Post
    What are those two methods supposed to do?

    How can the code be compiled and executed for testing? It needs import statements and a main() method.

    The code needs to be properly formatted. There should not be any code on the same line following a {
    I comment beside each method what it should return ???

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: help me with this code

    How can the code be compiled and executed for testing?

    Both those methods are empty. What are the steps each method must take to do its job?
    public int less(int n) // return how many number are less than n
    That method could walk the tree and count.
    public int nth(int n) // return the element that would be found at location n
    Same as the other one.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Feb 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: help me with this code

    yes I am confuse about how to write these method
    help me please ??
    thanks in advance

    --- Update ---

    i have write in public int less (int n)
    {if (parent == null)
    return 0; // this mean that if the tree is empty return 0
    else
    count the numbers that are less than n
    }

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: help me with this code

    First you need to make a design for what the method should do. What are the simple steps that the method needs to do to solve its problem? When you get the list of steps for the method, then work on writing the code for the method.

    Also you need to have a testing method that will test the new methods by building a BST and then calling the methods.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Feb 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: help me with this code

    i have write in public int less (int n)
    {if (parent == null)
    return 0; // this mean that if the tree is empty return 0
    else
    count the numbers that are less than n
    }

    --- Update ---

    this code is assignment that I have to do it and the touture ask to this code with these method

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: help me with this code

    I have to do it
    What progress have you made in designing the methods? Do you have a list of the steps each should do?
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Feb 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: help me with this code

    Quote Originally Posted by Norm View Post
    What progress have you made in designing the methods? Do you have a list of the steps each should do?
    my steps for first method
    search for numbers that is less than n in tree
    then count these number
    and my steps for the second acutlly i do not understand this method so i can not put steps for it ...

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: help me with this code

    How does the code do a "search"? Break that down into steps.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Feb 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: help me with this code

    Quote Originally Posted by Norm View Post
    How does the code do a "search"? Break that down into steps.
    i have done like this is it correct or not??
    public int less(int n) // return how many number are less than n appear in tree
      {
    	  if(parent == null)
    	  return 0;
    	  else
    	  if( parent.left < n && parent.right < n )
    	  return(1);
    	  else
    	  return(1 + (count(parent.left) + count(parent.right)));
     
    	  }


    --- Update ---

    Quote Originally Posted by Norm View Post
    How does the code do a "search"? Break that down into steps.
    i have done this method is it correct??

    public int less(int n) // return how many number are less than n appear in tree
      {
    	  if(parent == null)
    	  return 0;
    	  else
    	  if( parent.left < n && parent.right < n )
    	  return(1);
    	  else
    	  return(1 + (count(parent.left) + count(parent.right)));
     
    	  }

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: help me with this code

    is it correct
    Did you compile and test it?
    Were there compiler errors? Then it is not correct
    Did it produce the correct answer when executed? Then it could be correct.






    Also posted at http://forums.devshed.com/java-help-...ml#post2855531
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 4
    Last Post: January 24th, 2013, 11:20 AM
  2. Replies: 7
    Last Post: January 24th, 2013, 10:41 AM
  3. Trouble Porting my Java File Reading Code to Android Code
    By Gravity Games in forum Android Development
    Replies: 0
    Last Post: December 6th, 2012, 04:38 PM
  4. Replies: 5
    Last Post: November 14th, 2012, 10:47 AM
  5. Replies: 3
    Last Post: September 3rd, 2012, 11:36 AM