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
Click For Summary
SUMMARY

The discussion focuses on implementing the methods getLTree() and getRTree() in a binary tree class in Java. The user incorrectly attempts to create a new BinTree with only the value of the left child instead of referencing the entire left subtree. The correct approach requires creating a new BinTree that sets its root to the left subtree's root, not just its value. The user is advised to clarify their questions for more effective assistance.

PREREQUISITES
  • Understanding of Java programming language
  • Familiarity with binary tree data structures
  • Knowledge of exception handling in Java
  • Experience with object-oriented programming concepts
NEXT STEPS
  • Review Java exception handling best practices
  • Study binary tree traversal techniques
  • Learn how to implement tree node references in Java
  • Explore the Java Collections Framework for alternative data structures
USEFUL FOR

Java developers, computer science students, and anyone implementing binary tree structures in their projects.

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.
 

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
5K
Replies
3
Views
1K
  • · Replies 4 ·
Replies
4
Views
5K
  • · Replies 28 ·
Replies
28
Views
4K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 7 ·
Replies
7
Views
75K