Example #1
0
/*
function to built a Binary Search Tree (BST) by adding numbers in this specific order
the graph is empty to start: 50, 13, 110 , 10

*/
struct BSTree *buildBSTTree() {
    /*     50
         13  110
        10 
    */
    struct BSTree *tree	= newBSTree();		
		
	/*Create value of the type of data that you want to store*/
	struct data *myData1 = (struct data *) malloc(sizeof(struct data));
	struct data *myData2 = (struct data *) malloc(sizeof(struct data));
	struct data *myData3 = (struct data *) malloc(sizeof(struct data));
	struct data *myData4 = (struct data *) malloc(sizeof(struct data));
		
	myData1->number = 50;
	myData1->name = "rooty";
	myData2->number = 13;
	myData2->name = "lefty";
	myData3->number = 110;
	myData3->name = "righty";
	myData4->number = 10;
	myData4->name = "lefty of lefty";
	
	/*add the values to BST*/
	addBSTree(tree, myData1);
	addBSTree(tree, myData2);
	addBSTree(tree, myData3);
	addBSTree(tree, myData4);
    
    return tree;
}
Example #2
0
int main(int argc, char *argv[]){
    struct BSTree *tree = newBSTree();
    startScreen();
    buildTree(tree);
    playGame(tree);
    return 0;
}
Example #3
0
int containsBSTree(struct BSTree *tree, TYPE val)
{
        assert(val != 0);
	struct BSTree *tmp = newBSTree();
	assert(tmp != 0);
	
	if(compare(val, tree->root->val) == 0)
	{
	    clearBSTree(tmp);
	    deleteBSTree(tmp);
       	    return 1;
	}
	else if(compare(val, tree->root->val) == -1)
	{
	    tmp->root = tree->root->left;
	    
	}
	else if(compare(val, tree->root->val) == 1)
	{
	    tmp->root = tree->root->right;
	    
	}	
	if(tmp->root == NULL)
	{
	  clearBSTree(tmp);
	  deleteBSTree(tmp);
	  return 0;
	}

	return containsBSTree(tmp, val);
}
Example #4
0
File: main.c Project: jonese8/cs261
int main(int argc, char *argv[])
{
	struct BSTree *tree	= newBSTree();

	/*Create value of the type of data that you want to store*/
	struct data myData1;
	struct data myData2;
	struct data myData3;
	struct data myData4;

	myData1.number = 5;
	myData1.name = "rooty";
	myData2.number = 1;
	myData2.name = "lefty";
	myData3.number = 10;
	myData3.name = "righty";
	myData4.number = 3;
	myData4.name = "righty";

	/*add the values to BST*/
	addBSTree(tree, &myData1);
	addBSTree(tree, &myData2);
	addBSTree(tree, &myData3);
	addBSTree(tree, &myData4);

	/*Print the entire tree*/
	printTree(tree);
	/*(( 1 ( 3 ) ) 5 ( 10 ))*/
	return 1;
}
Example #5
0
/*----------------------------------------------------------------------------*/
struct Node *_removeNode(struct Node *cur, TYPE val)
{
	/*write this*/
	struct BSTree *tempTree = newBSTree();
	tempTree->root = cur;

	assert(val);
	assert(cur);
	assert(containsBSTree(tempTree, val));

	if (compare(val, cur->val) == 0) {
        if (cur->right == 0) {
            return cur->left;
        }
        else {
            cur->val = _leftMost(cur);
            cur->right = _removeLeftMost(cur);
        }
	}
	else {
        if (compare(val, cur->val) == -1) {
            cur->left = _removeNode(cur->left, val);
        }
        else {
            cur->right = _removeNode(cur->right, val);
        }
	}
    return cur;

}
Example #6
0
/* Main function for testing different functions of the assignment */
int main() {
		printf("%u\n",compareValues(2.0,2.0));
    struct BSTree *tree	= newBSTree();

    testAddNode(tree);

    testContainsBSTree(tree);

    printf("printing in-order traversal \n");
    inorderTraversal(tree->root);

    printf("printing pre-order traversal \n");
    preorderTraversal(tree->root);

    printf("printing post-order traversal \n");
    postorderTraversal(tree->root);

    testLeftMost(tree);

    testRemoveNode(tree);

    freeBSTree(tree);

	return 0;
}
Example #7
0
/*
  Function to test if nodes are added correctly.
*/
void testAddNode() {
   struct BSTree *tree	= newBSTree();
   struct data *test_data;
  
   test_data = malloc(sizeof(struct data));
   test_data->number = 90;
   test_data->name = "Root";
   addBSTree(tree, test_data);
   /*Check the Root node*/
   printTestResult( (compare(tree->root->val, test_data) == 0), "testAddNode", "Inserting 90 as Root");
   printTestResult( (tree->cnt == 1), "testAddNode", "Increase tree->cnt to 1 when inserting 90 as Root");

   test_data = malloc(sizeof(struct data));
   test_data->number = 10;
   test_data->name = "Root->L";
   addBSTree(tree, test_data);
   /*Check the position of the second element that is added to the BST tree*/
   printTestResult( (compare(tree->root->left->val, test_data) == 0), "testAddNode", "Inserting 10 as Root->L");
   printTestResult( (tree->cnt == 2), "testAddNode", "Increase tree->cnt to 2 when inserting 10 as Root->L");

   test_data = malloc(sizeof(struct data));
   test_data->number = 100;
   test_data->name = "Root->R";
   addBSTree(tree, test_data);
   /*Check the position of the third element that is added to the BST tree*/
   printTestResult( (compare(tree->root->right->val, test_data) == 0), "testAddNode", "Inserting 100 as Root->R");
   printTestResult( (tree->cnt == 3), "testAddNode", "Increase tree->cnt to 3 when inserting 100 as Root->R");

   test_data = malloc(sizeof(struct data));
   test_data->number = 5;
   test_data->name = "Root->L->L";
   addBSTree(tree, test_data);
   /*Check the position of the fourth element that is added to the BST tree*/
   printTestResult( (compare(tree->root->left->left->val, test_data) == 0), "testAddNode", "Inserting 5 as Root->L->L");
   printTestResult( (tree->cnt == 4), "testAddNode", "Increase tree->cnt to 4 when inserting 5 as Root->L->L");

   test_data = malloc(sizeof(struct data));
   test_data->number = 50;
   test_data->name = "Root->L->R";
   addBSTree(tree, test_data);
   /*Check the position of the fifth element that is added to the BST tree*/
   printTestResult( (compare(tree->root->left->right->val, test_data) == 0), "testAddNode", "Inserting 50 as Root->L->R");
   printTestResult( (tree->cnt == 5), "testAddNode", "Increase tree->cnt to 5 when inserting 50 as Root->L->R");

   test_data = malloc(sizeof(struct data));
   test_data->number = 40;
   test_data->name = "Root->L->R->L";
   addBSTree(tree, test_data);
   /*Check the position of the sixt element that is added to the BST tree*/
   printTestResult( (compare(tree->root->left->right->left->val, test_data) == 0), "testAddNode", "Inserting 40 as Root->L->R->L");
   printTestResult( (tree->cnt == 6), "testAddNode", "Increase tree->cnt to 6 when inserting 40 as Root->L->R->L");

   printf("Deleting the BSTree...\n");
   deleteBSTree(tree);
   printf("Returning from testAddNode().\n");
}
Example #8
0
int main(int argc, char *argv[])
{
	BSTree T;
	int i, n, nvals, v[MAXVALS];

	// Build tree from values in stdin
	T = newBSTree();
	nvals = 0;
	while (nvals < MAXVALS && scanf("%d",&n) == 1) {
		v[nvals++] = n;
		T = BSTreeInsert(T,n);
	}

	// Display information about constructed tree
	printf("Tree:\n");showBSTree(T);
	printf("\n#nodes = %d,  ",BSTreeNumNodes(T));
	printf("#leaves = %d\n",BSTreeNumLeaves(T));
	printf("Infix  : "); BSTreeInfix(T); printf("\n");
	printf("Prefix : "); BSTreePrefix(T); printf("\n");
	printf("Postfix: "); BSTreePostfix(T); printf("\n");
	printf("ByLevel: "); BSTreeLevelOrder(T); printf("\n");

	if (argc >= 2) {
		FILE *out = fopen(argv[1], "w");
		if (out) {
			BSTreeDot(T, out);
			fclose(out);
		} else {
			perror("fopen");
		}
	}

	// Check correctness of tree

	// assume no duplicates => each value produces a node
	assert(nvals == BSTreeNumNodes(T));
	// every inserted value can be found
	for (i = 0; i < nvals; i++)
		assert(BSTreeFind(T,v[i]) != 0);
	// (hopefully) non-existent value cannot be found
	assert(BSTreeFind(T,-7654321) == 0);

	dropBSTree(T);
	return 0;
}
Example #9
0
int main(int argc, char *argv[]){
	BSTree tree = newBSTree(5);
	assert(inTree(tree,5));
	insert(tree, 3);
	assert(inTree(tree,3));
	insert(tree, 7);
	assert(inTree(tree,7));
	insert(tree, 2);
	assert(inTree(tree,2));
	insert(tree, 4);
	assert(inTree(tree,4));
	insert(tree, 6);
	assert(inTree(tree,6));
	insert(tree, 8);
	assert(inTree(tree,8));

	prefixPrint(tree);

	dropTree(tree);
	return EXIT_SUCCESS;
}
Example #10
0
struct BSTree* buildAnimalTree(){
	struct BSTree *tree = newBSTree();

	struct animal *animal1 = (struct animal *)malloc(sizeof(struct animal));
	struct animal *animal2 = (struct animal *)malloc(sizeof(struct animal));
	struct animal *animal3 = (struct animal *)malloc(sizeof(struct animal));
	struct animal *animal4 = (struct animal *)malloc(sizeof(struct animal));
	struct animal *animal5 = (struct animal *)malloc(sizeof(struct animal));
	struct animal *animal6 = (struct animal *)malloc(sizeof(struct animal));
	struct animal *animal7 = (struct animal *)malloc(sizeof(struct animal));

	animal1->truth = 0;
	animal1->statement = "Lives in water?";
	animal2->truth = -1;
	animal2->statement = "Mammal?";
	animal3->truth = 1;
	animal3->statement = "Climb trees?";
	animal4->truth = -1;
	animal4->statement = "Whale?";
	animal5->truth = 1;
	animal5->statement = "Sea Turtle?";
	animal6->truth = -1;
	animal6->statement = "Cat?";
	animal7->truth = 1;
	animal7->statement = "Dog?";

	addBSTree(tree, animal1);
	addBSTree(tree, animal2);
	addBSTree(tree, animal3);
	addBSTree(tree, animal4);
	addBSTree(tree, animal5);
	addBSTree(tree, animal6);
	addBSTree(tree, animal7);

	return tree;
}
Example #11
0
/*
function to built a Binary Search Tree (BST) by adding numbers in this specific order
the graph is empty to start: 50, 13, 110 , 10
*/
struct BSTree *buildBSTTree() {
	// REMOVE THIS PRINT STATEMENT
	printf("%s\n", "building tree");
    /*     50
         13  110
        10
    */
    struct BSTree *tree	= newBSTree();

		// REMOVE THIS PRINT STATEMENT
		printf("%s\n", "allocating memory");
	/*Create value of the type of data that you want to store*/
	struct data *myData1 = (struct data *) malloc(sizeof(struct data));
	struct data *myData2 = (struct data *) malloc(sizeof(struct data));
	struct data *myData3 = (struct data *) malloc(sizeof(struct data));
	struct data *myData4 = (struct data *) malloc(sizeof(struct data));
	printf("%s\n", "done allocating memory");

	myData1->number = 50;
	myData1->name = "rooty";
	myData2->number = 13;
	myData2->name = "lefty";
	myData3->number = 110;
	myData3->name = "righty";
	myData4->number = 10;
	myData4->name = "lefty of lefty";

	/*add the values to BST*/
	addBSTree(tree, myData1);
	addBSTree(tree, myData2);
	addBSTree(tree, myData3);
	addBSTree(tree, myData4);
	// REMOVE THIS PRINT STATEMENT
	printf("%s\n", "building tree finished");
    return tree;
}
Example #12
0
void testAddNode() {
    struct BSTree *tree	= newBSTree();
    
    struct data myData1;
	struct data myData2;
	struct data myData3;
	struct data myData4;
		
	myData1.number = 50;
	myData1.name = "rooty";
    addBSTree(tree, &myData1);
    
    
    if (compare(tree->root->val, (TYPE *) &myData1) != 0) {
        printf("addNode() test: FAIL to insert 50 as root\n");
        return;
    }
    else if (tree->cnt != 1) {
        printf("addNode() test: FAIL to increase count when inserting 50 as root\n");
        return;
    }
    else printf("addNode() test: PASS when adding 50 as root\n");
    
    
	myData2.number = 13;
	myData2.name = "lefty";
    addBSTree(tree, &myData2);
    
    
    if (compare(tree->root->left->val, (TYPE *) &myData2) != 0) {
        printf("addNode() test: FAIL to insert 13 as left child of root\n");
        return;
    }
    else if (tree->cnt != 2) {
        printf("addNode() test: FAIL to increase count when inserting 13 as left of root\n");
        return;
    }
    else printf("addNode() test: PASS when adding 13 as left of root\n");
    
    
	myData3.number = 110;
	myData3.name = "righty";
    addBSTree(tree, &myData3);
        
        
    if (compare(tree->root->right->val, (TYPE *) &myData3) != 0) {
        printf("addNode() test: FAIL to insert 110 as right child of root\n");
        return;
    }
    else if (tree->cnt != 3) {
        printf("addNode() test: FAIL to increase count when inserting 110 as right of root\n");
        return;
    }
    else printf("addNode() test: PASS when adding 110 as right of root\n");
    
    
	myData4.number = 10;
	myData4.name = "righty of lefty";
	addBSTree(tree, &myData4);
    
    if (compare(tree->root->left->left->val, (TYPE *) &myData4) != 0) {
        printf("addNode() test: FAIL to insert 10 as left child of left of root\n");
        return;
    }
    else if (tree->cnt != 4) {
        printf("addNode() test: FAIL to increase count when inserting 10 as left of left of root\n");
        return;
    }
    else printf("addNode() test: PASS when adding 10 as left of left of root\n");
}
Example #13
0
/*
fucntion to test each node of the BST and their children

*/
void testAddNode() {
    struct BSTree *tree	= newBSTree();
    
    struct data myData1,  myData2,  myData3,  myData4;
		
	myData1.number = 50;
	myData1.name = "rooty";
    addBSTree(tree, &myData1);
    //check the root node
    if (compare(tree->root->val, (TYPE *) &myData1) != 0) {
        printf("addNode() test: FAIL to insert 50 as root\n");
        return;
    }
	//check the tree->cnt value after adding a node to the tree
    else if (tree->cnt != 1) {
        printf("addNode() test: FAIL to increase count when inserting 50 as root\n");
        return;
    }
    else printf("addNode() test: PASS when adding 50 as root\n");
    
    
	myData2.number = 13;
	myData2.name = "lefty";
    addBSTree(tree, &myData2);
    
    //check the position of the second element that is added to the BST tree
    if (compare(tree->root->left->val, (TYPE *) &myData2) != 0) {
        printf("addNode() test: FAIL to insert 13 as left child of root\n");
        return;
    }
    else if (tree->cnt != 2) {
        printf("addNode() test: FAIL to increase count when inserting 13 as left of root\n");
        return;
    }
    else printf("addNode() test: PASS when adding 13 as left of root\n");
    
    
	myData3.number = 110;
	myData3.name = "righty";
    addBSTree(tree, &myData3);
        
    //check the position of the third element that is added to the BST tree    
    if (compare(tree->root->right->val, (TYPE *) &myData3) != 0) {
        printf("addNode() test: FAIL to insert 110 as right child of root\n");
        return;
    }
    else if (tree->cnt != 3) {
        printf("addNode() test: FAIL to increase count when inserting 110 as right of root\n");
        return;
    }
    else printf("addNode() test: PASS when adding 110 as right of root\n");
    
    
	myData4.number = 10;
	myData4.name = "righty of lefty";
	addBSTree(tree, &myData4);
    
	//check the position of the fourth element that is added to the BST tree
    if (compare(tree->root->left->left->val, (TYPE *) &myData4) != 0) {
        printf("addNode() test: FAIL to insert 10 as left child of left of root\n");
        return;
    }
    else if (tree->cnt != 4) {
        printf("addNode() test: FAIL to increase count when inserting 10 as left of left of root\n");
        return;
    }
    else printf("addNode() test: PASS when adding 10 as left of left of root\n");
}
Example #14
0
/*
  Function to built a Binary Search Tree (BST) by adding numbers in this specific order
  the graph is empty to start: 90, 10, 5, 50, 55, 40, 100, 

*/
struct BSTree *buildBSTTree() {
    /*       90
	 10     100
       5   50       110
         40  55   105 
                    106	
    */
    struct BSTree *tree	= newBSTree();		
    struct data *myData;

    /*Generate new Nodes and add them to the tree.*/
    myData =  malloc(sizeof(struct data));
    myData->number = 90;
    myData->name = "Root";
    addBSTree(tree, myData);

    myData = malloc(sizeof(struct data));
    myData->number = 10;
    myData->name = "Root->L";
    addBSTree(tree, myData);

    myData = malloc(sizeof(struct data));
    myData->number = 5;
    myData->name = "Root->L->L";
    addBSTree(tree, myData);

    myData = malloc(sizeof(struct data));
    myData->number = 50;
    myData->name = "Root->L->R";
    addBSTree(tree, myData);

    myData = malloc(sizeof(struct data));
    myData->number = 55;
    myData->name = "Root->L->R->R";
    addBSTree(tree, myData);

    myData = malloc(sizeof(struct data));
    myData->number = 40;
    myData->name = "Root->L->R->L";
    addBSTree(tree, myData);

    myData = malloc(sizeof(struct data));
    myData->number = 100;
    myData->name = "Root->R";
    addBSTree(tree, myData);

    myData = malloc(sizeof(struct data));
    myData->number = 110;
    myData->name = "Root->R->R";
    addBSTree(tree, myData);

    myData = malloc(sizeof(struct data));
    myData->number = 105;
    myData->name = "Root->R->R->L";
    addBSTree(tree, myData);

    myData = malloc(sizeof(struct data));
    myData->number = 106;
    myData->name = "Root->R->R->L->R";
    addBSTree(tree, myData);

    return tree;
}
Example #15
0
File: main.c Project: guwu/cs261
int main(int argc, char *argv[])
{
	struct BSTree *tree	= newBSTree();

	/*Create value of the type of data that you want to store*/
	struct data myData1;
	struct data myData2;
	struct data myData3;
	struct data myData4;
	struct data myData5;
	struct data myData6;
	struct data myData7;
	struct data myData8;
	struct data myData9;
	struct data myData10;
	struct data myData11;
	struct data myData12;

	myData1.number = 5;
	myData1.name = "rooty";
	myData2.number = 1;
	myData2.name = "lefty";
	myData3.number = 10;
	myData3.name = "righty";
	myData4.number = 3;
	myData4.name = "righty";
	myData5.number = 50;
	myData6.number = 40;
	myData7.number = 29;
	myData8.number = 31;
	myData9.number = 7;
	myData10.number = 15;
	myData11.number = 55;
	myData12.number = 5;
	

	/*add the values to BST*/
	addBSTree(tree, &myData1);
	addBSTree(tree, &myData2);
	addBSTree(tree, &myData3);
	addBSTree(tree, &myData4);
	addBSTree(tree, &myData5);
	addBSTree(tree, &myData6);
	addBSTree(tree, &myData7);
	addBSTree(tree, &myData8);
	addBSTree(tree, &myData9);
	addBSTree(tree, &myData10);
	addBSTree(tree, &myData11);
	addBSTree(tree, &myData12);

	/*Print the entire tree*/
	printTree(tree);
	printf("\n");
	/*(( 1 ( 3 ) ) 5 ( 10 ))*/
	printf("Tree contains 1?  %s\n",
        containsBSTree(tree, &myData2) ? "True" : "False");
	removeBSTree(tree, &myData2);
	printTree(tree);
	printf("\n");
	return 1;
}