[Bf-codereview] New convenient get_sibling() function in Red-Black tree implementation (issue 5437054)

kokleine at googlemail.com kokleine at googlemail.com
Thu Nov 24 08:15:00 CET 2011


Reviewers: bf-codereview_blender.org,

Description:
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

Please review this at http://codereview.appspot.com/5437054/

Affected files:
   DLRB_tree.c


Index: DLRB_tree.c
===================================================================
--- DLRB_tree.c	(Revision 42091)
+++ DLRB_tree.c	(Arbeitskopie)
@@ -287,20 +287,27 @@
  		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;
  }





More information about the Bf-codereview mailing list