From 03398cfa8846e37fe88c8b992f2b7efb4d7b2a20 Mon Sep 17 00:00:00 2001 From: Konrad Kleine Date: Thu, 24 Nov 2011 14:58:42 +0000 Subject: [PATCH] (See http://codereview.appspot.com/5431064/ for review of patch) 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 --- source/blender/blenlib/intern/DLRB_tree.c | 28 +++++++++++++++-------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/source/blender/blenlib/intern/DLRB_tree.c b/source/blender/blenlib/intern/DLRB_tree.c index 4507d70e339..b7df06bbf24 100644 --- a/source/blender/blenlib/intern/DLRB_tree.c +++ b/source/blender/blenlib/intern/DLRB_tree.c @@ -287,20 +287,28 @@ static DLRBT_Node *get_grandparent (DLRBT_Node *node) 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 */ 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 */ - if (gpn) { - if (gpn->left == node->parent) - return gpn->right; - else - return gpn->left; - } - - /* not found */ + /* uncle not found */ return NULL; }