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

In summary: You need to think about what you need to do to get a reference to the left subtree. How can you get access to the left subtree? What do you need to do with that left subtree once you have it? Remember, the problem statement says that the routine should return a BinTree, not a Tnode. So how can you use the left subtree to create a new BinTree?In summary, the conversation discusses the issue of getting two methods to work correctly in an assignment involving BinTree constructors and get functions. The problem statement specifies that the getLTree method should return a BinTree object, but the provided code creates a new BinTree with a single node instead of referencing the left subtree. Suggestions are given to
  • #1
tonedog12345
3
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
  • #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.
 
  • #3


I would suggest first checking your BinTree constructor and make sure it is correctly setting the root node. If the root is not set correctly, then your getLTree() method will not work as expected.

Next, I would recommend breaking down your getLTree() method into smaller steps and testing each step individually. For example, you can first try to get the left child node of the root and make sure it is not null. Then, try to retrieve the value of that left child node. If that works, then you can proceed to creating a new BinTree and setting its root to be a reference to the left subtree.

Additionally, make sure to properly handle cases where the left subtree is null. You can either return null or throw an exception in those cases. Also, make sure to clearly define the pre and post conditions for your method.

I would also suggest reaching out to your professor or classmates for further assistance and clarification on the assignment requirements. Good luck!
 

1. What is a binary tree in Java?

A binary tree in Java is a data structure that consists of nodes connected by edges. Each node can have at most two child nodes, known as the left and right subtrees. The left subtree contains nodes with values less than the parent node, while the right subtree contains nodes with values greater than the parent node.

2. How do I create a binary tree in Java?

To create a binary tree in Java, you can define a Node class with variables for the data and the left and right subtrees. Then, you can use recursion to insert new nodes into the tree based on their values. Alternatively, you can use a built-in data structure like the TreeMap class in the Java Collections framework.

3. How do I access the left and right subtrees in a binary tree in Java?

To access the left and right subtrees in a binary tree in Java, you can use the getLeft() and getRight() methods. These methods will return the respective subtree as a new binary tree object, allowing you to perform operations on it.

4. How do I traverse a binary tree in Java?

There are several ways to traverse a binary tree in Java, including pre-order, in-order, and post-order traversal. These methods involve visiting the nodes in a specific order and performing an operation on each node. You can use recursion or iteration to implement these traversal methods.

5. What are the main applications of binary trees in Java?

Binary trees in Java have many applications, including representing hierarchical data structures, implementing sorting and searching algorithms, and building decision trees for artificial intelligence and machine learning. They are also used in file systems and databases for efficient data storage and retrieval.

Similar threads

  • Programming and Computer Science
Replies
3
Views
3K
  • Programming and Computer Science
Replies
4
Views
832
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
13
Views
4K
  • Programming and Computer Science
Replies
3
Views
892
  • Programming and Computer Science
Replies
28
Views
3K
  • Programming and Computer Science
Replies
8
Views
2K
  • Programming and Computer Science
Replies
7
Views
75K
  • Programming and Computer Science
Replies
6
Views
8K
  • Programming and Computer Science
Replies
4
Views
1K
Back
Top