/*----------------------------------------------------------------------------*/ struct Node *_removeNode(struct Node *cur, TYPE val) { assert(cur); int compareResult = compare(cur->val, val); if(compareResult == 1) // node value is greater than d cur->left = _removeNode(cur->left, val); else if(compareResult == -1) // node value is less than d cur->right = _removeNode(cur->right, val); else // node value is equal to d, so remove the node { if(cur->right) // if there is a right child for cur { /* Replace cur node with the left-most descendant of its right child */ cur->val = _leftMost(cur->right); cur->right = _removeLeftMost(cur->right); } else if(cur->left) // if there is no right-hand child but there is a //left child { /* Replace cur node with its left child */ struct Node *temp = cur->left; cur->val = temp->val; cur->left = temp->left; cur->right = temp->right; free(temp); temp = NULL; }else // cur has no children; it can be freed without detaching a part of the BST { free(cur); cur = NULL; } } return cur; }
/*----------------------------------------------------------------------------*/ struct Node *_removeNode(struct Node *cur, TYPE val) { /*write this*/ if (compare(cur->val, val) == 0) { if ((cur->right) == 0) { struct Node *newNode = cur->left; free(cur); return newNode; } else { cur->val = _leftMost(cur->right); cur->right = _removeLeftMost(cur->right); } } else if (compare(cur->val, val) == 1) { cur->left = _removeNode(cur->left, val); } else if (compare(cur->val, val) == -1) { cur->right = _removeNode(cur->right, val); } return cur; }
/*----------------------------------------------------------------------------*/ TYPE _leftMost(struct Node *cur) { if(cur->left == 0) return cur->val; else return _leftMost(cur->left); }
/*----------------------------------------------------------------------------*/ struct Node *_removeNode(struct Node *cur, TYPE val) { /*write this*/ struct BSTree *tempTree = newBSTree(); tempTree->root = cur; assert(val); assert(cur); assert(containsBSTree(tempTree, val)); if (compare(val, cur->val) == 0) { if (cur->right == 0) { return cur->left; } else { cur->val = _leftMost(cur); cur->right = _removeLeftMost(cur); } } else { if (compare(val, cur->val) == -1) { cur->left = _removeNode(cur->left, val); } else { cur->right = _removeNode(cur->right, val); } } return cur; }
/*----------------------------------------------------------------------------*/ TYPE _leftMost(struct Node *cur) { if ((cur != NULL) && (cur->left != NULL)) return (_leftMost(cur->left)); return cur->val; }
/*----------------------------------------------------------------------------*/ struct Node *_removeNode(struct Node *cur, TYPE val) { /*write this*/ assert(cur != NULL); assert(val != NULL); if (compare(cur->val, val) == 0) { if (cur->right != NULL){ cur->val = _leftMost(cur->right); cur->right = _removeLeftMost(cur->right); } else { return cur->left; } } else if (compare(cur->val, val) < 0) { cur->right = _removeNode(cur->right, val); } else if (compare(cur->val, val) > 0) { cur->left = _removeNode(cur->left, val); } return cur; }
/*----------------------------------------------------------------------------*/ struct Node *_removeNode(struct Node *cur, TYPE val) { if(compare(val, cur->val) == -1) cur->left = _removeNode(cur->left, val); else if(compare(val, cur->val) == 1) cur->right = _removeNode(cur->right, val); else { if (cur->left == 0 && cur->right == 0) return NULL; else if (cur->left == 0) { struct Node *retval = cur->right; free(cur); return retval; } else if (cur->right == 0) { struct Node *retval = cur->left; free(cur); return retval; } else { cur->val = _leftMost(cur->right); cur->right = _removeLeftMost(cur->right); } } return cur; }
/*----------------------------------------------------------------------------*/ struct Node *_removeNode(struct Node *cur, TYPE val) { /*write this*/ printf("- In _removeNode\n"); assert(cur !=NULL); assert(val!=NULL); if(compare( cur->val,val)==0){ if(cur->right==NULL){ struct Node* temp = cur->left; free(cur); return temp; } cur->val = _leftMost(cur->right); cur->right = _removeLeftMost(cur->right); } else if(compare(cur->val,val) ==1){ cur->left = _removeNode(cur->left,val); } else{ cur->right = _removeNode(cur->right, val); } return cur; }
/*----------------------------------------------------------------------------*/ struct Node *_removeNode(struct Node *cur, TYPE val) { /*write this*/ if (compare(val, cur->val) == -1) cur->left = _removeNode(cur->left, val); else if (compare(val, cur->val) == 1) cur->right = _removeNode(cur->right, val); else if (compare(val, cur->val) == 0){ if (cur->right == NULL) { struct Node* temp = cur->left; free(cur); return temp;} if (cur->left == NULL){ struct Node* temp = cur->right; free(cur); return temp;} else{ cur->val = _leftMost(cur->right); cur->right = _removeLeftMost(cur->right); } } return cur; }
/*----------------------------------------------------------------------------*/ TYPE _leftMost(struct Node *cur) { assert(cur != NULL); if(cur->left == NULL) return cur->val; cur = _leftMost(cur->left); }
/*----------------------------------------------------------------------------*/ struct Node *_removeNode(struct Node *cur, TYPE val) { /*write this*/ assert (cur != 0); assert (val != 0); // confirm val is in the tree if (compare(cur->val, val) == 0) { if (cur->right == 0) //BASE CASE: if val is equiv, and there's no right child, free cur { _freeBST(cur); return 0; //returning null bc that's the value of cur } else //val is equiv, but there's right child cur { cur->val = _leftMost(cur->right); cur->right = _removeLeftMost(cur->right); } } else if (compare(cur->val, val) == 1) //RECURSIVE CASE: 1 { cur->left = _removeNode(cur->left, val); } else //RECURSIVE CASE: compare(cur->val, val) == -1 { cur->right = _removeNode(cur->right, val); } return cur; }
TYPE _leftMost(struct Node *cur) { if(cur->left == NULL){ return cur->val; } else{ return _leftMost(cur->left); } }
/* fucntion to test the left_Most_element */ void testLeftMost() { struct BSTree *tree = buildBSTTree(); struct data myData3; struct data myData4; myData3.number = 110; myData3.name = "righty"; myData4.number = 10; myData4.name = "lefty of lefty"; printTestResult(compare(_leftMost(tree->root), &myData4) == 0, "_leftMost", "left most of root"); printTestResult(compare(_leftMost(tree->root->left), &myData4) == 0, "_leftMost", "left most of left of root"); printTestResult(compare(_leftMost(tree->root->left->left), &myData4) == 0, "_leftMost", "left most of left of left of root"); }
/*----------------------------------------------------------------------------*/ TYPE _leftMost(struct Node *cur) { /*write this*/ if(cur->left == NULL){ return cur->val; } else return _leftMost(cur->left); }
/* Function to test the left_Most_element */ void testLeftMost() { struct BSTree *tree = buildBSTTree(); struct data *myData; myData = (struct data*)tree->root->left->left->val; printTestResult( (compare(_leftMost(tree->root)->val, myData) == 0), "_leftMost", "Left most of Root"); printTestResult( (compare(_leftMost(tree->root->left)->val, myData) == 0), "_leftMost", "Left most of Left of Root"); myData = (struct data*)tree->root->left->right->left->val; printTestResult( (compare(_leftMost(tree->root->left->right)->val, myData) == 0), "_leftMost", "Left most of Left of Right of Root"); myData = (struct data*)tree->root->right->right->left->val; printTestResult( (compare(_leftMost(tree->root->right->right)->val, myData) == 0), "_leftMost", "Left most of Right of Right of Root"); printf("Deleting the BSTree...\n"); deleteBSTree(tree); printf("Returning from testLeftMost().\n"); }
/* fucntion to test the left_Most_element */ void testLeftMost() { struct BSTree *tree = buildBSTTree(); struct data myData3, myData4; myData3.number = 110; myData3.name = "righty"; myData4.number = 10; myData4.name = "lefty of lefty"; printTestResult(compare(_leftMost(tree->root), &myData4) == 0, "_leftMost", "left most of root"); printTestResult(compare(_leftMost(tree->root->left), &myData4) == 0, "_leftMost", "left most of left of root"); printTestResult(compare(_leftMost(tree->root->left->left), &myData4) == 0, "_leftMost", "left most of left of left of root"); printTestResult(compare(_leftMost(tree->root->right), &myData3) == 0, "_leftMost", "left most of right of root"); printf("Deleting the BSTree...\n"); deleteBSTree(tree); printf("Returning from testLeftMost().\n"); }
/*----------------------------------------------------------------------------*/ struct Node *_removeLeftMost(struct Node *cur) { assert(cur); struct Node * right; if(cur->val == _leftMost(cur)){ right = cur->right; free((void*)cur); cur = NULL; return right; }else{ cur->left = _removeLeftMost(cur->left); return cur; } }
void testLeftMost() { struct BSTree *tree = buildBSTTree(); struct data myData1; struct data myData2; struct data myData3; struct data myData4; myData1.number = 50; myData1.name = "rooty"; myData2.number = 13; myData2.name = "lefty"; myData3.number = 110; myData3.name = "righty"; myData4.number = 10; myData4.name = "lefty of lefty"; if (compare(_leftMost(tree->root), &myData4) == 0) printf("_leftMost(): PASS left most of root\n"); else printf("_leftMost(): FAIL left most of root\n"); if (compare(_leftMost(tree->root->left), &myData4) == 0) printf("_leftMost(): PASS left most of left of root\n"); else printf("_leftMost(): FAIL left most of left of root\n"); if (compare(_leftMost(tree->root->left->left), &myData4) == 0) printf("_leftMost(): PASS left most of left of left of root\n"); else printf("_leftMost(): FAIL left most of left of left of root\n"); if (compare(_leftMost(tree->root->right), &myData3) == 0) printf("_leftMost(): PASS left most of right of root\n"); else printf("_leftMost(): FAIL left most of right of root\n"); }
/*----------------------------------------------------------------------------*/ struct Node *_removeNode(struct Node *cur, TYPE val) { if(compare(cur->val, val) == 0) { cur->val = _leftMost(cur->right); _removeLeftMost(cur->right); }else if(compare(cur->val, val) == -1) { _removeNode(cur->left, val); }else if(compare(cur->val, val) == 1) { _removeNode(cur->right, val); } return cur; }
/*----------------------------------------------------------------------------*/ struct Node *_removeNode(struct Node *cur, TYPE val){ assert(cur != 0); assert(val != 0 && val != NULL); if(compare(cur->val, val) == 0){ //found it if(cur->right == 0){ struct Node *temp = cur->left; free(cur); return temp; } cur->val = _leftMost(cur->right); cur->right = _removeLeftMost(cur->right); } else if(compare(cur->val, val) > 0){ //val is less than cur->val. Go right. cur->left = _removeNode(cur->left, val); } else { //val is greater than cur->val. Go right. cur->right = _removeNode(cur->right, val); } return cur; }
struct Node* _removeNode(struct Node *cur, TYPE val) { if(compare(val, cur->val) == 0){ if(cur->right == NULL){ return cur->left; } else{ cur->val = _leftMost(cur->right); cur->right = _removeLeftMost(cur->right); } } else if(compare(val, cur->val) == 1){ cur->left = _removeNode(cur->left, val); } else{ cur->right = _removeNode(cur->right, val); } return cur; }
/*----------------------------------------------------------------------------*/ struct Node *_removeNode(struct BSTree tree, struct Node *cur, TYPE val) { if ((cur != NULL) && (val != NULL)) { if (containsBSTree(&tree, val)) { struct Node * tmp = NULL; //Base Case if (compare(val, cur->val) == 0) { //If the value is found //If the node has 2 children if ((cur->left != NULL) && (cur->right != NULL)) { tmp = cur; tmp->val = _leftMost(cur->right); cur->right = _removeLeftMost(cur->right); } //If the node has less than 2 children: else { tmp = cur; if (cur->left == NULL) return cur->right; else if (cur->right == NULL) return cur->left; free(tmp); } } else if (compare(val, cur->val) == -1) cur->left = _removeNode(tree, cur->left, val); else if (compare(val, cur->val) == 1) cur->right = _removeNode(tree, cur->right, val); return cur; } else printf("Error: node with specified not inside BST, cannot remove\n"); } else errorEndProg(); }
/*----------------------------------------------------------------------------*/ struct Node *_removeNode(struct Node *cur, TYPE val) { if(compare(cur->val, val) == 0){ //Base Case if(cur->right == 0) return cur->left; else{ /* Copy lowest child to current */ cur->val = _leftMost(cur->right); /* Set right child to subtree with lowest child removed */ cur->right = _removeLeftMost(cur->right); } } else if(compare(val, cur->val) == -1) cur->left = _removeNode(cur->left, val); //Less than == left else cur->right = _removeNode(cur->right, val); //Greater than == right return cur; }
struct Node *_removeNode(struct Node *cur, TYPE val) { assert(cur != 0); assert(val != 0); if(compare(val, cur->val) == 0) { if(cur->right == 0) return cur->left; else { cur->val = _leftMost(cur->right); cur->right = _removeLeftMost(cur->right); } } else if(compare(val, cur->val) == -1) cur->left = _removeNode(cur->left, val); else cur->right = _removeNode(cur->right, val); return cur; }
/* recursive helper function to remove a node from the tree. this function does not decrease tree->cnt. HINT: You have to use the compare() function to compare values. param: cur the current node val the value to be removed from the tree pre: val is in the tree cur is not null val is not null */ struct Node *_removeNode(struct Node *cur, TYPE val) { /* Assert pre-condtions */ assert(val != NULL && cur != NULL); if(!containsBSTree(cur, val)) { printf("ERROR:\tdata not located in tree, aborting...\n"); return cur; } struct Node *tmp; /* Remove value if it is found */ if(compare(cur->val, val) == 0) { if(cur->right == NULL) { tmp = cur->left; free(cur); printf("node deleted\n"); return tmp; } else { /* When a right node exists below a node to be removed */ tmp = _leftMost(cur->right); cur->val = tmp->val; cur->right = _removeLeftMost(cur->right); } } else { if(compare(cur->val, val) == 1) cur->left = _removeNode(cur->left, val); else cur->right = _removeNode(cur->right, val); } return cur; }
/*----------------------------------------------------------------------------*/ struct Node *_removeNode(struct Node *cur, TYPE val) { assert(cur != NULL && val != NULL); int cmpVal = compare(cur->val, val); //node value and test value are equal if(cmpVal == 0) { if(cur->right == NULL) return cur->left; else { cur->val = _leftMost(cur->right); cur->right = _removeLeftMost(cur->right); } } //node value is greater than test value else if(cmpVal == 1) cur->left = _removeNode(cur->left, val); else cur->right = _removeNode(cur->right, val); return cur; }
struct AVLNode *_removeNode(struct AVLNode *cur, void * val, comparator compare) { struct AVLNode *temp; if((*compare)(val, cur->val) == 0) { if(cur->rght != 0) { cur->val = _leftMost(cur->rght); cur->rght =_removeLeftmost(cur->rght); /* return _balance(cur);*/ /* could remove this since there's a return at the end*/ } else { temp = cur->left; free(cur); return temp; } } else if((*compare)(val, cur->val) < 0) cur->left = _removeNode(cur->left, val, compare); else cur->rght = _removeNode(cur->rght, val, compare); return _balance(cur); }
TYPE leftMost(struct BSTree *tree) { return _leftMost(tree->root); }