| 166 | } |
| 167 | |
| 168 | static void rebalanceInsert(Tree *t, Node *n) |
| 169 | { |
| 170 | Node *parent, *gparent, *tmp; |
| 171 | for (Node *m = n; m != nullptr; m = RB_PARENT(m)) |
| 172 | fix(m); |
| 173 | while ((parent = RB_PARENT(n)) != nullptr && |
| 174 | RB_COLOR(parent) == RB_RED) |
| 175 | { |
| 176 | gparent = RB_PARENT(parent); |
| 177 | if (parent == RB_LEFT(gparent)) |
| 178 | { |
| 179 | tmp = RB_RIGHT(gparent); |
| 180 | if (tmp != nullptr && RB_COLOR(tmp) == RB_RED) |
| 181 | { |
| 182 | RB_COLOR(tmp) = RB_BLACK; |
| 183 | RB_COLOR(parent) = RB_BLACK; |
| 184 | RB_COLOR(gparent) = RB_RED; |
| 185 | n = gparent; |
| 186 | continue; |
| 187 | } |
| 188 | if (RB_RIGHT(parent) == n) |
| 189 | { |
| 190 | rotateLeft(t, parent); |
| 191 | tmp = parent; |
| 192 | parent = n; |
| 193 | n = tmp; |
| 194 | } |
| 195 | RB_COLOR(parent) = RB_BLACK; |
| 196 | RB_COLOR(gparent) = RB_RED; |
| 197 | rotateRight(t, gparent); |
| 198 | } |
| 199 | else |
| 200 | { |
| 201 | tmp = RB_LEFT(gparent); |
| 202 | if (tmp != nullptr && RB_COLOR(tmp) == RB_RED) |
| 203 | { |
| 204 | RB_COLOR(tmp) = RB_BLACK; |
| 205 | RB_COLOR(parent) = RB_BLACK; |
| 206 | RB_COLOR(gparent) = RB_RED; |
| 207 | n = gparent; |
| 208 | continue; |
| 209 | } |
| 210 | if (RB_LEFT(parent) == n) |
| 211 | { |
| 212 | rotateRight(t, parent); |
| 213 | tmp = parent; |
| 214 | parent = n; |
| 215 | n = tmp; |
| 216 | } |
| 217 | RB_COLOR(parent) = RB_BLACK; |
| 218 | RB_COLOR(gparent) = RB_RED; |
| 219 | rotateLeft(t, gparent); |
| 220 | } |
| 221 | } |
| 222 | RB_COLOR(t->root) = RB_BLACK; |
| 223 | } |
| 224 | |
| 225 | static void rebalanceRemove(Tree *t, Node *parent, Node *n) |
no test coverage detected