Node* initBinaryTree(const char*& str) { if (str == NULL || *str == '\0' || *str == '#') { return NULL; } Node* root = new Node; root->value = *str; root->left = initBinaryTree(++str); root->right = initBinaryTree(++str); return root; }
int main(int argc, char **argv) { struct shell_state shell; shell.last_ret_value = 0; shell.cmds = initBinaryTree(); int counter = 0; char inchar; char wd[80]; dynamic_array *inbuffer = create_array(BLOCKSIZE); while(input < 0) { input = open("/dev/kbd", O_RDONLY | O_APPEND, 0); } printf("Ultrashell started.\n"); getcwd(wd, 80); printf("%s$ ", wd); do { //inchar = getchar(); read(input, &inchar, sizeof(char)); switch(inchar) { case '\0': break; case '\b': if(counter != 0) { set(inbuffer, --counter, '\0'); printf("\b"); } break; default: set(inbuffer, counter++, inchar); set(inbuffer, counter, '\0'); printf("%c", inchar); break; } if(inchar == '\n') { parserLine(&shell, as_string(inbuffer)); getcwd(wd, 80); printf("%s$ ", wd); counter = 0; set(inbuffer, counter, '\0'); } }FOREVER; return 0; }
int main(int argc, char* argv[]) { { const char* str = "5321###4##76##8##"; Node* root = initBinaryTree(str); assert(lowestCommonAncestorSearchTree(root, root->left->left->left, root->left->right)->value == '3'); } { const char* str = "5321###4##76##8##"; Node* root = initBinaryTree(str); assert(lowestCommonAncestorSearchTree(NULL, root->left->left->left, root->left->right) == NULL); assert(lowestCommonAncestorSearchTree(root, root->left->left->left, NULL) == NULL); assert(lowestCommonAncestorSearchTree(root, NULL, root->left->right) == NULL); assert(lowestCommonAncestorSearchTree(NULL, NULL, NULL) == NULL); Node node; assert(lowestCommonAncestorSearchTree(NULL, root->left->left->left, &node) == NULL); } { const char* str = "5321###4##76##8##"; Node* root = initBinaryTree(str); assert(lowestCommonAncestorBinaryTree(root, root->left->left->left, root->left->right)->value == '3'); assert(lowestCommonAncestorBinaryTree(root, root->left->left->left, root->left->left->left)->value == '1'); } { const char* str = "1238###5##36##7##"; Node* root = initBinaryTree(str); assert(lowestCommonAncestorBinaryTree(root, root->left->left->left, root->left->right)->value == '2'); } { const char* str = "1238###5##36##7##"; Node* root = initBinaryTree(str); assert(lowestCommonAncestorBinaryTree(NULL, root->left->left->left, root->left->right) == NULL); assert(lowestCommonAncestorBinaryTree(root, root->left->left->left, NULL) == NULL); assert(lowestCommonAncestorBinaryTree(root, NULL, root->left->right) == NULL); assert(lowestCommonAncestorBinaryTree(NULL, NULL, NULL) == NULL); Node node; assert(lowestCommonAncestorBinaryTree(NULL, root->left->left->left, &node) == NULL); } printf("all test case passed!\n"); return 0; }
int main(int argc, char* argv[]) { const char* str1 = "124##57###3#6##"; Node* root1 = initBinaryTree(str1); assert(getBinaryTreeDeep(root1) == 4); assert(isBalanceTree(root1)); const char* str2 = "5321###4##76##8##"; Node* root2 = initBinaryTree(str2); assert(getBinaryTreeDeep(root2) == 4); assert(isBalanceTree(root2)); const char* str3 = "123####"; Node* root3 = initBinaryTree(str3); assert(getBinaryTreeDeep(root3) == 3); assert(!isBalanceTree(root3)); printf("all test case passed!\n"); return 0; }
int main(int argc, char* argv[]){ BinTreePtr btPtr; Data tempData; Data key; FILE *fp; NodePtr tempNode; char str[50]; tempNode = malloc(sizeof(Node)); fp = fopen("list", "r"); if(fp == NULL) { printf("Could not open file!"); return 1; } btPtr = initBinaryTree(); printf("***Class Search Program***\n\n"); while(fgets(str, 50, fp) != NULL) { if((strlen(str) > 0) && (str[strlen(str) - 1] == '\n')) { str[strlen(str) - 1] = '\0'; } btPtr->root = addBST(btPtr->root, str); } printf("\nSearch for a class: "); fgets(str, 50, stdin); if((strlen(str) > 0) && (str[strlen(str) - 1] == '\n')) { str[strlen(str) - 1] = '\0'; } tempNode = search_(btPtr->root, str); if(tempNode == NULL) { printf("\nClass not found"); } else printf("\nYou have taken the %s course\n", tempNode->nodeData); printf("\nSearch for a 2nd class: "); fgets(str, 50, stdin); if((strlen(str) > 0) && (str[strlen(str) - 1] == '\n')) { str[strlen(str) - 1] = '\0'; } tempNode = search_(btPtr->root, str); if(tempNode == NULL) { printf("\nClass not found"); } else printf("\nYou have taken the %s course\n", tempNode->nodeData); printf("\nSearch for a 3rd class: "); fgets(str, 50, stdin); if((strlen(str) > 0) && (str[strlen(str) - 1] == '\n')) { str[strlen(str) - 1] = '\0'; } tempNode = search_(btPtr->root, str); if(tempNode == NULL) { printf("\nClass not found"); } else printf("\nYou have taken the %s course\n", tempNode->nodeData); inorderTraverse(btPtr->root); return 0; }