Summary: in this tutorial, you will learn about red-black tree data structure and how to implement the red-black tree in C.
Introduction to red-black tree data structure
A red-black tree is a special kind of the binary search tree where each tree’s node stores a color, which is either red or black. A red-black tree is a self-balancing binary search tree, in which the insert or remove operation is done intelligently to make sure that the tree is always balanced.
The complexity of any operation in the tree such as search, insert or delete is O(logN) where N is the number of nodes in the red-black tree.
The red-black tree data structure is used to implement associative arrays.
Red-black tree implementation in C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | #include <stdlib.h> #include "fatal.h" typedef int ElementType; #define NegInfinity (-10000) #ifndef _RedBlack_H #define _RedBlack_H struct RedBlackNode; typedef struct RedBlackNode *Position; typedef struct RedBlackNode *RedBlackTree; RedBlackTree MakeEmpty(RedBlackTree T); Position Find(ElementType X, RedBlackTree T); Position FindMin(RedBlackTree T); Position FindMax(RedBlackTree T); RedBlackTree Initialize(void); RedBlackTree Insert(ElementType X, RedBlackTree T); RedBlackTree Remove(ElementType X, RedBlackTree T); ElementType Retrieve(Position P); void PrintTree(RedBlackTree T); #endif /* _RedBlack_H */ |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 | #include "redblack.h" #include <stdio.h> #include "fatal.h" typedef enum ColorType { Red, Black } ColorType; struct RedBlackNode { ElementType Element; RedBlackTree Left; RedBlackTree Right; ColorType Color; }; static Position NullNode = NULL; /* Needs initialization */ /* Initialization procedure */ RedBlackTree Initialize(void) { RedBlackTree T; if (NullNode == NULL) { NullNode = malloc(sizeof ( struct RedBlackNode)); if (NullNode == NULL) FatalError("Out of space!!!"); NullNode->Left = NullNode->Right = NullNode; NullNode->Color = Black; NullNode->Element = 12345; } /* Create the header node */ T = malloc(sizeof ( struct RedBlackNode)); if (T == NULL) FatalError("Out of space!!!"); T->Element = NegInfinity; T->Left = T->Right = NullNode; T->Color = Black; return T; } /* END */ void Output(ElementType Element) { printf("%d\n", Element); } /* Print the tree, watch out for NullNode, */ /* and skip header */ static void DoPrint(RedBlackTree T) { if (T != NullNode) { DoPrint(T->Left); Output(T->Element); DoPrint(T->Right); } } void PrintTree(RedBlackTree T) { DoPrint(T->Right); } /* END */ static RedBlackTree MakeEmptyRec(RedBlackTree T) { if (T != NullNode) { MakeEmptyRec(T->Left); MakeEmptyRec(T->Right); free(T); } return NullNode; } RedBlackTree MakeEmpty(RedBlackTree T) { T->Right = MakeEmptyRec(T->Right); return T; } Position Find(ElementType X, RedBlackTree T) { if (T == NullNode) return NullNode; if (X < T->Element) return Find(X, T->Left); else if (X > T->Element) return Find(X, T->Right); else return T; } Position FindMin(RedBlackTree T) { T = T->Right; while (T->Left != NullNode) T = T->Left; return T; } Position FindMax(RedBlackTree T) { while (T->Right != NullNode) T = T->Right; return T; } /* This function can be called only if K2 has a left child */ /* Perform a rotate between a node (K2) and its left child */ /* Update heights, then return new root */ static Position SingleRotateWithLeft(Position K2) { Position K1; K1 = K2->Left; K2->Left = K1->Right; K1->Right = K2; return K1; /* New root */ } /* This function can be called only if K1 has a right child */ /* Perform a rotate between a node (K1) and its right child */ /* Update heights, then return new root */ static Position SingleRotateWithRight(Position K1) { Position K2; K2 = K1->Right; K1->Right = K2->Left; K2->Left = K1; return K2; /* New root */ } /* Perform a rotation at node X */ /* (whose parent is passed as a parameter) */ /* The child is deduced by examining Item */ static Position Rotate(ElementType Item, Position Parent) { if (Item < Parent->Element) return Parent->Left = Item < Parent->Left->Element ? SingleRotateWithLeft(Parent->Left) : SingleRotateWithRight(Parent->Left); else return Parent->Right = Item < Parent->Right->Element ? SingleRotateWithLeft(Parent->Right) : SingleRotateWithRight(Parent->Right); } static Position X, P, GP, GGP; static void HandleReorient(ElementType Item, RedBlackTree T) { X->Color = Red; /* Do the color flip */ X->Left->Color = Black; X->Right->Color = Black; if (P->Color == Red) /* Have to rotate */ { GP->Color = Red; if ((Item < GP->Element) != (Item < P->Element)) P = Rotate(Item, GP); /* Start double rotate */ X = Rotate(Item, GGP); X->Color = Black; } T->Right->Color = Black; /* Make root black */ } RedBlackTree Insert(ElementType Item, RedBlackTree T) { X = P = GP = T; NullNode->Element = Item; while (X->Element != Item) /* Descend down the tree */ { GGP = GP; GP = P; P = X; if (Item < X->Element) X = X->Left; else X = X->Right; if (X->Left->Color == Red && X->Right->Color == Red) HandleReorient(Item, T); } if (X != NullNode) return NullNode; /* Duplicate */ X = malloc(sizeof ( struct RedBlackNode)); if (X == NULL) FatalError("Out of space!!!"); X->Element = Item; X->Left = X->Right = NullNode; if (Item < P->Element) /* Attach to its parent */ P->Left = X; else P->Right = X; HandleReorient(Item, T); /* Color it red; maybe rotate */ return T; } RedBlackTree Remove(ElementType Item, RedBlackTree T) { printf("Remove is unimplemented\n"); if (Item) return T; return T; } ElementType Retrieve(Position P) { return P->Element; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | #include "redblack.h" #include <stdio.h> #define N 800 main() { RedBlackTree T; Position P; int i; int j = 0; T = Initialize(); T = MakeEmpty(T); for (i = 0; i < N; i++, j = (j + 7) % N) T = Insert(j, T); printf("Inserts are complete\n"); for (i = 0; i < N; i++) if ((P = Find(i, T)) == NULL || Retrieve(P) != i) printf("Error at %d\n", i); printf("Min is %d, Max is %d\n", Retrieve(FindMin(T)), Retrieve(FindMax(T))); return 0; } |