Example #1
0
void postorderRecursive (TNODE_p_t root) {
	if (root != NULL) {
		postorderRecursive(root->left);
		postorderRecursive(root->right);
		printf(" %d ", root->data);
	}
}
void postorderRecursive(struct TreeNode* node, UT_array* v) {
    if (node != NULL) {
        postorderRecursive(node->left, v);
        postorderRecursive(node->right, v);
        utarray_push_back(v, &node->val);
    }
}
/**
 * Return an array of size *returnSize.
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* postorderTraversal(struct TreeNode* root, int* returnSize) {
    UT_array* vec;
    utarray_new(vec, &ut_int_icd);
    utarray_reserve(vec, 8);

    postorderRecursive(root, vec);
    *returnSize = utarray_len(vec);

    int bytes = utarray_len(vec) * sizeof(int);
    int* res = malloc(bytes);
    memcpy(res, vec->d, bytes);
    utarray_free(vec);
    return res;
}
Example #4
0
int main (int argc, const char * argv []) {
	
	// Sample input: 1 2 4 8 12 0 15 0 0 0 0 5 0 9 0 0 3 6 10 0 13 16 0 0 17 0 0 11 0 14 18 0 0 19 0 0 7 0 0
	// Sample input: 1 2 4 7 0 0 8 10 13 0 0 14 0 0 0 5 0 9 11 0 0 12 0 15 0 0 3 0 6 0 0
	
	TNODE_p_t tree = buildTree();
	
	int choice = 0;
	
	do {
		
		printf("\n-----------------------------------------------------------------------");
		printf("\n\t1. Rebuild tree.\n\t2. Insert element.\n\t3. Preorder traversal.\n\t4. Inorder traversal.\n\t5. Postorder traversal.\n\t6. Search for an element.\n\t7. Path between two elements.\n\t8. Lowest common ancestor.\n\t9. Find diameter of the tree.\n\tChoice: ");
		scanf(" %d", &choice);
		
		if (choice == 1)
			tree = buildTree();
		
		else if (choice == 2) {
			int item;
			printf("\n\tEnter item to be inserted: ");
			scanf(" %d", &item);
			insertTree(&tree, item);
		}
		
		else if (choice == 3)
			preorderRecursive(tree);
		
		else if (choice == 4)
			inorderRecursive(tree);
		
		else if (choice == 5)
			postorderRecursive(tree);
		
		else if (choice == 6) {
			int item;
			printf("\n\tEnter item to be searched: ");
			scanf(" %d", &item);
			TNODE_p_t search = searchTree(tree, item);
			if (search != NULL)
				printf("\n\t'%d' is present in the tree. (%p).", item, search);
			else
				printf("\n\t'%d' is not present in the tree.", item);
		}
		
		else if (choice == 7) {
			int itema, itemb;
			printf("\n\tEnter elements you want to find the path between: ");
			scanf(" %d", &itema);
			scanf(" %d", &itemb);
			
			TNODE_p_t searcha = searchTree(tree, itema);
			TNODE_p_t searchb = searchTree(tree, itemb);
			
			if (searcha == NULL)
				printf("\n\t'%d' is not present in the tree.", itema);
			if (searchb == NULL)
				printf("\n\t'%d' is not present in the tree.", itemb);
			
			if (searcha != NULL && searchb != NULL) {
				TSNODE_p_t patha = NULL;
				TSNODE_p_t pathb = NULL;
				pathFromNode(tree, itema, &patha);
				pathFromNode(tree, itemb, &pathb);
				TNODE_p_t lca = lowestCommonAncestor(patha, pathb);
				TSNODE_p_t pathAB = pathBetweenLists(patha, pathb, lca);
				printf("\n\tPath between %d and %d: ", itema, itemb);
				displayList(pathAB);
			}
		}
		
		else if (choice == 8) {
			int itema, itemb;
			printf("\n\tEnter elements you want to find the lowest common ancestor of: ");
			scanf(" %d", &itema);
			scanf(" %d", &itemb);
			
			TNODE_p_t searcha = searchTree(tree, itema);
			TNODE_p_t searchb = searchTree(tree, itemb);
			
			if (searcha == NULL)
				printf("\n\t'%d' is not present in the tree.", itema);
			if (searchb == NULL)
				printf("\n\t'%d' is not present in the tree.", itemb);
			
			if (searcha != NULL && searchb != NULL) {
				TSNODE_p_t patha = NULL;
				TSNODE_p_t pathb = NULL;
				pathFromNode(tree, itema, &patha);
				pathFromNode(tree, itemb, &pathb);
				TNODE_p_t lca = lowestCommonAncestor(patha, pathb);
				printf("\n\tLowest common ancestor of %d and %d: %d", itema, itemb, lca->data);
			}
		}
		
		else if (choice == 9) {
			int diam = diameter(tree);
			printf("\n\tDiameter of the tree = %d.", diam);
		}
		
	} while (choice >= 1 && choice <= 9);
	
	printf("\n\n");
	
	return 0;
}
Example #5
0
int main (int argc, const char * argv []) {
	
	printf("\n\tStart inserting into the tree...\n");
	TNODE_p_t tree = createNode();
	
	int choice;
	do {
		printf("\n---------------------------------------------------------------------");
		printf("\n\t0. Rebuild tree (recursive)\n\t1. Insert element (iterative)\n\t2. Insert element (recursive)\n\t3. Preorder transversal (recursive)\n\t4. Preorder transversal (iterative)\n\t5. Inorder transversal (recursive)\n\t6. Inorder transversal (iterative)\n\t7. Postorder transversal (recursive)\n\t8. Postorder transversal (iterative)\n\t9. Level order transversal (recursive)\n\t10. Search for an item (recursive)\n\t11. Check if it's BST.\n\t12. Check if it's a Mirror.\n\tEnter choice: ");
		scanf(" %d", &choice);
		
		String item = initString(SIZE);
		
		switch (choice) {
				
			case 0: tree = createNode();
				break;
				
			case 1: printf("\n\tEnter item to be inserted: ");
				scanf(" %s", item);
				insertIterative(&tree, item);
				break;
				
			case 2: printf("\n\tEnter item to be inserted: ");
				scanf(" %s", item);
				insertRecursive(&tree, item);
				break;
				
			case 3: printf("\n\tPreorder (Rec): ");
				preorderRecursive(tree);
				break;
				
			case 4: printf("\n\tPreorder (Iter): ");
				preorderIterative(tree);
				break;
				
			case 5: printf("\n\n\tInorder (Rec): ");
				inorderRecursive(tree);
				break;
				
			case 6: printf("\n\tInorder (Iter): ");
				inorderIterative(tree);
				break;
				
			case 7: printf("\n\n\tPostorder (Rec): ");
				postorderRecursive(tree);
				break;
				
			case 8: printf("\n\tPostorder (Iter): ");
				postorderIterative(tree);
				break;
				
			case 9: printf("\n\n\tLevel Order: ");
				levelOrder(tree);
				break;
				
			case 10: {
				printf("\tEnter item to be searched: ");
				scanf(" %s", item);
				TNODE_p_t loc = search(tree, item);
				if (loc != NULL)
					printf("\n\t'%s' is present in the tree. (%p)\n", item, loc);
				else
					printf("\n\t'%s' is not present in the tree.\n", item);
				break;
			}
				
			case 11: {
				if (isBST(tree)) {
					printf("\n\tTree is BST. Inorder: ");
					inorderRecursive(tree);
				}
				else {
					printf("\n\tTree is not a BST. Inorder: ");
					inorderRecursive(tree);
				}
				break;
			}
				
			case 12: {
				if (isMirror(tree)) {
					printf("\n\tTree is a mirror. Inorder: ");
					inorderRecursive(tree);
				}
				else {
					printf("\n\tTree is not a mirror. Inorder: ");
					inorderRecursive(tree);
				}
				break;
			}
				
			default: break;
				
		}
		
	} while (choice >= 0 && choice <= 12);
	
}