NodeT* insertNodeAVLtree(NodeT* root, int data) { if(root==NULL) return createNodeT(data); else { if(root->data < data) root->right=insertNodeAVLtree(root->right, data); else root->left=insertNodeAVLtree(root->left, data); } root->height=maxx(height(root->left), height(root->right))+1; int aux=balanceFactor(root); if(aux>1) { if(root->left->data > data)//RR rightRotation(&root); else//LR LeftRight(&root); } else if(aux<-1) { if(root->right->data < data)//LL leftRotation(&root); else//RL RightLeft(&root); } //else return root; }
/* * Delete a node from AVL tree * Recursive method */ bool delete_avl_node(avl_pp head, int val) { avl_p node; avl_p tmp; if (!head) { log(ERROR, "Initialize AVL tree first\n"); return FALSE; } node = *head; if (!node) { log(ERROR, "No nodes to delete\n"); return FALSE; } if (val > node->data) { if (!node->right) return FALSE; if (delete_avl_node(&(node->right), val) == FALSE) return FALSE; if (BalanceFactor(node) == 2) { if (BalanceFactor(node->left) >= 0) node = LeftLeft(node); else node = LeftRight(node); } } else if (val < node->data) { if (!node->left) return FALSE; if (delete_avl_node(&(node->left), val) == FALSE) return FALSE; if (BalanceFactor(node) == -2) { if (BalanceFactor(node->right) <= 0) node = RightRight(node); else node = RightLeft(node); } } else { /* Match found */ if (node->right) { /* Delete the inorder successor */ tmp = node->right; while (tmp->left) tmp = tmp->left; node->data = tmp->data; if (delete_avl_node(&(node->right), tmp->data) == FALSE) return FALSE; if (BalanceFactor(node) == 2) { if (BalanceFactor(node->left) >= 0) node = LeftLeft(node); else node = LeftRight(node); } } else { *head = node->left; return TRUE; } } node->height = height(node); *head = node; return TRUE; }
void ModulePlayer::ThrowWall(Looking direction, Collider* c){ p2Point<int> tmp; tmp.x = c->rect.x; tmp.y = c->rect.y; switch (direction) { case upD: switch (RightLeft(tmp)) { case 0: //left //GO UP LEFT position.x = tmp.x - 16; position.y = tmp.y + 28; return; break; case 2: //right //GO UP RIGHT position.x = tmp.x + 16; position.y = tmp.y + 28; return; break; } break; case downD: switch (RightLeft(tmp)) { case 0: //Left //GO DOWN LEFT position.x = tmp.x - 16; position.y = tmp.y + 4; return; break; case 2: //Right //GO DOWN RIGHT position.x = tmp.x + 16; position.y = tmp.y + 4; return; break; } break; case rightD: switch (UpDown(tmp)) { case 0: //Up //RIGHT UP position.x = tmp.x - 12; position.y = tmp.y; return; break; case 2: //Down //RIGHT DOWN position.x = tmp.x - 12; position.y = tmp.y + 32; return; break; } break; case leftD: switch (UpDown(tmp)) { case 0: //Up //left UP position.x = tmp.x + 12; position.y = tmp.y; return; break; case 2: //Down //left DOWN position.x = tmp.x + 12; position.y = tmp.y + 32; break; } break; } position = last_position; }
/* * Rebalance subtree tmp based on balance factor & skew */ bool rebalance(stack_p stack, avl_pp head, avl_p tmp, int data) { nodedata_p p = NULL; int direction; avl_p parent = NULL; bool modified = TRUE; if (BalanceFactor(tmp) == -2) { /* Right subtree longer */ p = pop(stack); if (p) { parent = p->node; direction = p->direction; } if (data >= tmp->right->data) { /* Right-right skewed subtree */ if (p) direction == RIGHT ? (parent->right = RightRight(tmp)) : (parent->left = RightRight(tmp)); else /* If p is NULL, this is the topmost node, update *head */ *head = RightRight(tmp); } else { /* Right-left skewed subtree */ if (p) direction == RIGHT ? (parent->right = RightLeft(tmp)) : (parent->left = RightLeft(tmp)); else *head = RightLeft(tmp); } } else if (BalanceFactor(tmp) == 2) { /* Left subtree longer */ p = pop(stack); if (p) { parent = p->node; direction = p->direction; } /* If p is NULL, this is the topmost node, update *head */ if (data < tmp->left->data) { /* Left-left skewed subtree */ if (p) direction == RIGHT ? (parent->right = LeftLeft(tmp)) : (parent->left = LeftLeft(tmp)); else *head = LeftLeft(tmp); } else { /* Left-right skewed subtree */ if (p) direction == RIGHT ? (parent->right = LeftRight(tmp)) : (parent->left = LeftRight(tmp)); else *head = LeftRight(tmp); } } else modified = FALSE; if (p) free(p); tmp->height = height(tmp); return modified; }