void test_avlRemove_remove_node10_in_tree(void){ Node *root = &node30; int cState = avlAdd(&root,&node10); cState =avlAdd(&root,&node60); TEST_ASSERT_EQUAL_NODE(&node30,&node10,&node60,0); int heightChange; Node *temp = avlRemove(&root,60,&heightChange); TEST_ASSERT_EQUAL_PTR(&node60,temp); TEST_ASSERT_EQUAL_NODE(&node30,&node10,NULL,-1); }
/** 100(-1)60(0) -> 100(-1)60(+1) * 100(-1) -130 100(-2) 60(+1) * / \ ------> / -----> / \ * (0)60 130 (0)60 40 100(-1) * / \ / \ / * 40 80 40 80 80 * **/ void test_avlRemove_remove_node130_and_given_the_bal_factor_of_node60_is_0(void){ Node *root = &node100; int cState = avlAdd(&root,&node60); cState =avlAdd(&root,&node130); cState =avlAdd(&root,&node40); cState =avlAdd(&root,&node80); int heightChange; Node *temp = avlRemove(&root,130,&heightChange); TEST_ASSERT_EQUAL(0,heightChange); TEST_ASSERT_EQUAL_PTR(&node130,temp); TEST_ASSERT_EQUAL_NODE(root,&node40,&node100,1); TEST_ASSERT_EQUAL_NODE(&node100,&node80,NULL,-1); TEST_ASSERT_EQUAL_NODE(&node80,NULL,NULL,0); TEST_ASSERT_EQUAL_NODE(&node40,NULL,NULL,0); }
void test_avlRemove_remove_node80_and_The_BF_of_node60_is_(void){ Node *root = &node100; int cState = avlAdd(&root,&node60); cState =avlAdd(&root,&node130); cState =avlAdd(&root,&node140); cState =avlAdd(&root,&node110); int heightChange; Node *temp = avlRemove(&root,110,&heightChange); TEST_ASSERT_EQUAL(0,heightChange); TEST_ASSERT_EQUAL_PTR(&node110,temp); TEST_ASSERT_EQUAL_NODE(root,&node60,&node130,1); TEST_ASSERT_EQUAL_NODE(&node60,NULL,NULL,0); TEST_ASSERT_EQUAL_NODE(&node130,NULL,&node140,1); }
void* setRemove(SET set,void * key) { void *e; if (SET_Ord(set)) return avlRemove(set->telts,key); for (e = qFirst(set->qelts); e; e = qNext(set->qelts)) { if (set->cmp ? set->cmp(e,key) == 0 : e == key) { qElemRemove(set->qelts,qElemCurr(set->qelts)); return e; } } return NULL; }
/** 30(+1)60(+1) -> 30(0)60(0) * 30(+1) -5 30(+2) 60(0) * / \ ------> / \ -----> / \ * 10 60(+1) 10 60(0) (0)30 70 * / / \ / \ / \ \ * 5 50 70 50 70 10 50 80 * \ \ * 80 80 * * **/ void test_avlRemove_remove_node5_and_given_the_bal_factor_of_node60_is_1_and_given_extra_node80(void){ Node *root = &node30; int cState = avlAdd(&root,&node10); cState =avlAdd(&root,&node60); cState =avlAdd(&root,&node5); cState =avlAdd(&root,&node50); cState =avlAdd(&root,&node70); cState =avlAdd(&root,&node80); int heightChange; Node *temp = avlRemove(&root,5,&heightChange); TEST_ASSERT_EQUAL(1,heightChange); TEST_ASSERT_EQUAL_PTR(&node5,temp); TEST_ASSERT_EQUAL_NODE(root,&node30,&node70,0); TEST_ASSERT_EQUAL_NODE(&node30,&node10,&node50,0); TEST_ASSERT_EQUAL_NODE(&node70,NULL,&node80,1); TEST_ASSERT_EQUAL_NODE(&node80,NULL,NULL,0); TEST_ASSERT_EQUAL_NODE(&node50,NULL,NULL,0); }
void test_avlRemove_remove_node10_and_given_the_bal_factor_of_node40_is_1(void){ Node *root = &node30; int cState = avlAdd(&root,&node20); cState =avlAdd(&root,&node60); cState =avlAdd(&root,&node10); cState =avlAdd(&root,&node40); cState =avlAdd(&root,&node70); cState =avlAdd(&root,&node50); int heightChange; Node *temp = avlRemove(&root,10,&heightChange); TEST_ASSERT_EQUAL(1,heightChange); TEST_ASSERT_EQUAL_PTR(&node10,temp); TEST_ASSERT_EQUAL_NODE(root,&node30,&node60,0); TEST_ASSERT_EQUAL_NODE(root->left,&node20,NULL,-1); TEST_ASSERT_EQUAL_NODE(root->right,&node50,&node70,0); TEST_ASSERT_EQUAL_NODE(&node20,NULL,NULL,0); TEST_ASSERT_EQUAL_NODE(&node50,NULL,NULL,0); TEST_ASSERT_EQUAL_NODE(&node70,NULL,NULL,0); }
/** *Description : To remove a node from the AVL tree * *Inputs : **ptrToRoot -> The pointer to pointer to the root of the current tree * *nodeToRemove -> The node to be removed from the tree * *Output : returnNode -> The node that is removed * */ Node *avlRemove(Node **ptrToRoot, Node *nodeToRemove, int (*compare)(void *, void *)){ Node *returnNode = NULL; Node *tempNode; int tempBalanceLeft, tempBalanceRight, tempBalanceForReplacement; int compareResult; if((*ptrToRoot) !=NULL){ compareResult = compareInt((*ptrToRoot), nodeToRemove); if(compareResult == 0){ returnNode = (*ptrToRoot); tempNode = returnNode; if((*ptrToRoot)->leftChild == NULL){ (*ptrToRoot) = (*ptrToRoot)->rightChild; if((*ptrToRoot) != NULL){ (*ptrToRoot)->leftChild = tempNode->leftChild; (*ptrToRoot)->rightChild = tempNode->rightChild; (*ptrToRoot)->balance = tempNode->balance; (*ptrToRoot)->balance--; } } else{ tempBalanceForReplacement = (*ptrToRoot)->leftChild->balance; (*ptrToRoot) = avlGetReplacer(&(*ptrToRoot)->leftChild); if((*ptrToRoot) != NULL){ (*ptrToRoot)->leftChild = tempNode->leftChild; (*ptrToRoot)->rightChild = tempNode->rightChild; (*ptrToRoot)->balance = tempNode->balance; if((*ptrToRoot)->leftChild == NULL) (*ptrToRoot)->balance++; else if((*ptrToRoot)->leftChild->balance == 0){ if(tempBalanceForReplacement - (*ptrToRoot)->leftChild->balance != 0) (*ptrToRoot)->balance++; else{} } else{} } } } else if(compareResult == 1){ if((*ptrToRoot)->leftChild != NULL) tempBalanceLeft = (*ptrToRoot )->leftChild->balance; returnNode = avlRemove(&(*ptrToRoot)->leftChild, nodeToRemove, compare); if(returnNode == NULL) return returnNode; if((*ptrToRoot)->leftChild == NULL) (*ptrToRoot)->balance++; else if((*ptrToRoot)->leftChild->balance == 0){ if(tempBalanceLeft - (*ptrToRoot)->leftChild->balance != 0) (*ptrToRoot)->balance++; else {} } else{} } else if(compareResult == -1){ if((*ptrToRoot)->rightChild != NULL) tempBalanceRight = (*ptrToRoot)->rightChild->balance; returnNode = avlRemove(&(*ptrToRoot)->rightChild, nodeToRemove, compare); if(returnNode == NULL) return returnNode; if((*ptrToRoot)->rightChild == NULL) (*ptrToRoot)->balance--; else if((*ptrToRoot)->rightChild->balance == 0){ if(tempBalanceRight - (*ptrToRoot)->rightChild->balance != 0) (*ptrToRoot)->balance--; else{} } else{} } } if((*ptrToRoot) !=NULL){ if((*ptrToRoot)->balance == 2 && (*ptrToRoot)->rightChild->balance == 1) (*ptrToRoot) = leftRotate((*ptrToRoot)); else if((*ptrToRoot)->balance == 2 && (*ptrToRoot)->rightChild->balance == 0) (*ptrToRoot) = leftRotate((*ptrToRoot)); else if((*ptrToRoot)->balance == 2 && (*ptrToRoot)->rightChild->balance == -1) (*ptrToRoot) = doubleLeftRotate((*ptrToRoot)); else if((*ptrToRoot)->balance == -2 && (*ptrToRoot)->leftChild->balance == 1) (*ptrToRoot) = doubleRightRotate((*ptrToRoot)); else if((*ptrToRoot)->balance == -2 && (*ptrToRoot)->leftChild->balance == -1) (*ptrToRoot) = rightRotate((*ptrToRoot)); else if((*ptrToRoot)->balance == -2 && (*ptrToRoot)->leftChild->balance == 0) (*ptrToRoot) = rightRotate((*ptrToRoot)); else{} } return returnNode; }