NODE *balance_tree (NODE **node) { int height_diff; height_diff = get_balance(*node); printf("밸런스값 %d\n", height_diff); if (height_diff > 1) { if (get_balance((*node)->left) > 0) { *node = rotate_LL(*node); }else { *node = rotate_LR(*node); } } else if (height_diff < -1) { if (get_balance((*node)->right) < 0) { *node = rotate_RR(*node); } else { *node = rotate_LL(*node); } } return *node; }
void do_rotation(ROOT *r, int rotation_type) { if(rotation_type == 1) rotate_LL(r); else if(rotation_type == 2) rotate_RL(r); else if(rotation_type == 3) rotate_LR(r); else if(rotation_type == 4) rotate_RR(r); else printf("invalid rotation type \n"); }
//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; }
NODE *balance_tree(NODE **node){ int height_diff; height_diff = get_balance(*node); printf("밸런스 값 : %d\n", height_diff); if(height_diff > 1){ if(get_balance((*node)->left) > 0) *node = rotate_LL(node); else *node = rotate_LR(node); } else if(height_diff <-1){ if(get_balance((*node)->left) >0) *node = rotate_RR(node); else *node = rotate_RL(node); } return *node; }
NODE* rotate_RL (NODE *parent) { NODE *child = parent->right; parent->right = rotate_LL(child); return rotate_RR(parent); }
NODE* rotate_LR (NODE *parent) { NODE *child = parent->left; parent->left = rotate_RR(child); return rotate_LL(parent); }
int main (int argc, char *argv[]) { int i; //int buff[] = {7, 3, 1, 6, 5, 12}; // for (i=0; i<6; i++) // { // insert_data(buff[i]); // } //print(root); //indent_display (root); //root_return = search (root, 5); // print(root_return); //int rr[10] = {6, 3, 1, 5, 7, 11}; //int rr[6] = {20, 10, 40, 30, 50, 60}; //int ll[5] = {7, 6, 3, 1, 12}; //int ll[6] = {12, 7, 3, 2, 10, 20}; //int lr[6] = {7, 3, 1, 6, 12, 5}; //int lr[6] = {12, 7, 3, 10, 20, 8}; //int rl[7] = {6, 3, 1, 5, 7, 12, 9}; int lr[12] = {8, 3, 2, 1, 5, 4, 6, 7, 9, 10, 11, 12}; int row = -1; int col = 0; //7 //for (i=0; i<7; i++) // 6 //for (i=0; i<6; i++) // 5 //for (i=0; i<5; i++) for (i=0; i<12; i++) { // insert_data(rr[i]); // insert_data(ll[i]); insert_data(lr[i]); // insert_data(rl[i]); } // _display (root, rr, &row, &col); // // RR Rotation // parent 의 좌측을 잡아야 한다 // print(root); // insert_data(12); // print(root); // // NODE *root_return; // root_return = search(root, 6); // root_return->right = rotate_RR(root_return->right); // print(root); // print(root); // // NODE *root_return; // root_return = search(root, 20); // root = rotate_RR(root_return); // print(root); // // // // // // LL Rotation // parent의 좌측을 잡아야한다. // print(root); // NODE *root_return; // root_return = search(root, 7); // root_return->left = rotate_LL(root_return->left); // print(root); // print(root); // NODE *root_return; // root_return = search(root, 12); // root = rotate_LL(root_return); // print(root); // // // // // LR Rotation // int lr[6] = {7, 3, 1, 6, 12, 5}; // print(root); // NODE *root_return; // root_return = search(root, 7); // root = rotate_LR(root_return); // print(root); // // LR Rotation //int lr[6] = {12, 7, 3, 10, 20, 8}; // print(root); // NODE *root_return; // root_return = search(root, 12); // root = rotate_LR(root_return); // print(root); // print(root); NODE *root_return; root_return = search(root, 8); root = rotate_LR(root_return); root_return = search(root, 5); printf("--------------------------"); print(root); printf("--------------------------"); root_return = search(root, 5); root_return->right = rotate_RR(root_return->right); indent_display (root); //print(root); // // RL Rotation //int rl[7] = {6, 3, 1, 5, 7, 12, 9}; // print(root); // NODE *root_return; // root_return = search(root, 6); // root_return->right = rotate_RL(root_return->right); // print(root); return 0; }
AvlNode* rotate_LR(AvlNode *parent){ AvlNode *child = parent->left_child; parent->left_child = rotate_RR(child); return rotate_LL(parent); }
AvlNode* rotate_RL(AvlNode *parent){ AvlNode *child = parent->right_child; parent->right_child = rotate_LL(child); return rotate_RR(parent); }