/* File: main8c.c * * Demonstrate binary search tree. * This version uses bst2.h and inserts nodes with name and frequency. * * Last revision: 2013-04-18 * Fixed bug that used bst_remove incorrectly. */ #include #include #include "bst2.h" int main() { tnode *tree = NULL ; printf("\n\n### Simple test ###\n") ; printf("name (frequency) [size]\n\n") ; tree = bst_insert(tree, "Stephanie", 149776) ; tree = bst_insert(tree, "Victoria", 117429) ; tree = bst_insert(tree, "Megan", 160337) ; tree = bst_insert(tree, "Nicole", 136071) ; tree = bst_insert(tree, "Hannah", 158724) ; tree = bst_insert(tree, "Jessica", 303027) ; tree = bst_insert(tree, "Lauren", 153557) ; tree = bst_insert(tree, "Alexis", 131164) ; tree = bst_insert(tree, "Elizabeth", 172503) ; tree = bst_insert(tree, "Samantha", 223970) ; tree = bst_insert(tree, "Jennifer", 147982) ; tree = bst_insert(tree, "Brittany", 190804) ; tree = bst_insert(tree, "Rachel", 149004) ; tree = bst_insert(tree, "Emily", 237195) ; tree = bst_insert(tree, "Amanda", 190979) ; tree = bst_insert(tree, "Kayla", 155868) ; tree = bst_insert(tree, "Taylor", 169007) ; tree = bst_insert(tree, "Sarah", 224125) ; tree = bst_insert(tree, "Amber", 115586) ; tree = bst_insert(tree, "Ashley", 301756) ; printf("\n-------------------------------\n") ; bst_walk_depth(tree,0) ; printf("\n-------------------------------\n") ; // Testing bst_find() // tnode *ptr ; int freq ; printf("\n\n### Testing bst_find() ###\n") ; freq = 149004 ; ptr = bst_find(tree, freq) ; if (ptr != NULL) { printf("Found %s (%d)\n", ptr->name, ptr->frequency) ; } else { printf("Did not find name with frequency = %d\n", freq) ; } freq = 173102 ; ptr = bst_find(tree, freq) ; if (ptr != NULL) { printf("Found %s (%d)\n", ptr->name, ptr->frequency) ; } else { printf("Did not find name with frequency = %d\n", freq) ; } // Testing bst_remove() // int found ; printf("\n\n### Testing bst_remove() ###\n") ; freq = 190979 ; tree = bst_remove(tree, freq, &found) ; if (found) { printf("Found name with frequency = %d\n", freq) ; } else { printf("Did not find name with frequency = %d\n", freq) ; } printf("\n-------------------------------\n") ; bst_walk_depth(tree,0) ; printf("\n-------------------------------\n") ; // Testing find_by_name() // and making sure that strings in the BST are writeable. // printf("\n\n### Testing bst_find_by_name() ###\n") ; ptr = bst_find_by_name(tree, "Elizabeth") ; if (ptr != NULL) { ptr->name[3] = 'S' ; } printf("\n-------------------------------\n") ; bst_walk_depth(tree,0) ; printf("\n-------------------------------\n") ; bst_destroy(tree) ; return 0 ; }