struct node *removeBST(struct node *node, char *name) { if(node == 0) { return; } if (strcmp(name,node->name)==0) { if(node->left == 0 && node->right == 0) { free(node); return 0; } else if(node->left==0) {//오른쪽 자식만 있는경우 struct node *ret=node->right; free(node); return ret; } else if(node->right==0) {//왼쪽 자식만 있는경우 struct node *ret=node->left; free(node); return ret; } else {//자식이 둘 다 있는 경우 struct node *toReplace = findLeast(node->right); strcpy(node->name,toReplace->name); node->right=removeBST(node->right,toReplace->name); return node; } } else if (strcmp(name,node->name) < 0) { node->left = removeBST(node->left,name); return node; } else { node->right = removeBST(node->right,name); return node; } }
/* Remove data from BST pointed to by rootRef, changing root if necessary. * For simplicity's sake, always choose node's in-order * successor in the two-child case. * Memory for removed node should be freed. * Return 1 if data was present, 0 if not found. */ int removeBST(struct TreeNode** rootRef, int data) { if (*rootRef == NULL) return 0; if (data < (*rootRef)->data) { removeBST(&(*rootRef)->left, data); } else if (data > (*rootRef)->data) { removeBST(&(*rootRef)->right, data); } else if (data == (*rootRef)->data) //delete { if ((*rootRef)->left == NULL && (*rootRef)->right == NULL) { free((*rootRef)); (*rootRef) = NULL; } else if ((*rootRef)->right == NULL) { struct TreeNode *temp = (*rootRef)->left; free(*rootRef); *rootRef = temp; } else if ((*rootRef)->left == NULL) { struct TreeNode *temp = (*rootRef)->right; free(*rootRef); *rootRef = temp; } else { struct TreeNode **successor = getInOrderSuccessor(&(*rootRef)->right); (*rootRef)->data = (*successor)->data; removeBST(successor, (*successor)->data); } return 1; } return 0; }
int main(){ struct BinarySearchTree * BST = (struct BinarySearchTree *) malloc(sizeof(struct BinarySearchTree)); struct BinarySearchTree * BST2 = (struct BinarySearchTree *) malloc(sizeof(struct BinarySearchTree)); struct BinarySearchTree * BST3 = (struct BinarySearchTree *) malloc(sizeof(struct BinarySearchTree)); int i = 0, numElements = 10; /* Initialize the three BST's */ printf("Initializing BST..."); initBST(BST); printf("COMPLETE!\n"); printf("Initializing BST2..."); initBST(BST2); printf("COMPLETE!\n"); printf("Initializing BST3..."); initBST(BST3); printf("COMPLETE!\n\n"); /* Add identical values to three BST's (BST 2 and 3 are for testing equalsBST function) */ printf("Adding 10 unordered elements into BST..."); addBST(BST, 45); addBST(BST, 67); addBST(BST, 22); addBST(BST, 100); addBST(BST, 13); addBST(BST, 11); addBST(BST, 64); addBST(BST, 30); addBST(BST, 12); addBST(BST, 14); printf("COMPLETE!\n"); printf("Adding 10 unordered elements into BST2..."); addBST(BST2, 45); addBST(BST2, 67); addBST(BST2, 22); addBST(BST2, 100); addBST(BST2, 13); addBST(BST2, 11); addBST(BST2, 64); addBST(BST2, 30); addBST(BST2, 12); addBST(BST2, 14); printf("COMPLETE!\n"); printf("Adding 10 unordered elements into BST3..."); addBST(BST3, 45); addBST(BST3, 67); addBST(BST3, 22); addBST(BST3, 100); addBST(BST3, 13); addBST(BST3, 11); addBST(BST3, 64); addBST(BST3, 30); addBST(BST3, 12); addBST(BST3, 14); printf("COMPLETE!\n\n"); /* Test sizing aspect of addBST function */ printf("Confirming equivalency of BST size to number of elements added (10):\n"); if(EQ(numElements, sizeBST(BST))) printf("Comparison confirms successful addBST and sizeBST functions. i.e., SUCCESS!\n\n"); else printf("Error in sizing aspect of addBST! FAIL!!\n\n"); /* Test getFirst and removeFirst functions */ printf("The first element in the BST is: %d\n", getFirst(BST)); printf("Removing the first element in BST..."); removeFirst(BST); numElements--; /* Reduce numElements for next test of sizing aspect (post-removal tests). */ printf("COMPLETE!\n"); printf("The first element in the BST is now: %d\n\n", getFirst(BST)); /* Test containsBST function by searching for known value */ printf("Searching BST for known value \'13\'..."); if(containsBST(BST, 13)) printf("FOUND!\nSearch confirms success of containsBST function. i.e., SUCCESS!\n\n"); else printf("Error in containsBST function! FAIL!!!\n\n"); /* Remove a known value from BST and use that value to pass negative case through containsBST function. This is also a test of the removeBST function. */ printf("Removing known value \'13\' in BST..."); removeBST(BST, 13); numElements--; /* Reduce numElements for next test of sizing aspect (post-removal tests). */ printf("COMPLETE!\n\n"); printf("Searching BST for removed value \'13\'..."); if(containsBST(BST, 13)) printf("FOUND!\nError in removeBST function. FAIL!!!\n\n"); else printf("NOT FOUND!\nremoveBST function successfully removed test value. i.e., SUCCESS!\n\n"); /* Repeat sizing test to determine success of removeBST function */ printf("Confirming removal resulted in proper resizing of BST..."); if(EQ(numElements, sizeBST(BST))) printf("SUCCESS!\n\n"); else printf("Error in BST resizing. FAILED!\n\n"); /* Test outputPriorityQueue function via execution (all values to display to user). NOTE: This is a destructive function and will remove all elements in BST */ printf("The elements in BST, in order, are:\n"); outputPriorityQueue(BST); numElements = 0; /* Reduce numElements to mirror anticipated count in BST */ printf("Output complete.\n\n"); /* Test positive case comparison of equalsBST function NOTE: This is a destructive function and will remove all elements in BST2 and BST3 */ printf("Determining if BST2 == BST 3..."); if(equalsBST(BST2, BST3)) printf("EQUAL! SUCCESS!\n\n"); else printf("NOT EQUAL! FAIL!!!\n\n"); /* Add trivial element to the empty BST and test negative case comparison of equalsBST function. NOTE: This is a destructive function and will remove all elements in BST and BST2 Since there are no more sizing tests, numElements will not be incremented to mirror the size of BST during this routine. */ printf("Adding element to empty BST to differentiate it from empty BST2..."); addBST(BST, 23); printf("COMPLETE!!\n"); printf("Determining if BST == BST 2..."); if(equalsBST(BST, BST2)) printf("EQUAL! FAIL!!!\n\n"); else printf("NOT EQUAL! SUCCESS!\n\n"); /* Free memory allocation of BST, BST2, and BST3 Nodes and their BinarySearchTree memory allocation */ if(BST->root != 0) freeBST(BST); if(BST2->root != 0) freeBST(BST2); if(BST3->root != 0) freeBST(BST3); free(BST); free(BST2); free(BST3); return 0; }
int main(int argc, char** argv) { int i, n; struct TreeNode* bst = NULL; struct TreeNode* tree = makeTestTree(5,1); printf("test tree: "); printTree(tree); printf("tree leaves: "); printLeaves(tree); printf("tree depth = %d\n", maxDepth(tree)); printf("tree balanced = %d\n", isBalanced(tree)); printf("tree isBST = %d\n", isBST(tree)); freeTree(tree); tree = NULL; tree = makeTestTree(6,2); printf("another test tree: "); printTree(tree); printf("tree leaves: "); printLeaves(tree); printf("tree depth = %d\n", maxDepth(tree)); printf("tree balanced = %d\n", isBalanced(tree)); printf("tree isBST = %d\n", isBST(tree)); freeTree(tree); tree = NULL; tree = makeNotBST(); printf("notBST: "); printTree(tree); printf("notBST leaves: "); printLeaves(tree); printf("notBST depth = %d\n", maxDepth(tree)); printf("notBST balanced = %d\n", isBalanced(tree)); printf("notBST isBST = %d\n", isBST(tree)); printf("empty tree: "); printTree(bst); for(i = 0; i < 23; ++i) { n = (i*17+11) % 23; bst = insertBST(bst, n); } printf("filled BST: "); printTree(bst); printf("BST leaves: "); printLeaves(bst); printf("BST depth = %d\n", maxDepth(bst)); printf("BST minimum value = %d\n", minValueBST(bst)); printf("BST balanced = %d\n", isBalanced(bst)); printf("BST isBST = %d\n", isBST(bst)); for(i = -4; i < 25; i+=4) { n = removeBST(&bst, i); if(!n) printf("remove did not find %d\n", i); } printf("BST after removes: "); printTree(bst); printf("BST leaves: "); printLeaves(bst); printf("BST depth = %d\n", maxDepth(bst)); printf("BST minimum value = %d\n", minValueBST(bst)); printf("BST balanced = %d\n", isBalanced(bst)); printf("BST isBST = %d\n", isBST(bst)); freeTree(bst); bst = NULL; freeTree(tree); tree = NULL; return 0; }