void deleteBST(TreeNode *root){
    if(!root)    return;

    deleteBST(root->left);
    root->left = NULL;

    deleteBST(root->right);
    root->right = NULL;

    delete root;
    return;
}
/*
 * test case
 * */
TEST(sortedSLL2BBST_Test, Positive01){
    Solution s;

    ListNode *head = constructSLL();

    TreeNode *result = s.sortedSLL2BBST(head);
    TreeNode *expected = constructBST();

    EXPECT_TRUE(assertBST(expected, result));

    deleteBST(expected);
    deleteBST(result);
    deleteSLL(head);
}
TEST(sortedArray2BBST_Test, Positive01){
    Solution s;

    int arr[] = {1, 2, 3, 4, 5, 6, 7};
    vector<int> num(arr, arr + sizeof(arr) / sizeof(int));

    TreeNode *result = s.sortedArray2BBST(num);
    TreeNode *expected = constructBST();

    EXPECT_TRUE(assertBST(expected, result));

    deleteBST(expected);
    deleteBST(result);
    num.clear();
}
Exemple #4
0
/*5*/void deleteName(BST* contactBST)
{
	char inputStr[MAX];
	contactData * dataList;
	int strlength;
	int sucStatus = 0;
	
	while(sucStatus == 0){
		fflush(stdin);
		printf("Enter name to delete: ");
		fgets(inputStr,MAX,stdin);
		strlength = strlen(inputStr);
		if(inputStr[strlength-1] == '\n'){
			inputStr[strlength-1] = 0;
		}
		sucStatus = deleteBST(contactBST,(void*)inputStr,(void**)&dataList); //Need to check if sucStatus is successful, THIS IS NOT WORKING!!
		if(sucStatus == 1){
			printf("Deleted record: %s, %s %s\n", dataList->name, dataList->phoneNum, dataList->webAddr);
		}
		else{
			printf("%s not found\n\n",inputStr);
			inOrder(contactBST,printTree);
			printf("\n");
		}
	}
}//end deleteName
Exemple #5
0
/*6*/void lookupName(BST* contactBST)
{
	char inputStr[MAX];
	char changeRec, changeWhich;
	contactData* dataList;
	int strlength;
	int sucStatus = 0;

	while(sucStatus == 0){
		fflush(stdin);
		printf("Enter name to look up: ");
		fgets(inputStr,MAX,stdin);
		strlength = strlen(inputStr);
		if(inputStr[strlength-1] == '\n'){
			inputStr[strlength-1] = 0;
		}
		sucStatus = deleteBST(contactBST,(void*)inputStr,(void**)&dataList); //Use deleteBST instead
		if(sucStatus == 0){
			printf("%s cannot be found in list\n\n",inputStr);
			inOrder(contactBST,printTree);
			printf("\n");
		}
		else{
			printf("%-18s\t%s   %s\n", dataList->name, dataList->phoneNum, dataList->webAddr);
			printf("Change record? [y/n]: ");
			scanf(" %c",&changeRec); //Not working cause its not initialized
			if(changeRec == 'y' || changeRec == 'Y'){
				printf("n. change name\nc. change contact info\n");
				fscanf(stdin," %c",&changeWhich);
				if(changeWhich == 'n'){
					fflush(stdin);
					printf("Enter name: ");
					fgets(inputStr,MAX,stdin);
					strlength = strlen(inputStr);
					if(inputStr[strlength-1] == '\n'){
						inputStr[strlength-1] = 0;
					}
					strcpy(dataList->name,inputStr);
					insertBST(contactBST,(void*)dataList);
				}
				else if(changeWhich == 'c'){
					fflush(stdin);
					printf("Enter phone or hit <enter> for no change: ");
					fgets(inputStr,MAX,stdin);
					strlength = strlen(inputStr);
					if(inputStr[strlength-1] == '\n'){
						inputStr[strlength-1] = 0;
					}
					if(strcmp(inputStr,"\0") != 0){ //Means user wants a change
						strcpy(dataList->phoneNum,inputStr);
					}
					fflush(stdin);
					printf("Enter web address or hit <enter> for no change: ");
					fgets(inputStr,MAX,stdin);
					if(inputStr[strlength-1] == '\n'){
						inputStr[strlength-1] = 0;
					}
					if(strcmp(inputStr,"\0") != 0){ //Means user wants a change
						strcpy(dataList->webAddr,inputStr);
					}
					insertBST(contactBST,(void*)dataList);
				}
			}
		}
	}
}//end lookupName