Esempio n. 1
0
void destroyBST(bst_node_ptr root) {
    if (root == NULL) {
        return;
    }
    /* destroy left subtree*/
    if (root->left != NULL) {
        destroyBST(root->left);
        root->left = NULL;
    }
    /* destroy right subtree*/
    if (root->right != NULL) {
        destroyBST(root->right);
        root->right = NULL;
    }
    free(root);
}
void deinit_memory_manage() {
    chunk_ptr tmp_chunk;
    block_ptr tmp_block;
    int i;
    /* free chunks*/
    memory_loginfo("free chunks");
    lockMemoryGlobalInfo();
    while (g_memory_info->chunk_list) {
        tmp_chunk = g_memory_info->chunk_list;
        g_memory_info->chunk_list = tmp_chunk->next;
        free(tmp_chunk->addr);
        free(tmp_chunk);
    }
    /* free bst*/
    memory_loginfo("free bst");
    destroyBST(g_memory_info->category_map_BST);
    /* free free block chain*/
    memory_loginfo("free free block chain");
    for (i = 0; i < CATEGORY_NUM; i ++) {
        memory_debug("free category %d free block chain", i);
        while (g_memory_info->free_block_chains[i]) {
            tmp_block = g_memory_info->free_block_chains[i];
            g_memory_info->free_block_chains[i] = tmp_block->next;
            free(tmp_block);
        }
    }
    free(g_memory_info);
    g_memory_info = NULL;
    memory_loginfo("deinit successed");
    unlockMemoryGlobalInfo();
    destroyMemoryGlobalInfoLock();
}
Esempio n. 3
0
int main(void)
{
	BST* contactBST; //NOT sure if these are right
	char userInput;
	int inputStatus;
	int quitStatus = 0;

	contactBST = createBST(compareWord);
	readFileBST(contactBST);
	while(quitStatus == 0){
		inputStatus = 0;
		while(inputStatus == 0){ //Looping until choice is entered correctly
			fflush(stdin);
			printf("Please select: (a) add, (d) delete, (l) lookup, or (q) quit: "); 
			scanf("%c",&userInput);
			if(userInput == 'a' || userInput == 'd' || userInput == 'l' || userInput == 'q'){
				inputStatus = 1;
			}
		}
		printf("Choice: %c",userInput);
		if(userInput == 'q'){ //User Quits
				quitStatus = 1;
		}
		else{
			if(userInput == 'a'){
				addName(contactBST);
			}
			else if(userInput == 'd'){
				//delete function here
			}
			else if(userInput == 'l'){
				//look up function here
			}
		}
	}

	contactBST = destroyBST(contactBST); //Not sure of this
	return 0;
}
Esempio n. 4
0
int main(void){
	contactData* dataList[MAX];
	BST* contactBST; //NOT sure if these are right
	char userInput;
	int inputStatus;
	int quitStatus = 0;
	

	contactBST = createBST(compareWord);
	readFileBST(dataList,contactBST);
	while(quitStatus = 0){
		inputStatus = 0;
		while(inputStatus == 0){ //Looping until choice is entered correctly
			printf("Please select from the following: (a) add, (d) delete, (l) lookup, or (q) quit:"); 
			scanf("%c",userInput);
			if(userInput == 'a' || userInput == 'd' || userInput == 'l' || userInput == 'q');
				inputStatus = 1;
			}
		if(userInput == 'q'){ //User Quits
			quitStatus = 1;
		}
		else{
			if(userInput == 'a'){
				//add function here
			}
			else if(userInput == 'd'){
				//delete function here
			}
			else if(userInput == 'l'){
				//look up function here
			}
		}
	}
	
	contactBST = destroyBST(contactBST); //Not sure of this
	return 0;
}
int main(void)
{
	char inputMenu;
	char inputData;
	int menu = 0;
	int type;

	while (1)
	{
		printf("-----------------------\n");
		printf("1. Insert a Node\n");
		printf("2. Delete a Node\n");
		printf("3. Destroy BST\n");
		printf("4. Find a Node\n");
		printf("5. Tree Traverse\n");
		printf("6. Show the height of tree\n");
		printf("7. Show the shape of Tree\n");
		printf("8. Make the completed BST\n");
		printf("9. Cut Tree\n");
		printf("0. Exit\n");
		printf("-----------------------\n");
		printf("->");

		fgets(&inputMenu, 3, stdin);
		menu = atoi(&inputMenu);

		switch (menu) {

		case 1:
			printf("Type the value to insert\n");
			printf("->");
			fgets(&inputData, 3, stdin);

			insertNode(inputData);

			break;

		case 2:
			printf("Type the node id to delete\n");
			printf("->");
			fgets(&inputData, 3, stdin);

			deleteNode(inputData);

			break;

		case 3:
			destroyBST();

			break;

		case 4:
			printf("Type the node id to find\n");
			printf("->");
			fgets(&inputData, 3, stdin);

			findNode(inputData);

			break;

		case 5:
			printf("Type the type of Tree Traversal (0: Pre-order, 1: In-order, 2: Post-order)\n");
			printf("->");
			fgets(&inputData, 3, stdin);
			type = atoi(&inputData);
			printf("Type the node id to start Traverse\n");
			printf("->");
			fgets(&inputData, 3, stdin);

			treeTraversal(type, inputData);

			break;

		case 6:
			findHeight();

			break;

		case 7:
			showTree();

			break;

		case 8:
			makeCompleteBST();

			break;

		case 9:
			printf("Type the node id that will become the new root node\n");
			printf("->");
			fgets(&inputData, 3, stdin);

			cutTree(inputData);

			break;

		case 0:
			exit(0);

			break;

		default:
			printf("Input should be 0-9\n");
			break;
		}
	}


	return 0;
}
Esempio n. 6
0
//===================================================================
// Cleans up and frees all data
//===================================================================
void cleanupData(DATA_HEAD *data) {
    destroyBST(data->pTree, freeBST);
    destroyStack(data->pStack);
    free(data->pHash);
    free(data);
}