/* This function returns the height of a BST * Parameters: * root - the pointer to the root node of a bst * Returns: * the height of the bst. An empty bst (root is NULL) has a height of 0. */ int getBstHeight(Node* root) { int leftHeight = 0; int rightHeight = 0; if(root == NULL) { return 0; } else { leftHeight = getBstHeight(root->left); rightHeight = getBstHeight(root->right); if( leftHeight > rightHeight || leftHeight == rightHeight) { return (leftHeight + 1); } else { return (rightHeight + 1); } } }
// Main function. int main(int argc, char** argv) { if(argc != 2) { puts("Invalid number of arguments!"); return -1; } else { FILE *fptr = NULL; if((fptr = fopen(argv[1],"r")) != NULL) { char buffer[BUFSIZ]; Tree* root = NULL; // Read data into a new binary tree and print it and it's height while(fgets(buffer,BUFSIZ,fptr) != NULL) { int data = atoi(buffer); root = insertToTree(root,data); } fclose(fptr); printf("Original tree has height %d:\n\n",getBstHeight(root)); printTree(root); // Search tree for key printf("Enter key to search for: "); fgets(buffer,BUFSIZ,stdin); int searchKey = atoi(buffer); int result = searchTree(root,searchKey); if(result == 1) puts("\nTarget found in tree"); else puts("\nTarget not found in tree"); // Flatten tree to list puts("Flattened list:\n\n"); List* newList = flattenTreeToList(root); printList(newList); freeList(newList); // Reflect tree, print it, and free it puts("Reflected Tree:\n\n"); reflectTree(root); printTree(root); freeTree(root); } else { puts("Unable to open file!"); return -1; } } return 0; }
int main(int argc,char* argv[]) { if(argc==1 || argc%2==0) { return -1; } int i=0; struct data* myDta = NULL; struct tree* myT = createTree(); for(i=1;i<argc;i+=2) { assert(i+1<argc); myDta = createData(atof(argv[i]),atof(argv[i+1])); insertBst(myT,myDta); } printf("\ninOrderBst()\n"); inOrderBst(myT); printf("\npreOrderBst()\n"); preOrderBst(myT); printf("\npostOrderBst()\n"); postOrderBst(myT); printf("\ntotalSum()\n"); printf("%f\n",totalSum(myT)); printf("\ngetMaxData()\n"); printData(getMaxData(myT)); printf("\ngetMinData()\n"); printData(getMinData(myT)); printf("\ngetBstHeight()\n"); printf("%d\n",getBstHeight(myT)); printf("\nprintInRange() (range 6.0-14.0)\n"); printInRange(myT,6.0,14.0); printf("\nisCompleteBst()\n"); printf("%d\n",isCompleteBst(myT)); printf("\nisFullBst()\n"); printf("%d\n",isFullBst(myT)); printf("\nsumLeave()\n"); printf("%f\n",sumLeave(myT)); printf("\nprintDepthFirstSearch()\n"); printDepthFirstSearch(myT); printf("\nprintBreadthFirstSearch()\n"); printBreadthFirstSearch(myT); printf("\nreverseBST()\n"); reverseBST(myT); printf("\ninOrderBst()\n"); inOrderBst(myT); cleanBST(myT); return 0; }
int main(int argc, char **argv) { FILE *in = NULL; int num_read, array_size = 0; if(argc != 2){ printf("hw4 <input-file>\n"); return 1; } in = fopen(argv[1], "r"); if(in == NULL){ printf("File can not be opened.\n"); return 2; } // declare the array int array[MAX_SIZE]; // read from the second line to get each element of the array while(!feof(in)){ fscanf(in, "%d", &num_read); if(isValidID(array, array_size, num_read) == 0) { array[array_size] = num_read; array_size++; printf("%d added to array\n", num_read); } else if(isValidID(array, array_size, num_read) == 1) { printf("ERROR - %d bad ID. Not in range [0, 99].\n", num_read); } else if(isValidID(array, array_size, num_read) == 2) { printf("ERROR - %d bad ID. Duplicate.\n", num_read); } } fclose(in); Node *root1 = NULL, *root2 = NULL, *root3 = NULL; int i; // task1: construct a bst from the unsorted array printf("=== task1: construct a bst from the unsorted array ===\n"); for (i = 0; i < array_size; i++) { root1 = bstInsert(root1, array[i]); } displayTree(root1, 0); printf("Height of bst1 is %d\n", getBstHeight(root1)); // task2: sort the array and use the sorted array to construct a bst, each time taking an element in order printf("=== task2: sort the array and use the sorted array to construct a bst ===\n"); bsort1(array, array_size); for (i = 0; i < array_size; i++) { root2 = bstInsert(root2, array[i]); } displayTree(root2, 0); printf("Height of bst2 is %d\n", getBstHeight(root2)); // task3: use the sorted array to construct a balanced bst printf("=== task3: use the sorted array to construct a balanced bst ===\n"); root3 = sortedArrayToBalancedBst(array, 0, array_size-1); displayTree(root3, 0); printf("Height of bst3 is %d\n", getBstHeight(root3)); return 0; }