1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Need help with binary tree code

  1. Jul 27, 2006 #1
    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!
     
  2. jcsd
  3. Jul 27, 2006 #2

    chroot

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    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
     
  4. Jul 27, 2006 #3
    Thanks

    Thanks a lot, that helped a ton.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: Need help with binary tree code
  1. Binary trees (Replies: 1)

  2. Binary Search tree (Replies: 5)

Loading...