예제 #1
0
// test balancing of a binary tree
void test5(int n, int min, int max)
{
	test("balance binary tree ", 5);
	tree *t = create_random_tree(n, min, max);
	printf("Binary Tree: \n");
	print_ascii_tree(t);
	printf("Balanced Tree: \n");
	balance_tree(&t, n);
	print_ascii_tree(t);
	free_tree(t);
}
예제 #2
0
// test binary tree to linked list conversion
void test4(int n, int min, int max)
{
	test("convert binary tree to linked list",4);

	tree *t = create_random_tree(n,min,max);
	print_ascii_tree(t);
	printf("Binary Tree to Linked List: \n");
	create_backbone(&t);
	print_ascii_tree(t);
	free_tree(t);
}
예제 #3
0
int main(void)
{
	struct node* root = NULL;
	//struct node* root2 = NULL;
	struct node *find = NULL;

	char str[1024];

	root = insert(root, 10);
	root = insert(root, 15);
	root = insert(root, 9);
	root = insert(root, 8);
	root = insert(root, 13);
	root = insert(root, 7);
	root = insert(root, 5);
	root = insert(root, 18);
	root = insert(root, 22);
	root = insert(root, 3);
	root = insert(root, 4);
	root = insert(root, 2);
	root = insert(root, 1);
	print_ascii_tree(root);
	find = search(root, 18);
	print_ascii_tree(root);
	find = search(root, 22);
	printf("\n\n\nDATA found is %d\n", find->data);
	find = min_node(root);
	printf("Min in this tree is %d\n", find->data);
	find = max_node(root);
	printf("Mx in this tree is %d\n", find->data);
	print_ascii_tree(root);
	preorder(root);
	printf("\n");
	inorder(root);
	printf("\n");
	postorder(root);
	printf("\n");
	printf("DEPTH is %d\n", depth(root));
	tree_to_string(root, str);
	printf("The STR generated is %s\n", str);
	//string_to_tree(&root2, str);
	//print_ascii_tree(root2);
	printf("COUNT is %d\n",nodes(root));
	bool res = hassum(root, 45);
	printf("Bool val is %d\n", (int)res);
	levelorder(root);
	return 0;
}
예제 #4
0
// test predecessor & successor of a BST
void test6(int n, int min, int max)
{
	test("Test predecessor: ",6);
	tree *t = create_random_tree(n, min, max);
	print_ascii_tree(t);
	int i;
	for ( i = min; i <= max	; i = i+10)
	{
		tree *predecessor = find_predecessor(t, i);
		printf("%d's predecessor: ", i);
		if( predecessor == NULL)
			printf(" not found\n");
		else
			printf(" %d\n", predecessor->data);
	}

	test("Test successor: ",6);
	for ( i = min; i <= max	; i = i+10)
	{
		tree *successor = find_successor(t, i);
		printf("%d's successor: ", i);
		if( successor == NULL)
			printf(" not found\n");
		else
			printf(" %d\n", successor->data);
	}

	free_tree(t);
}
예제 #5
0
// create random tree and print
test1(int n, int min, int max)
{
	test("create random tree and print",1);
	tree *t = 0;
	t  = create_random_tree(n, min, max);
	print_ascii_tree(t);
	free_tree(t);
}
예제 #6
0
파일: main.c 프로젝트: beingzeroin/cps-9
int main()
{
    BSTNode *root = NULL;
    root = insertBSTNode(root, 5);

    root = insertBSTNode(root, 3);
    root = insertBSTNode(root, 1);
    root = insertBSTNode(root, 4);

    root = insertBSTNode(root, 7);
    root = insertBSTNode(root, 6);
    root = insertBSTNode(root, 9);

    print_ascii_tree(root);

    // Delete Root
    root = delteBSTNode(root, 5);

    print_ascii_tree(root);

    return 0;
}
예제 #7
0
// search from min to max
void test2(int n, int min, int max)
{
	test("search elements between min & max",2);
	int i;
	tree *result;
	tree *t = create_random_tree(n, min, max);
	print_ascii_tree(t);

	for( i = min ; i < max; ++i)
	{
		result = search(t,i);
		if( result != NULL )
			printf("%d found \n", i);
	}
	free_tree(t);
}
예제 #8
0
// delete within min and max
void test3(int n, int min, int max)
{
	test("delete elements between min & max",3);
	int i, size ;
	tree *t = create_random_tree(n, min, max);
	size = max - min;
	int *a = (int *) malloc(size*sizeof(int));

	for( i = 0 ; i < size; ++i)
		a[i] = i;
	shuffle(a, size);
	for(i = 0 ; i <  size; ++i)
	{
		printf("Delete : %d\n", a[i]);
		delete_by_copying(&t, a[i]);
		print_ascii_tree(t);
	}
	free(a);
	free_tree(t);
}
예제 #9
0
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;
}