diff --git a/PA07-1025/BST_Utility.c b/PA07-1025/BST_Utility.c index c883544..a1b1e2f 100644 --- a/PA07-1025/BST_Utility.c +++ b/PA07-1025/BST_Utility.c @@ -42,7 +42,7 @@ StateDataType *clearTree( StateDataType *wkgPtr ) free(wkgPtr); // return NULL since no tree left return NULL; -} //DONE +} /* Name: privateCompareStrings @@ -77,7 +77,7 @@ int privateCompareStrings( const char *leftStr, const char *rightStr ) index = index + 1; } return leftStrLen - rightStrLen; -} //DONE +} /* Name: privateCopyString @@ -104,7 +104,7 @@ void privateCopyString( char *dest, const char *source ) // add a NULL_CHAR once done, characterCount is in the right place for \0 dest[characterCount] = NULL_CHAR; // no return, void -} //DONE +} /* Name: copyTree @@ -133,7 +133,7 @@ StateDataType *copyTree( StateDataType *wkgPtr ) } // return copyPtr return copyPtr; -}//DONE +} /* Name: displayInOrder @@ -163,7 +163,7 @@ void displayInOrder( StateDataType *sdtPtr, int *itemNum ) displayInOrder(sdtPtr->rightChildPtr, itemNum); } // no return, void -} //DONE +} /* Name: displayPostOrder @@ -193,7 +193,7 @@ void displayPostOrder( StateDataType *sdtPtr, int *itemNum ) printf("%2d) %s", *itemNum, displayString); } // no return, void -} //DONE +} /* Name: displayPreOrder @@ -223,7 +223,7 @@ void displayPreOrder( StateDataType *sdtPtr, int *itemNum ) displayPostOrder(sdtPtr->rightChildPtr, itemNum); } // no return, void -} //DONE +} /* Name: privateGetStringLen @@ -246,7 +246,7 @@ int privateGetStringLen( const char *str ) } // return strLen return strLen; -} //DONE +} /* Name: initializeBST @@ -264,7 +264,7 @@ void initializeBST( StateDataType **bstPtr ) // initialize the address of the BST to null *bstPtr = NULL; // no return, void -} //DONE +} /* Name: initializeStateNodeFromData @@ -295,7 +295,7 @@ StateDataType *initializeStateNodeFromData( const char *state, double avgTemp, initNode->highestTemp = highTemp; // return the node return initNode; -} //DONE +} /* Name: initializeStateNodeFromNode @@ -314,7 +314,7 @@ StateDataType *initializeStateNodeFromNode( const StateDataType source ) // return the initializeStateNodeFromData function with correct data return initializeStateNodeFromData(source.state, source.avgTemp, source.lowestTemp, source.highestTemp); -} //DONE +} /* Name: insert @@ -370,7 +370,7 @@ StateDataType *insert( StateDataType *wkgPtr, const StateDataType inData ) } // return working pointer to prev recursion return wkgPtr; -} //DONE +} /* Name: isEmpty @@ -386,7 +386,7 @@ bool isEmpty( StateDataType *sdtPtr) { // return check for rootNode being NULL return sdtPtr == NULL; -} //DONE +} /* Name: removeFromMin @@ -415,7 +415,7 @@ StateDataType *removeFromMin( StateDataType *parentPtr, } // return removed ptr return childPtr; -} //DONE +} /* Name: removeItem @@ -435,7 +435,26 @@ Dependencies: search, initializeStateNodeFromNode, removeItemHelper bool removeItem( StateDataType **rootPtr, StateDataType *removedData, const StateDataType outData ) { - // return false (temp) + StateDataType *searchPtr; + // check to see if the BST is there + if(rootPtr != NULL) + { + // search for the data in the tree + searchPtr = search(*rootPtr, outData); + // check to see if data was found from search to remove + if(searchPtr != NULL) + { + // if found, set the search value to removedData + *removedData = *searchPtr; + // start the recursions down to the remove value + *rootPtr = removeItemHelper(*rootPtr, outData); + // return success with item removal + return true; + } + } + // set removeData to NULL since no value found + removedData = NULL; + // return false since no item found or tree empty/not found return false; } @@ -459,8 +478,67 @@ Notes: No redundant code permitted StateDataType *removeItemHelper( StateDataType *wkgPtr, const StateDataType outData ) { - // return NULL (temp) - return NULL; + // create, initialize result of compare strings + int stringComparator; + stringComparator = privateCompareStrings(outData.state, wkgPtr->state); + StateDataType *removalPtr; + + // check for moving to left + if(stringComparator < 0) + { + // - recurse to left, link to left child + wkgPtr->leftChildPtr = removeItemHelper(wkgPtr->leftChildPtr, outData); + } + // otherwise, check for moving to right + else if(stringComparator > 0) + { + // - recurse to right, link to right child + wkgPtr->rightChildPtr = removeItemHelper(wkgPtr->rightChildPtr, outData); + } + // otherwise, check for left child null + else if(wkgPtr->leftChildPtr == NULL) + { + // - set a temp pointer to the wkg pointer + removalPtr = wkgPtr; + // - set return pointer to its right child + wkgPtr = wkgPtr->rightChildPtr; + // - free temp pointer + free(removalPtr); + } + // otherwise, check for right child null + else if(wkgPtr->rightChildPtr == NULL) + { + // - set a temp pointer to the wkg pointer + removalPtr = wkgPtr; + // - set return pointer to its left child + wkgPtr = wkgPtr->rightChildPtr; + // - free temp pointer + free(removalPtr); + } + // otherwise, check for right child has no left child + else if(wkgPtr->rightChildPtr->leftChildPtr == NULL) + { + // - copy the data from the right child into the current node + setStateNodeData(wkgPtr, *wkgPtr->rightChildPtr); + // - set a temp pointer to the right child + removalPtr = wkgPtr->rightChildPtr; + // - link around the right child + wkgPtr->rightChildPtr = removalPtr->rightChildPtr; + // - free the right child node + free(removalPtr); + } + // otherwise // , assume right child has left child + else + { + // - call removeFromMin, capture the returned node, + removalPtr = removeFromMin(wkgPtr, wkgPtr->rightChildPtr); + // - set the current node from the captured node + setStateNodeData(wkgPtr, *removalPtr); + // - free the captured node (temp ptr) + free(removalPtr); + } + // recommend one return at the bottom of function, will give back wkgPtr + return wkgPtr; } /* @@ -479,43 +557,32 @@ Notes: No redundant code permitted */ StateDataType *search( StateDataType *wkgPtr, const StateDataType searchData ) { - // define space for node - StateDataType *searchNode; - int stringComparator; - // check to see if the working pointer is not null + // define the string comparison number + int stringComparator = privateCompareStrings(searchData.state, + wkgPtr->state); + // check to see if at the end of tree if(wkgPtr != NULL) { - // set string comparator - stringComparator = privateCompareStrings(searchData.state, wkgPtr->state); - // compare the value less than + // check the left side of the tree if(stringComparator < 0) { - // workingPtr -> nextPtr = search(workingPtr->nextPtr, data); - wkgPtr->leftChildPtr = search(wkgPtr->leftChildPtr, searchData); + return search(wkgPtr->leftChildPtr, searchData); } - // compare the value greater than - if(stringComparator > 0) + // check the right side of the tree + else if(stringComparator > 0) { - // workingPtr -> nextPtr = search(workingPtr->nextPtr, data); - wkgPtr->rightChildPtr = search(wkgPtr->rightChildPtr, searchData); + return search(wkgPtr->rightChildPtr, searchData); } - // compare the value equal to - if(stringComparator == 0) + // otherwise, data has been found + else { - searchNode = wkgPtr; - // returns the data, finishes find. - return searchNode; + // return the found value + return wkgPtr; } } - // otherwise, the data was not found, and must fail. - else - { - // return NULL - return NULL; - } - // return working pointer to prev recursion - return wkgPtr; -} //DONE + // otherwise, return null for not found + return NULL; +} /* Name: setStateNodeToEmpty @@ -541,7 +608,7 @@ void setStateNodeToEmpty( StateDataType *node ) node->leftChildPtr = NULL; node->rightChildPtr = NULL; // no return, void -} //DONE +} /* Name: setStateNodeData @@ -567,7 +634,7 @@ void setStateNodeData( StateDataType *destPtr, const StateDataType source ) // destPtr->leftChildPtr = source.leftChildPtr; // destPtr->rightChildPtr = source.rightChildPtr; // no return, void -} //DONE +} /* Name: stateDataToString @@ -589,4 +656,4 @@ void stateDataToString( StateDataType stateData, char str[] ) sprintf(str, "%s/%0.2f/%0.2f/%0.2f\n", stateData.state, stateData.avgTemp, stateData.lowestTemp, stateData.highestTemp); // no return, void -} //DONE \ No newline at end of file +} \ No newline at end of file diff --git a/PA07-1025/PA07_Main b/PA07-1025/PA07_Main index 0d57262..8bc4ed9 100755 Binary files a/PA07-1025/PA07_Main and b/PA07-1025/PA07_Main differ