Browse Source

Added debug functionality

Thomas Johnson 5 months ago
parent
commit
69f01b6544
1 changed files with 10 additions and 0 deletions
  1. 10
    0
      tree_alloc.c

+ 10
- 0
tree_alloc.c View File

@@ -7,14 +7,24 @@
7 7
 #ifdef DEBUG
8 8
 #include <stdio.h>
9 9
 
10
+int debug_tree_black_height(TreeAlloc *node) {
11
+  if (node == NULL) {
12
+    return 1;
13
+  }
14
+  return ((node->color == COLOR_BLACK) ? 1 : 0) + debug_tree_black_height(node->left);
15
+}
16
+
10 17
 void debug_print_tree(int indent, void *p) {
11 18
   TreeAlloc *node = (TreeAlloc*) p;
12 19
   if (node != NULL) {
20
+    int bad = debug_tree_black_height(node->left) != debug_tree_black_height(node->right);
13 21
     debug_print_tree(indent + 1, node->left);
14 22
     for (int ii = 0; ii < indent; ii++) { printf("  "); }
15 23
     if (node->color == COLOR_RED) { printf("\e[31m"); }
24
+    if (bad) { printf("\e[43m"); }
16 25
     printf("%p %lu\n", node, node->size);
17 26
     if (node->color == COLOR_RED) { printf("\e[37m"); }
27
+    if (bad) { printf("\e[40m"); }
18 28
     debug_print_tree(indent + 1, node->right);
19 29
   }
20 30
 }

Loading…
Cancel
Save