void freeBinaryTree(cnode *root) { if (root == NULL) return; freeBinaryTree(root -> previous); freeBinaryTree(root -> next); free(root); }
int main() { struct TreeNode* tree = genBinaryTree("[3,5,1,6,2,0,8,null,null,7,4]"); printf("5, 1 : %d\n", lowestCommonAncestor(tree, tree->left, tree->right)->val); printf("5, 4 : %d\n", lowestCommonAncestor(tree, tree->left, tree->left->right->right)->val); freeBinaryTree(tree); tree = genBinaryTree("1,2,3"); printf("2, 3 : %d\n", lowestCommonAncestor(tree, tree->right, tree->left)->val); freeBinaryTree(tree); return 0; }
void freeBinaryTree(bnode *root) { if (root == NULL) return; freeBinaryTree(root -> left); freeBinaryTree(root -> right); free(root -> word); free(root); }
int main() { char *data = "[1,-2,3]"; struct TreeNode *root = genBinaryTree(data); printf("The max path sum is %d\n", maxPathSum(root)); freeBinaryTree(root); return 0; }
int main (int argc, char *argv[]) { if (argc == 1) { printf("You must include a filename in the program call.\n"); exit(1); } else if (argc > 2) { printf("Too many arguments.\n"); exit(1); } maxSize = 1; char *word = NULL; char *str = NULL; bnode *root = NULL; int x; str = readFile(argv[1]); if(str != NULL) { const char* limiter = " ,\n\r\t."; //set these as constant characters. Space is included. word = strtok(str,limiter); if (word == NULL) { free(str); printf("Empty file.\n"); exit(2); } root = addNode(root, word); //pass the word in at the root for binary tree. while((word = strtok(NULL,limiter)) != NULL) root = addNode(root, word); free(str); //root = rotateTree(root); for (x = 1; x <= maxSize; x++) //this is the worst practice and I am so sorry printBinaryTree(root, x); } freeBinaryTree(root); return 0; }
int main() { printf("Enter numbers for doubly linked list in sorted order\n"); int holding = 0; char junk = '.'; junk = getchar(); if (atoi(&junk) == 0 && junk != '0') { printf("Null list added, binary tree is null.\n"); exit(1); } int x = 1; char *build = malloc(sizeof(char) * x); char *tmp = '\0'; build[0] = '\0'; while (atoi(&junk) != 0 || junk == '0') { build[x-1] = junk; junk = getchar(); x++; tmp = realloc(build, sizeof(char) * x); if (tmp != NULL) build = tmp; else { printf("Out of memory, program exit."); free(build); exit(3); } build[x-1] = '\0'; } holding = atoi(build); free(build); cnode *head = malloc(sizeof(cnode)); head -> info = holding; head -> previous = NULL; head -> next = NULL; cnode *tail = head; while (junk == ' ') { scanf("%d", &holding); if (holding < tail -> info) { printf("List is out of order, program exit.\n"); freeDoublyLinkedList(head, tail); exit(2); } tail = insertTail(head, tail, holding); junk = getchar(); } printf("Doubly Linked List Contents:\n"); cnode *ptr = head; while (ptr != NULL) { printf("%d ", ptr -> info); ptr = ptr -> next; } printf("\n"); cnode *root = toBinaryTree(head, tail); printf("Binary Search Tree Contents:\n"); printBinaryTree(root); printf("\n"); freeBinaryTree(root); return 0; }