views:

44

answers:

3

In red-black tree, when rotate, you need to know who is the parent of particular node. However, the node only has reference to either right or left child.

I was thinking to give a node instance variable "parent" but just for this reason I don't think it is worth doing so and also it would be too complicated to change parent reference per rotation.

public class Node {
  private left;
  private right;
  private isRed;
  private parent; //I don't think this is good idea
}

So, my solution is to write findParent() method that use search to find parent. I am wondering if there is any other way to find a node's parent?

My solution:

sample tree:

    50
    / \
   25  75

If you want to find parent of Node 25, you pass something like:

Node parent = findParent(Node25.value);

and it returns node50.

protected Node findParent(int val) {
        if(root == null) {
            return null;
        }
        Node current = root;
        Node parent = current;
        while(true) {
            if(current.val == val) { //found
                return parent;
            }
            if(current == null) { //not found
                return null;
            }
            parent = current;
            if(current.val > val) { //go left
                current = current.left;
            }
            else { //go right
                current = current.right; 
            }
        }
    }
+1  A: 

It's definitely better to store the parent than to look it up. Updating parent reference is not that complex.

unbeli
+2  A: 

I was thinking to give a node instance variable "parent" but just for this reason I don't think it is worth doing so

Having your nodes have a parent reference requires one extra pointer/reference per node. Compare this with needing to traverse the tree whenever you need to know the parent for a given node.

This is then a trade-off between

  1. The cost of maintaining an extra reference, and keeping it up to date whenever you modify a node.
  2. The computational cost and complexity of having to traverse the tree to find a parent of a given node

I think that the choice between these two options is somewhat subjective but personally I would choose to simply keep track of the parent references.

As a point of reference for you, java.util.TreeMap is implemented as a Red-Black tree which Entry nodes that contain left, right, and parent references.

matt b
I don't think it's all that subjective. If you're going to go the distance of having an R-B tree at all, it's probably because you're willing to sacrifice some things in the pursuit of faster operations. If you're that crunched for memory, why not just use an array?
Mark Peters
Well, I think there are some algorithms where you can say that a tradeoff of not having to maintain the data structure is worth it for small increases in computational complexity. Personally I don't think this is one of them. I just wanted to avoid giving a broad answer.
matt b
+1  A: 

As you traverse the tree to get to your pivot node you can cache the previous parent or if you need more than one level of "undo" you could cache each traversed node on to a stack.

This cache would be a variable local to your rotation algorithm so it wouldn't require any more space in the tree or expensive additional traversals.

joshperry