예제 #1
0
//balancing
AvlNode* rebalance(AvlNode **node){
  int height_diff = get_height_diff(*node);
  if( height_diff > 1){
    if(get_height_diff((*node)->left_child) > 0)
      *node = rotate_LL(*node);
    else
      *node = rotate_LR(*node);
  }
  else if(height_diff<-1){
    if(get_height_diff((*node)->right_child) < 0)
      *node = rotate_RR(*node);
    else
      *node = rotate_RL(*node);
  }
  return *node;
}
예제 #2
0
static void update_balance(struct Node** root, struct Node* node) {
    if (node != NULL) {
        int height_diff = get_height_diff(node);
        if (height_diff > 1) {
            bool needs_double_rotation = node->height > 1 && get_height_diff(node->left) < 0;
            if (needs_double_rotation) {
                rotate_left(root, node->left);
                rotate_right(root, node);
            } else {
                rotate_right(root, node);
            }
        } else if (height_diff < -1) {
            bool needs_double_rotation = node->height > 1 && get_height_diff(node->right) > 0;
            if (needs_double_rotation) {
                rotate_right(root, node->right);
                rotate_left(root, node);
            } else {
                rotate_left(root, node);
            }
        }

        update_balance(root, node->parent);
    }
}