//The action of balancing the tree to the given node, according to each individual circumstance avlnode * Tree::balance(avlnode * temp) { int bal=balance_factor(temp); if (bal>1) { if (balance_factor(temp->left)>0) { temp=LL_rotation(temp); } else { temp=LR_rotation(temp); } } else if (bal<-1) { if (balance_factor(temp->right)>0) { temp=RL_rotation(temp); } else { temp=RR_rotation(temp); } } return temp; }
node* insert(node *root, int data){ if(root && root->data == data) return root; if(root == NULL){ root = (node *)malloc(sizeof(node )); root->data = data; root->h = 1; root->left = NULL; root->right = NULL; return root; } printf("data: %d ", root->data); if(root->data < data){ root->right = insert(root->right, data); } else{ root->left = insert(root->left, data); } // update the height root->h = max(height(root->left), height(root->right)) + 1; //check if the root node is unbalanced // there can be four cases int bf = balance_factor(root); printf("bf is: %d\n", bf); if(bf == 2){ if(balance_factor(root->left) == -1){ //left-right case printf("left-right case..\n"); root->left = rotate_left(root->left); } // left-left case printf("left-left case...\n"); return rotate_right(root); } else if(bf == -2){ if(balance_factor(root->right) == 1){ //the right-left case printf("right-left case...\n"); root->right = rotate_right(root->right); } // right-right case printf("right-right case...\n"); return rotate_left(root); } // compiler would not tell you anything if you forget // to write this return statement return root; }
void balance_tree(BinaryTree** tree){ BinaryTree** child; if(balance_factor(*tree) == 2){ child = &(*tree)->left; if(balance_factor(*child) == -1){ rotate_left(child); } rotate_right(tree); }else if(balance_factor(*tree) == -2){ child = &(*tree)->right; if(balance_factor(*child) == 1){ rotate_right(child); } rotate_left(tree); } }
struct elem * ins(struct elem *const e, const KEY_T key) { int b; if (!e) return new_elem(key); if (e->key == key) return e; if (key < e->key) e->left = ins(e->left, key); else e->right = ins(e->right, key); update(e), b = balance_factor(e); return b > 1 && key < e->left->key ? zig(e) : b > 1 && key > e->left->key ? (e->left = zag(e->left), zig(e)) : b < -1 && key > e->right->key ? zag(e) : b < -1 && key < e->right->key ? (e->right = zig(e->right), zag(e)) : e; }
static void insert_fixup(struct vertex *new_vertex) { while ((new_vertex->p != NULL) && ((new_vertex->height + 1) > new_vertex->p->height)) { update_height(new_vertex->p); new_vertex = new_vertex->p; if (balance_factor(new_vertex) == 2) { if (balance_factor(new_vertex->l) == -1) { struct vertex* l = new_vertex->l; left_rotate(&(l)); } right_rotate(&new_vertex); return; } else if (balance_factor(new_vertex) == -2) { if (balance_factor(new_vertex->r) == 1) { struct vertex* r = new_vertex->r; right_rotate(&(r)); } left_rotate(&new_vertex); return; } } }
struct elem * post_del(struct elem *const e) { int b; if (!e) return NULL; update(e), b = balance_factor(e); return b > 1 ? (balance_factor(e->left) >= 0 ? zig(e) : (e->left = zag(e->left), zig(e))) : b < -1 ? (balance_factor(e->right) <= 0 ? zag(e) : (e->right = zig(e->right), zag(e))) : e; }