Need help with binary tree code

In summary, the conversation discusses a problem with a red and black tree and a parent method that is not returning the correct value. The code for the parent method is provided and a possible solution is suggested by Warren.
  • #1
giantimi
2
0
Hello, I am having some trouble with my red and black tree. I made up a parent method which finds the node before the node and then sets an iterator to that node. The code is below:

private RBNode parent(Comparable x, RBNode i) {
RBNode tmp = new RBNode(null,null,null);
if(i==null || x==null)
return null;
if( (i.left!=null && x==i.left.data )|| (i.right!=null && x==i.right.data)){
tmp.setCurrent(i);
System.out.println(tmp.getCurrent().data);
}
else if(x.compareTo(i.data)<0)parent(x,i.left);
else if(x.compareTo(i.data)>0)parent(x,i.right);
return tmp;

}


My problem is that from the way my test program runs for the entire tree, something is wrong...I think the problem is that the parent method is returning something else. I'm not sure what that is or exactly how it is doing that. Any help would be greatly appreciated!
 
Physics news on Phys.org
  • #2
These lines:

else if(x.compareTo(i.data)<0)parent(x,i.left);
else if(x.compareTo(i.data)>0)parent(x,i.right);

don't actually do anything. Perhaps you meant to return the value returned by your recursive calls to parent().

- Warren
 
  • #3
Thanks

Thanks a lot, that helped a ton.
 

1. What is a binary tree?

A binary tree is a data structure that consists of nodes, where each node has at most two child nodes (left and right). The topmost node is called the root and the nodes at the bottom with no children are called leaf nodes. Binary trees are used to store data in a hierarchical manner.

2. How do I implement a binary tree in code?

There are several ways to implement a binary tree in code, but the most common approach is to use a class or struct to represent the nodes, and use pointers or references to connect the nodes. The root node can be stored as a member variable, and the left and right child nodes can be accessed using getter and setter methods.

3. What operations can be performed on a binary tree?

The main operations that can be performed on a binary tree are insertion, deletion, and searching. Other common operations include traversal (pre-order, in-order, post-order), finding the height or depth of the tree, and checking if the tree is balanced.

4. How do I traverse a binary tree?

There are three main ways to traverse a binary tree: pre-order, in-order, and post-order. Pre-order traversal visits the root node first, followed by the left subtree and then the right subtree. In-order traversal visits the left subtree first, then the root node, and then the right subtree. Post-order traversal visits the left subtree, then the right subtree, and finally the root node. Each of these traversal methods can be implemented using recursion or iteration.

5. How do I fix bugs in my binary tree code?

Debugging binary tree code can be tricky, but some common approaches include using a debugger to step through the code and check variable values, printing out the tree at different stages to see how it changes, and using test cases to isolate the issue. It's also important to have a good understanding of how the code is supposed to work and to check for common mistakes like incorrect pointer assignments or off-by-one errors.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
8K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
9K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
18
Views
1K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
3
Views
3K
Back
Top