Binary Tree java help please, with getRight and getLeft subtree.

  • Context: Java 
  • Thread starter Thread starter tonedog12345
  • Start date Start date
  • Tags Tags
    Binary Java Tree
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
1 reply · 6K views
tonedog12345
Messages
3
Reaction score
0
I can't figure out how to get these 2 methods to work. In my assignment description it says


public BinTree getLTree()

Get a reference to the left subtree. If this is not possible to do then throw an IllegalArgumentException.

NOTE

This routine returns a BinTree, not a Tnode. You will have to create a new BinTree and set its root to be a reference to the left subtree.
pre: fill this in...
post: fill this in...

Returns:
fill this in...


my BinTree constructors were correct and this is what they are


public BinTree()
{
root = null;
}


public BinTree( int val )
{
root = new Tnode( val, null, null );
}

this is what I put for the get functions

public BinTree getLTree()
{
if ( isEmpty() )
{
throw new IllegalArgumentException();
}
return new BinTree( root.getLkid().getVal() );
}

any suggestions please?
 
Physics news on Phys.org
1. In future it would help a lot if you ask your question in a more specific way than just to say "it doesn't work". We kind of need to know in what way it fails to work or it is difficult to give advice.

2. One thing that does jump to mind though is that your problem statement says
"You will have to create a new BinTree and set its root to be a reference to the left subtree. "
But if you look at your code:
return new BinTree( root.getLkid().getVal() );
This is not what you do. What you do in that code is create a new BinTree and set its root to be a reference to a new subtree, containing a single node, with that node having the value of the root of the left subtree. This is quite different from what the problem asked for.