/* fucntion to test that the BST contains the elements that we added to it */ void testContainsBSTree() { struct BSTree *tree = buildBSTTree(); struct data myData1, myData2, myData3, myData4, myData5; 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"; myData5.number = 111; myData5.name = "not in tree"; printTestResult(containsBSTree(tree, &myData1), "containsBSTree", "when test containing 50 as root"); printTestResult(containsBSTree(tree, &myData2), "containsBSTree", "when test containing 13 as left of root"); printTestResult(containsBSTree(tree, &myData3), "containsBSTree", "when test containing 110 as right of root"); printTestResult(containsBSTree(tree, &myData4), "containsBSTree", "when test containing 10 as left of left of root"); //check containsBSTree fucntion when the tree does not contain a node printTestResult(!containsBSTree(tree, &myData5), "containsBSTree", "when test containing 111, which is not in the tree"); }
/* Function to test that the BST contains the elements that we added to it */ void testContainsBSTree() { /* printf("test the test \n");*/ struct BSTree *tree = buildBSTTree(); struct data myData; myData.number = 90; myData.name = "Root"; printTestResult(containsBSTree(tree->root, &myData), "containsBSTree", "When test containing 90 as Root"); myData.number = 10; myData.name = "Root->L"; printTestResult(containsBSTree(tree->root, &myData), "containsBSTree", "When test containing 10 as Root->Left"); myData.number = 5; myData.name = "Root->L->L"; printTestResult(containsBSTree(tree->root, &myData), "containsBSTree", "When test containing 5 as Root->Left->Left"); myData.number = 50; myData.name = "Root->L->R"; printTestResult(containsBSTree(tree->root, &myData), "containsBSTree", "When test containing 50 as Root->Left->Right"); myData.number = 40; myData.name = "Root->L->R->L"; printTestResult(containsBSTree(tree->root, &myData), "containsBSTree", "When test containing 40 as Root->Left->Right->Left"); myData.number = 55; myData.name = "Root->L->R->R"; printTestResult(containsBSTree(tree->root, &myData), "containsBSTree", "When test containing 55 as Root->Left->Right->Right"); myData.number = 100; myData.name = "Root->R"; printTestResult(containsBSTree(tree->root, &myData), "containsBSTree", "When test containing 100 as Root->Right"); myData.number = 110; myData.name = "Root->R->R"; printTestResult(containsBSTree(tree->root, &myData), "containsBSTree", "When test containing 110 as Root->Right->Right"); myData.number = 105; myData.name = "Root->R->R->L"; printTestResult(containsBSTree(tree->root, &myData), "containsBSTree", "When test containing 105 as Root->Right->Right->Left"); /*Check if containsBSTree will properly recognize if a number is not in the tree. */ myData.number = 1337; myData.name = "Root->N->A->N"; printTestResult(!containsBSTree(tree->root, &myData), "containsBSTree", "When testing if 1337 is not in the tree."); printf("Deleting the BSTree...\n"); deleteBSTree(tree); printf("Returning from testContainsBSTree().\n"); }
/* function to remove a value from the binary search tree param: tree the binary search tree val the value to be removed from the tree pre: tree is not null val is not null val is in the tree pose: tree size is reduced by 1 */ void removeBSTree(struct BSTree *tree, TYPE val) { if (containsBSTree(tree, val)) { tree->root = _removeNode(tree->root, val); tree->cnt--; } }
int containsBSTree(struct BSTree *tree, TYPE val) { assert(val != 0); struct BSTree *tmp = newBSTree(); assert(tmp != 0); if(compare(val, tree->root->val) == 0) { clearBSTree(tmp); deleteBSTree(tmp); return 1; } else if(compare(val, tree->root->val) == -1) { tmp->root = tree->root->left; } else if(compare(val, tree->root->val) == 1) { tmp->root = tree->root->right; } if(tmp->root == NULL) { clearBSTree(tmp); deleteBSTree(tmp); return 0; } return containsBSTree(tmp, val); }
/*----------------------------------------------------------------------------*/ 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; }
void testContainsBSTree() { struct BSTree *tree = buildBSTTree(); struct data myData1; struct data myData2; struct data myData3; struct data myData4; struct data myData5; 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"; myData5.number = 111; myData5.name = "not in tree"; if (containsBSTree(tree, &myData1)) printf("containsBSTree(): PASS when test containing 50 as root\n"); else printf("containsBSTree(): FAIL when test containing 50 as root\n"); if (containsBSTree(tree, &myData2)) printf("containsBSTree(): PASS when test containing 13 as left of root\n"); else printf("containsBSTree(): FAIL when test containing 13 as left of root\n"); if (containsBSTree(tree, &myData3)) printf("containsBSTree(): PASS when test containing 110 as right of root\n"); else printf("containsBSTree(): FAIL when test containing 110 as right of root\n"); if (containsBSTree(tree, &myData4)) printf("containsBSTree(): PASS when test containing 10 as left of left of root\n"); else printf("containsBSTree(): FAIL when test containing 10 as left of left of root\n"); if (!containsBSTree(tree, &myData5)) printf("containsBSTree(): PASS when test containing 111, which is not in the tree\n"); else printf("containsBSTree(): FAIL when test containing 111, which shouldn't be in the tree.\n"); }
/*----------------------------------------------------------------------------*/ int containsBSTree(struct BSTree *tree, TYPE val) { struct BSTree * subTree; if ((tree != NULL) && (val != NULL)) { subTree = (struct BSTree *) malloc (sizeof(struct BSTree)); //Base case if (compare(tree->root->val, val) == 0) return 1; //If val is less than the current node's value else if (compare(val, tree->root->val) == -1) { //IF we have not yet reached the end of the tree.. if (tree->root->left != NULL) { subTree->root = tree->root->left; return containsBSTree(subTree, val); } //At this point we have reached the end of the tree and the value has not been found else return 0; } //If val is greater than current node's value else if (compare(val, tree->root->val) == 1) { //IF we have not yet reached the end of the tree.. if (tree->root->right != NULL) { subTree->root = tree->root->right; return containsBSTree(subTree, val); } //At this point we have reached the end of the tree and the value has not been found else return 0; } return 0; } else errorEndProg(); }
/* testContainsBSTree: function to test that the BST contains the elements that we added to it pre: tree is not null param: tree - the tree we are testing post: none */ void testContainsBSTree(struct BSTree *tree) { assert(tree != NULL); printTestResult(containsBSTree(tree, 55), "containsBSTree", "when test containing 55 as root"); printTestResult(containsBSTree(tree, 36), "containsBSTree", "when test containing 36 as left of root"); printTestResult(containsBSTree(tree, 78), "containsBSTree", "when test containing 78 as right of root"); printTestResult(containsBSTree(tree, 20), "containsBSTree", "when test containing 20 as left-left of root"); printTestResult(containsBSTree(tree, 67), "containsBSTree", "when test containing 20 as right-left of root"); printTestResult(containsBSTree(tree, 85), "containsBSTree", "when test containing 20 as right-right of root"); printTestResult(!containsBSTree(tree, 88), "containsBSTree", "when test containing 88, which is not in the tree"); }
/* function to determine if the binary subtree contains an element HINT: You have to use the compare() function to compare values. param cur: the root node of the subtree to be searched val the value to search for in the tree pre: tree is not null val is not null post: none */ int containsBSTree(struct Node *cur, TYPE val) { /* Assert pre-condtion */ assert(val != NULL); /* Branch down looking for the value */ if(cur != NULL) { switch(compare(cur->val, val)) { case 1: return containsBSTree(cur->left, val); break; /* When compare returns a 0, a match has been found */ case 0: return 1; break; case -1: return containsBSTree(cur->right, val); break; } } return 0; }
/*----------------------------------------------------------------------------*/ 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(); }
/* 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; }
int main(int argc, char *argv[]) { struct BSTree *tree = newBSTree(); /*Create value of the type of data that you want to store*/ struct data myData1; struct data myData2; struct data myData3; struct data myData4; struct data myData5; struct data myData6; struct data myData7; struct data myData8; struct data myData9; struct data myData10; struct data myData11; struct data myData12; myData1.number = 5; myData1.name = "rooty"; myData2.number = 1; myData2.name = "lefty"; myData3.number = 10; myData3.name = "righty"; myData4.number = 3; myData4.name = "righty"; myData5.number = 50; myData6.number = 40; myData7.number = 29; myData8.number = 31; myData9.number = 7; myData10.number = 15; myData11.number = 55; myData12.number = 5; /*add the values to BST*/ addBSTree(tree, &myData1); addBSTree(tree, &myData2); addBSTree(tree, &myData3); addBSTree(tree, &myData4); addBSTree(tree, &myData5); addBSTree(tree, &myData6); addBSTree(tree, &myData7); addBSTree(tree, &myData8); addBSTree(tree, &myData9); addBSTree(tree, &myData10); addBSTree(tree, &myData11); addBSTree(tree, &myData12); /*Print the entire tree*/ printTree(tree); printf("\n"); /*(( 1 ( 3 ) ) 5 ( 10 ))*/ printf("Tree contains 1? %s\n", containsBSTree(tree, &myData2) ? "True" : "False"); removeBSTree(tree, &myData2); printTree(tree); printf("\n"); return 1; }