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

  • Thread starter Thread starter tonedog12345
  • Start date Start date
  • Tags Tags
    Binary Java Tree
Click For Summary
The discussion centers on implementing the method getLTree() for a binary tree assignment. The method is supposed to return a new BinTree that references the left subtree of the current tree's root. However, the current implementation incorrectly creates a new BinTree with only the value of the left child's node instead of referencing the entire left subtree. The suggestion emphasizes the importance of clearly stating how the code fails to work for better assistance. The key takeaway is that the implementation needs to align with the assignment's requirement to return a BinTree that encompasses the entire left subtree, not just its value.
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?
 
Technology 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.
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 13 ·
Replies
13
Views
4K
Replies
3
Views
1K
  • · Replies 4 ·
Replies
4
Views
5K
  • · Replies 28 ·
Replies
28
Views
3K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 7 ·
Replies
7
Views
75K
  • · Replies 4 ·
Replies
4
Views
2K