Exemplo n.º 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;
}
Exemplo n.º 2
0
Arquivo: main.c Projeto: 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;
}
Exemplo n.º 3
0
struct BSTree *buildTree(struct BSTree *tree) {
    FILE *ifp;
    char *mode = "r";
    char name[201];
    int number;

    ifp = fopen("animalGame.txt", mode);

    if (ifp == NULL) {
        fprintf(stderr, "Can't open input file in.list!\n");
        exit(1);
    }

    while(fscanf(ifp, " %200[^,],%d", name, &number) == 2){

        /*Create value of the type of data that you want to store*/
        struct data *myData1 = malloc(sizeof(struct data));
        myData1->number = number;
        myData1->name = strdup(name);

        /*add the values to BST*/
        addBSTree(tree, myData1);
        }
        fclose(ifp);
        return tree;
}
Exemplo n.º 4
0
/*
	testAddNode: function to test each node of the BST and children
	pre: tree is not null
	param: tree - the tree we are testing
	post: none
*/
void testAddNode(struct BSTree *tree) {
    assert(tree != NULL);

    printf("testing add node... \n");

    addBSTree(tree, 55);
    if (compareValues(tree->root->val,  55) != 0) {
        printf("addNode() test: FAIL to insert 55 as root\n");
    } else if (tree->cnt != 1) { /*check the tree->cnt value after adding a node to the tree*/
        printf("addNode() test: FAIL to increase count when inserting 55 as root\n");
    } else{
        printf("addNode() test: PASS when adding 55 as root\n");
		}

		addBSTree(tree, 36);
    if (compareValues(tree->root->left->val, 36) != 0) { /*check the position of the second element that is added to the BST tree*/
        printf("addNode() test: FAIL to insert 36 as left child of root\n");
    } else if (tree->cnt != 2) {
        printf("addNode() test: FAIL to increase count when inserting 36 as left of root\n");
    } else
        printf("addNode() test: PASS when adding 36 as left of root\n");

    addBSTree(tree, 78);
    if (compareValues(tree->root->right->val, 78) != 0) { /*check the position of the third element that is added to the BST tree*/
        printf("addNode() test: FAIL to insert 78 as right child of root\n");
    } else if (tree->cnt != 3) {
        printf("addNode() test: FAIL to increase count when inserting 78 as right of root\n");
    } else
        printf("addNode() test: PASS when adding 78 as right of root\n");

	addBSTree(tree, 20);
    if (compareValues(tree->root->left->left->val, 20) != 0) { /*check the position of the fourth element that is added to the BST tree*/
        printf("addNode() test: FAIL to insert 10 as left child of left of root\n");
    } else if (tree->cnt != 4) {
        printf("addNode() test: FAIL to increase count when inserting 20 as left of left of root\n");
    } else
        printf("addNode() test: PASS when adding 20 as left of left of root\n");

	addBSTree(tree, 85);
    if (compareValues(tree->root->right->right->val, 85) != 0) { /*check the position of the fourth element that is added to the BST tree*/
        printf("addNode() test: FAIL to insert 85 as right right child of left of root\n");
    } else if (tree->cnt != 5) {
        printf("addNode() test: FAIL to increase count when inserting 85 as right right child of root\n");
    } else
        printf("addNode() test: PASS when adding 85 as right right child of root\n");

	addBSTree(tree, 67);
    if (compareValues(tree->root->right->left->val, 67) != 0) { /*check the position of the fourth element that is added to the BST tree*/
        printf("addNode() test: FAIL to insert 67 as right left child of left of root\n");
    } else if (tree->cnt != 6) {
        printf("addNode() test: FAIL to increase count when inserting 67 as right left child of root\n");
    } else
        printf("addNode() test: PASS when adding 67 as right left child of root\n");

    printf("done testing add node... \n");

}
Exemplo n.º 5
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;
}
Exemplo n.º 6
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");
}
Exemplo n.º 7
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;
}
Exemplo n.º 8
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");
}
Exemplo n.º 9
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");
}
Exemplo n.º 10
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;
}
Exemplo n.º 11
0
Arquivo: main.c Projeto: 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;
}