Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

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

  1. Dec 2, 2008 #1
    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?
     
  2. jcsd
  3. Dec 3, 2008 #2
    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.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: Binary Tree java help! please, with getRight and getLeft subtree.
  1. Binary tree (Replies: 5)

Loading...