int testBinarySearchTree(void) { Tree *head = NULL; Tree *singleNodeTree = malloc(sizeof(Tree)); Tree *test[2]; if(singleNodeTree == NULL) { exit(EXIT_FAILURE); } singleNodeTree->value = 100; singleNodeTree->left = NULL; singleNodeTree->right = NULL; printf("Start Test of Binary Search Tree.\n"); printf("Initial contents of the tree:\n"); print_ascii_tree(head); printf("\nadd() 5 to the tree\n"); head = addToTree(head, 5); print_ascii_tree(head); printf("\nadd() 2 to the tree\n"); head = addToTree(head, 2); print_ascii_tree(head); printf("\nadd() 12 to the tree\n"); head = addToTree(head, 12); print_ascii_tree(head); printf("\nadd() 1 to the tree\n"); head = addToTree(head, 1); print_ascii_tree(head); printf("\nadd() 8 to the tree\n"); head = addToTree(head, 8); print_ascii_tree(head); printf("\nadd() 4 to the tree\n"); head = addToTree(head, 4); print_ascii_tree(head); printf("\nadd() 3 to the tree\n"); head = addToTree(head, 3); print_ascii_tree(head); printf("\nadd() 10 to the tree\n"); head = addToTree(head, 10); print_ascii_tree(head); printf("\nadd() 9 to the tree\n"); head = addToTree(head, 9); print_ascii_tree(head); printf("\nadd() 11 to the tree\n"); head = addToTree(head, 11); print_ascii_tree(head); printf("\nadd() 7 to the tree\n"); head = addToTree(head, 7); print_ascii_tree(head); if( (test[0] = findInTree(head, 3)) == NULL) { printf("\nfind(3) = Value Not Found\n"); } else { printf("\nfind(3) = %d\n", test[0]->value); } if( (test[0] = findInTree(head, 7)) == NULL) { printf("\nfind(7) = Value Not Found\n"); } else { printf("\nfind(7) = %d\n", test[0]->value); } if( (test[0] = findInTree(head, 4)) == NULL) { printf("\nfind(4) = Value Not Found\n"); } else { printf("\nfind(4) = %d\n", test[0]->value); } findParentInTree(head, head, 3, test); if( test[1] == NULL) { printf("\nfindParentOfNode(3) = Value Not Found\n"); } else { printf("\nfindParentOfNode(%d) = %d\n", test[0]->value, test[1]->value); } findParentInTree(head, head, 5, test); if( test[1] == NULL) { printf("\nfindParentOfNode(5) = Value Not Found\n"); } else { printf("\nfindParentOfNode(%d) = %d\n", test[0]->value, test[1]->value); } findParentInTree(head, head, 2, test); if( test[1] == NULL) { printf("\nfindParentOfNode(2) = Value Not Found\n"); } else { printf("\nfindParentOfNode(%d) = %d\n", test[0]->value, test[1]->value); } findParentInTree(head, head, 9, test); if( test[1] == NULL) { printf("\nfindParentOfNode(9) = Value Not Found\n"); } else { printf("\nfindParentOfNode(%d) = %d\n", test[0]->value, test[1]->value); } printf("Depth of the tree = %d\n", getTreeDepth(head, 0)); inOrderTraversal(head); preOrderTraversal(head); postOrderTraversal(head); breadthFirstSearch(head); /* Delete Order: 1, 3, 12, 8, 5 */ printf("\nbefore removing any items\n"); print_ascii_tree(head); printf("\nremove() 1 from the tree\n"); head = removeFromTree(head, 1); print_ascii_tree(head); printf("\nremove() 3 from the tree\n"); head = removeFromTree(head, 3); print_ascii_tree(head); printf("\nremove() 12 from the tree\n"); head = removeFromTree(head, 12); print_ascii_tree(head); printf("\nremove() 8 from the tree\n"); head = removeFromTree(head, 8); print_ascii_tree(head); printf("\nremove() 5 from the tree\n"); head = removeFromTree(head, 5); print_ascii_tree(head); printf("\nremove() 11 from the tree\n"); head = removeFromTree(head, 11); print_ascii_tree(head); printf("Depth of the tree = %d\n", getTreeDepth(head, 0)); inOrderTraversal(head); breadthFirstSearch(head); printf("\nsinglNodeTests start here: \n"); print_ascii_tree(singleNodeTree); inOrderTraversal(singleNodeTree); printf("\n remove() 1 from tree\n"); if(removeFromTree(singleNodeTree, 1) == NULL) { printf("Node Not Found, nothing removed\n"); } printf("Depth of the singleNodeTree = %d\n", getTreeDepth(singleNodeTree, 0)); printf("\n remove() 100 from tree\n"); singleNodeTree = removeFromTree(singleNodeTree, 100); printf("Depth of the singleNodeTree = %d\n", getTreeDepth(singleNodeTree, 0)); return EXIT_SUCCESS; }
int BinarySearchTree::getDepth() { return getTreeDepth(root); }