์ •๊ธ€/TIL

[TIL] - 2023.05.09

Casteira 2023. 5. 9. 09:26

๐Ÿ“Œ - 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

 

ํ™”๊ฐ€ ๋‚˜ ์žˆ๊ฑฐ๋‚˜ ์ƒ์ฒ˜๋ฐ›์€ ์ƒํƒœ์— ์žˆ์„์ˆ˜๋ก ํŒ๋‹จ์€ ์ž์ œํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค. ๊ฐ์ •์ด ํŒ๋‹จ์„ ํ๋ฆฌ๊ฒŒ ํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

ํƒ€์ธ์˜ ํ–‰๋™๊ณผ ์™ธ๋ถ€ ์‚ฌ๊ฑด์„ ์ •ํ™•ํ•˜๊ฒŒ ์ถ”๋ก ํ•  ์ˆ˜ ์ž‡์–ด์•ผ ์ ์ ˆํ•˜๊ฒŒ ๋ฐ˜์‘ํ•  ์ˆ˜ ์žˆ๋‹ค.