// 트리 노드 삭제 void deleteNode(Tree *RBT, Node *node) { Node *successor, *fixupNode; Node *left = node->left; Node *right = node->right; Node *p = node->parent; int nColor = node->color; // child 0, 1 : 왼쪽노드 없는 경우 if (left == RBT->NIL) { fixupNode = right; transPlant(RBT, node, right); } // child 1 : 오른쪽노드 없는 경우 else if (right == RBT->NIL) { fixupNode = left; transPlant(RBT, node, left); } // child 2 : 자식노드 2개인 경우 -> successor를 삭제하는 것으로 가정 else { successor = findSuccessor(RBT, right); nColor = successor->color; fixupNode = successor->right; // fixupNode를 successor의 자리에 이식 transPlant(RBT, successor, fixupNode); successor->right = node->right; successor->right->parent = successor; // successor를 node의 자리에 이식 transPlant(RBT, node, successor); successor->left = node->left; successor->left->parent = successor; successor->color = node->color; } // 삭제 노드의 색이 BLACK인 경우 Violation 발생 if (nColor == BLACK) { treeDelFixUp(RBT, fixupNode); } // 메모리 영역 해방시켜줌 free(node); return; }
//Delete Node void deleteWord(WordBST* bst, Word* z) { Word* y = z; Word *x, *xParent; int yOriginalColor = y->color; if (z->left == NULL) { x = z->right; xParent = z->parent; transPlant(bst, z, z->right); } else if (z->right == NULL) { x = z->left; xParent = z->parent; transPlant(bst, z, z->left); } else { y = findMinWord(z->right); yOriginalColor = y->color; x = y->right; xParent = y->parent; if (y->parent == z) { xParent = y; } else { transPlant(bst, y, y->right); y->right = z->right; y->right->parent = y; } transPlant(bst, z, y); y->left = z->left; y->left->parent = y; y->color = z->color; } if (yOriginalColor == 0) { WorddeleteFixUp(bst, x, xParent); } freeWord(z); }