๐ - RBํธ๋ฆฌ Insert
๐ - ํธ๋ฆฌ ์์ฑ
rbtree *new_rbtree(void) {
rbtree *p = (rbtree *)calloc(1, sizeof(rbtree));
// TODO: initialize struct if needed
node_t *nil = (node_t*)calloc(1,sizeof(node_t));
nil -> color = RBTREE_BLACK;
p->nil = p-> root = nil;
return p;
}
๐ - ๋ ธ๋ ์ฝ์
node_t *rbtree_insert(rbtree *t, const key_t key) {
// TODO: implement insert
node_t *new_node = (node_t*)calloc(1,sizeof(node_t)); // ๋์ ํ ๋น์ผ๋ก ์ ๋
ธ๋ ์์ฑ
new_node -> key= key; // ๋ฐ์์จ ํค๋ก ๋
ธ๋ ํค ์ง์
new_node ->color= RBTREE_RED; // ๋นจ๊ฐ์์ผ๋ก ์ด๊ธฐํ
new_node ->left = new_node->right = t->nil; // ์ผ์ชฝ, ์ค๋ฅธ์ชฝ ์ ๋ถ nil๋ก ์ด๊ธฐํ
node_t *current = t-> root; //ํ์ฌ ๋
ธ๋ -> rbํธ๋ฆฌ์ ๋ฃจํธ๋
ธ๋
while(current != t->nil){ // ํ์ฌ ๋
ธ๋๊ฐ nil๋
ธ๋๊ฐ ์๋๋๊น์ง
if(key<current->key){ // ์
๋ ฅ ๋ฐ์ ํค๊ฐ ํ์ฌ ๋
ธ๋์ ํค ๊ฐ๋ณด๋ค ์์ ๊ฒฝ์ฐ
if(current -> left == t -> nil){ // ํ์ฌ๋
ธ๋์ ์ผ์ชฝ ๋
ธ๋๊ฐ nil์ผ ๊ฒฝ์ฐ
current-> left= new_node; // ํ์ฌ ๋
ธ๋์ ์ผ์ชฝ๋
ธ๋์ new_node ๋ฃ๊ธฐ
break; //๋ฉ์ถฐ
}
current = current -> left; // ํ์ฌ ๋
ธ๋ = ์ผ์ชฝ๋
ธ๋
}
else{ // ํค๊ฐ ํ์ฌ๋
ธ๋์ ํค๋ณด๋ค ํฌ๊ฑฐ๋ ๊ฐ์ ๊ฒฝ์ฐ
if(current -> right == t->nil){ //ํ์ฌ๋
ธ๋์ ์ค๋ฅธ์ชฝ์ด nil๋
ธ๋์ผ ๊ฒฝ์ฐ
current -> right = new_node; // ํ์ฌ๋
ธ๋์ ์ค๋ฅธ์ชฝ์ new_node๋ฃ๊ธฐ
break;
}
current = current->right; //ํ์ฌ ๋
ธ๋ = ์ค๋ฅธ์ชฝ ๋
ธ๋
}
}
new_node -> parent = current; // new node์ ๋ถ๋ชจ = ํ์ฌ๋
ธ๋
if(current == t->nil){ // ํ์ฌ๋
ธ๋๊ฐ nil๋
ธ๋ ์ผ๋
t-> root = new_node; // ๋ฃจํธ์ new_node ์ฝ์
}
rb_insert_fix(t,new_node); //๋ถ๊ท์น ํ๋จ
return new_node;
}
๐ - ๋ถ๊ท์น ํ๋จ
void rb_insert_fix(rbtree *t , node_t *node){
node_t *uncle;
while(node->parent->color == RBTREE_RED){
if (node->parent == node->parent->parent->left){
uncle= node->parent->parent->right;
if (uncle->color == RBTREE_RED){
node->parent->color = RBTREE_BLACK;
uncle->color = RBTREE_BLACK;
node->parent->parent->color = RBTREE_RED;
node = node->parent->parent;
rb_insert_fix(t,node);
return;
}
else {
if( node == node->parent->right){
node = node->parent;
left_rotate(t,node);
}
node->parent->color = RBTREE_BLACK;
node->parent->parent->color = RBTREE_RED;
right_rotate(t,node->parent->parent);
}
}else{
uncle= node->parent->parent->left;
if (uncle->color == RBTREE_RED){
node->parent->color = RBTREE_BLACK;
uncle->color = RBTREE_BLACK;
node->parent->parent->color = RBTREE_RED;
node = node->parent->parent;
rb_insert_fix(t,node);
return;
}else {
if( node == node->parent->left){
node = node->parent;
right_rotate(t,node);
}
node->parent->color = RBTREE_BLACK;
node->parent->parent->color = RBTREE_RED;
left_rotate(t,node->parent->parent);
}
}
}
t->root->color =RBTREE_BLACK;
}
๐ - ํธ๋ฆฌ ํ์
void right_rotate(rbtree *t, node_t *node){
node_t* y = node->left;
node->left = y->right;
if ( y->right != t->nil){
y->right->parent = node;
}
y->parent = node->parent;
if( node->parent == t->nil){
t->root = y;
}
else if(node==node->parent->right){
node->parent->right =y;
}else{
node->parent->left = y;
}
y->right = node;
node->parent = y;
}
void left_rotate(rbtree *t, node_t *node){
node_t* y = node->right;
node->right = y->left;
if ( y->left != t->nil){
y->left->parent =node;
}
y->parent = node->parent;
if( node->parent == t->nil){
t->root = y;
}
else if(node==node->parent->left){
node->parent->left =y;
}else{
node->parent->right = y;
}
y->left = node;
node->parent = y;
}
โ๏ธ - ์ฒ์์ ๊ฐ๋ ์ฑ์ ๋์ด๊ธฐ ์ํด ์๋์ ํ์์ผ๋ก ํ์๋๋ฐ ๋ ธ๋๋ฅผ ๋ณ๊ฒฝํด์ฃผ๋ฉด์ ๊ณ์ ์ฃผ์ ๋ฐ๋์ด์ ๊ฐ์ด ์ ๋๋ก ์ถ๋ ฅ์ด ๋์ง ์์์
node_t *parent = node->parent;
node_t *grandParent = parent->parent;
node_t *uncle;
๊ฒฐ๊ตญ ํ๋ํ๋ ํ์ดํ์ ํด์ ํด๊ฒฐํ๋ค.
ใ ์ฐ์ธํจ๊ณผ ์ฌ๋์ด ๊ฐ์ ์ผ๋ก๋ ์ด๋ค ๊ฒ๋ ๋ฐ์๋ค์ด์ง ๋ชปํ ๊ฒ์ด๋ค. ๋๋ ๊ธฐ๊บผ์ด ๋ด ๋ชซ์ ์ธ๊ธ์ ๋ผ ๊ฒ์ด์ผ. ๋ถํ์ด๋ ๋๋ ค์์ ๋ถ๋ฌ์ค๋ ๋ชจ๋ ๊ฒ๋ค์ ์ด๋ฉด์ ์ง๋ถํด์ผ ํ๋ ์ธ๊ธ๊ณผ ๊ฐ์ ๊ฒ์ด์ง. ์น์ ํ๋ ๋ฃจ์ค๋ฆฌ์ฐ์ค์ฌ! ์๋ค ๋ํ ์ด๊ฒ๋ค๋ก๋ถํฐ ์์๋ฅผ ํฌ๋งํด์๋ ์๋๋ค๋ค. ใ
-์ธ๋ค์นด. ๋๋์ ๊ดํ ์ํ. 96.2
์ธ์์๋ ์ฌ๋ ค๊ฐ์ง ํํ์ ์ธ๊ธ์ด ๋ฐ๋ฅธ๋ค. ๋ถ๋ง์ ํ๊ณ ์ธ๊ธ์ ํํผํ๊ณ ์ ์ ๋ฅผ ์ธ ์ ์์ง๋ง ๊ถ๊ทน์ ์ผ๋ก๋ ๋ชจ๋ ํ๋ ์ผ์ด๋ค. ์ง๋ถํด์ผ ํ ๊ฒ์ ์ง๋ถํ๊ณ ์ง์ผ์ผํ ๊ฒ์ ์ง์ผ์ผ ํ๋ค.
ใ ๋ํ์์ ์๋๋ฐฉ์ด ๋ฌด์์ ๋งํ๋์ง ์ฃผ์๋ฅผ ๊ธฐ์ธ์ฌ๋ผ. ๊ทธ ๋ค์ ํ๋์ผ๋ก๋ถํฐ ๋ฌด์์ด ๋ฐ๋ผ ๋์ค๋์ง ์ดํด๋ผ. ํ๋์ ์ดํผ๋ ๊ฒ์ ๊ทธ ๋ชฉํ๊ฐ ๋ฌด์์ธ์ง ์ฐพ๊ธฐ ์ํจ์ด๊ณ ๋ง์ ์ฃผ์๋ฅผ ๊ธฐ์ธ์ด๋ ๊ฒ์ ์๋ฏธํ๋ ๋ฐ๋ฅผ ์ฐพ๊ธฐ ์ํจ์ด๋ค. ใ
-๋ง๋ฅด์ฟ ์ค ์์ฐ๋ ๋ฆฌ์ฐ์ค, ๋ช ์๋ก, 7.4
์์ฐ๋ ๋ฆฌ์ฐ์ค๋ ์ฐ๋ฆฌ์๊ฒ ์์ ์ ์ฌ๊ณ ์ ํ๋์ ๊ด์ฐฐํ๋ผ๊ณ ์๊ตฌํ๋ค. ํจํด์ ์ฐพ์์ผ ์์ธ์ด ์ด๋ค ๊ฒฐ๊ณผ์ ๋ง๋๋์ง ์ ์ ์๊ณ ๋ถ์ ์ ์ธ ํ๋ ํจํด์ ๋ง์ ์ ์๋ค. ์ด๋ฅผ ํตํด ์ฐ๋ฆฌ๋ ๊ถ๊ทน์ ์ผ๋ก ์ถ์ ๊ฐ์ ํ ์ ์๋ค.
ใ ์์ฒ๋ฐ์๋ค๋ ์๊ฐ์ ๋ฒ๋ ค๋ผ. ๊ทธ๋ฌ๋ฉด ์์ฒ๋ ์์ด์ง๋ค. ํผํด์์์ ๋ฒ๋ ค๋ผ. ๊ทธ๋ฌ๋ฉด ํผํด๋ ์ฌ๋ผ์ง๋ค. ใ
-๋ง๋ฅด์ฟ ์ค ์์ฐ๋ ๋ฆฌ์ฐ์ค, ๋ช ์๋ก, 4.7
ํ๊ฐ ๋ ์๊ฑฐ๋ ์์ฒ๋ฐ์ ์ํ์ ์์์๋ก ํ๋จ์ ์์ ํ๋ ๊ฒ์ด ์ข๋ค. ๊ฐ์ ์ด ํ๋จ์ ํ๋ฆฌ๊ฒ ํ๊ธฐ ๋๋ฌธ์ด๋ค.
ํ์ธ์ ํ๋๊ณผ ์ธ๋ถ ์ฌ๊ฑด์ ์ ํํ๊ฒ ์ถ๋ก ํ ์ ์์ด์ผ ์ ์ ํ๊ฒ ๋ฐ์ํ ์ ์๋ค.