Browse Source

Made color checks into defines

Cameron Weinfurt 5 months ago
parent
commit
2293d6aad1
1 changed files with 32 additions and 28 deletions
  1. 32
    28
      tree_alloc.c

+ 32
- 28
tree_alloc.c View File

@@ -166,9 +166,12 @@ void rotate_right(TreeAlloc **root_ptr, TreeAlloc *ta) {
166 166
   tmp->parent = parent;
167 167
 }
168 168
 
169
+#define IS_BLACK_NODE(n) (n == NULL || n->color == COLOR_BLACK)
170
+#define IS_RED_NODE(n) (n != NULL && n->color == COLOR_RED)
171
+
169 172
 void repair_tree_after_insert(TreeAlloc **root_ptr, TreeAlloc *ta) {
170 173
 	TreeAlloc *parent = ta->parent;
171
-	
174
+
172 175
 	if (parent == NULL) {
173 176
 		ta->color = COLOR_BLACK;
174 177
 		return;
@@ -176,31 +179,31 @@ void repair_tree_after_insert(TreeAlloc **root_ptr, TreeAlloc *ta) {
176 179
 	TreeAlloc *grandparent = parent->parent;
177 180
 	TreeAlloc *uncle = get_sibling(parent);
178 181
 
179
-	if (parent->color == COLOR_RED) {
180
-		if (uncle != NULL && uncle->color == COLOR_RED) {
182
+	if (IS_RED_NODE(parent)) {
183
+		if (IS_RED_NODE(uncle)) {
181 184
 			parent->color = COLOR_BLACK;
182 185
 			uncle->color = COLOR_BLACK;
183 186
 			grandparent->color = COLOR_RED;
184 187
 			repair_tree_after_insert(root_ptr, grandparent);
185 188
 		} else {
186
-				if (ta == parent->left && parent == grandparent->left) {
187
-						rotate_left(root_ptr, parent);
188
-						ta = ta->left;
189
-            parent = parent->left;
190
-				} else {
191
-						rotate_right(root_ptr, parent);
192
-						ta = ta->right;	
193
-            parent = parent->right;
194
-				}
195
-
196
-				grandparent = parent->parent;
197
-				if (ta == parent->left) {
198
-						rotate_right(root_ptr, grandparent);
199
-				} else {
200
-						rotate_left(root_ptr, grandparent);
201
-				}
202
-				parent->color = COLOR_BLACK;
203
-				grandparent->color = COLOR_RED;
189
+			if (ta == parent->left && parent == grandparent->left) {
190
+				rotate_left(root_ptr, parent);
191
+				ta = ta->left;
192
+				parent = parent->left;
193
+			} else {
194
+				rotate_right(root_ptr, parent);
195
+				ta = ta->right;	
196
+				parent = parent->right;
197
+			}
198
+
199
+			grandparent = parent->parent;
200
+			if (ta == parent->left) {
201
+				rotate_right(root_ptr, grandparent);
202
+			} else {
203
+				rotate_left(root_ptr, grandparent);
204
+			}
205
+			parent->color = COLOR_BLACK;
206
+			grandparent->color = COLOR_RED;
204 207
 		}
205 208
 	}
206 209
 }
@@ -218,24 +221,25 @@ void replace_node(TreeAlloc **root_ptr, TreeAlloc *node, TreeAlloc *replace) {
218 221
 }
219 222
 
220 223
 void repair_after_remove(TreeAlloc **root_ptr, TreeAlloc *node) {
221
-	if (node->color == COLOR_RED) {
224
+	if (IS_RED_NODE(node)) {
222 225
 		node->color = COLOR_BLACK;
223 226
 	} else {
224 227
 		TreeAlloc *sibling = get_sibling(node);
225
-		if (sibling != NULL && sibling->color == COLOR_RED) {
226
-			if (node->parent->left == node)
228
+		if (IS_RED_NODE(sibling)) {
229
+			if (node->parent->left == node) {
227 230
 				rotate_left(root_ptr, node->parent);
228
-			else
231
+			} else {
229 232
 				rotate_right(root_ptr, node->parent);
233
+			}
230 234
 			node->parent->parent->color = node->parent->color = COLOR_BLACK;
231 235
 		} 
232 236
 
233
-		if (sibling->left->color == COLOR_BLACK && sibling->right->color == COLOR_BLACK) {
237
+		if (IS_BLACK_NODE(sibling->left) && IS_BLACK_NODE(sibling->right)) {
234 238
 			node->color = COLOR_BLACK;
235 239
 			sibling->color = COLOR_RED;
236 240
 			repair_after_remove(root_ptr, node->parent);
237 241
 		} else {
238
-			if (node->parent->left == node && sibling->right->color == COLOR_BLACK) {
242
+			if (node->parent->left == node && IS_BLACK_NODE(sibling)) {
239 243
 				rotate_right(root_ptr, sibling);
240 244
 				sibling = get_sibling(node);
241 245
 				sibling->color = COLOR_RED;
@@ -243,7 +247,7 @@ void repair_after_remove(TreeAlloc **root_ptr, TreeAlloc *node) {
243 247
 
244 248
 				rotate_left(root_ptr, node->parent);
245 249
 				node->color = get_sibling(node->parent)->color = COLOR_BLACK;
246
-			} else if (node->parent->right == node && sibling->left->color == COLOR_BLACK) {
250
+			} else if (node->parent->right == node && IS_BLACK_NODE(sibling)) {
247 251
 				rotate_left(root_ptr, sibling);
248 252
 				sibling = get_sibling(node);
249 253
 				sibling->color = COLOR_RED;

Loading…
Cancel
Save