I've written a convenient function that returns the sibling of a node in the
red-black tree implementation originally implemented by Joshua Leung.

I want to use this get_sibling() function in the future to implement the missing
removal function of the red-black tree implementation.

For now the get_sibling() function just simplifies the get_uncle() function.

Just like the rest of the red-black tree implementation this diff is based on
Wikipedia: http://en.wikipedia.org/wiki/Red%E2%80%93black_tree#Removal
This commit is contained in:
Konrad Kleine 2011-11-24 14:58:42 +00:00
parent 5acde0d24c
commit 03398cfa88

@ -287,20 +287,28 @@ static DLRBT_Node *get_grandparent (DLRBT_Node *node)
return NULL; return NULL;
} }
/* get the sibling node (e.g. if node is left child of parent, return right child of parent) */
static DLRBT_Node *get_sibling(DLRBT_Node *node)
{
if (node && node->parent) {
if (node == node->parent->left)
return node->parent->right;
else
return node->parent->left;
}
/* sibling not found */
return NULL;
}
/* get the 'uncle' - the sibling of the parent - of the given node */ /* get the 'uncle' - the sibling of the parent - of the given node */
static DLRBT_Node *get_uncle (DLRBT_Node *node) static DLRBT_Node *get_uncle (DLRBT_Node *node)
{ {
DLRBT_Node *gpn= get_grandparent(node); if (node)
/* return the child of the grandparent which isn't the node's parent */
return get_sibling(node->parent);
/* return the child of the grandparent which isn't the node's parent */ /* uncle not found */
if (gpn) {
if (gpn->left == node->parent)
return gpn->right;
else
return gpn->left;
}
/* not found */
return NULL; return NULL;
} }