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.

# Thread: help me with this code

1. ## 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;
}
}```  Reply With Quote

3. ## 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 {  Reply With Quote

4. ## Re: help me with this code Originally Posted by Norm 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 ???  Reply With Quote

5. ## 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.  Reply With Quote

6. ## 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
}  Reply With Quote

7. ## 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.  Reply With Quote

8. ## 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  Reply With Quote

9. ## 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?  Reply With Quote

10. ## Re: help me with this code Originally Posted by Norm 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 ...  Reply With Quote

11. ## Re: help me with this code

How does the code do a "search"? Break that down into steps.  Reply With Quote

12. ## Re: help me with this code Originally Posted by Norm 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 --- Originally Posted by Norm 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)));

}```  Reply With Quote

13. ## 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  Reply With Quote