Completed all but Two Remove Functions.

+- BST_Utility.c
++ PA07_Main
This commit is contained in:
Charles Descamps 2023-10-24 23:37:26 -07:00
parent e7a8428eef
commit f0e9927f32
2 changed files with 259 additions and 36 deletions

View file

@ -1,3 +1,4 @@
// header files
#include "BST_Utility.h" #include "BST_Utility.h"
/* Data Structures /* Data Structures
@ -27,9 +28,21 @@ Dependencies: free, clearTree (recursively)
*/ */
StateDataType *clearTree( StateDataType *wkgPtr ) StateDataType *clearTree( StateDataType *wkgPtr )
{ {
// return NULL (temp) // recurse through the left children
if(wkgPtr->leftChildPtr != NULL)
{
clearTree(wkgPtr->leftChildPtr);
}
// recurse through the right children
if(wkgPtr->rightChildPtr != NULL)
{
clearTree(wkgPtr->rightChildPtr);
}
// otherwise, free the working pointer if at end of getting to bottom tree
free(wkgPtr);
// return NULL since no tree left
return NULL; return NULL;
} } //DONE
/* /*
Name: privateCompareStrings Name: privateCompareStrings
@ -50,9 +63,21 @@ Dependencies: privateGetStringLen
*/ */
int privateCompareStrings( const char *leftStr, const char *rightStr ) int privateCompareStrings( const char *leftStr, const char *rightStr )
{ {
// return 0 (temp) int diff, index = 0;
return 0; int leftStrLen = privateGetStringLen(leftStr);
} int rightStrLen = privateGetStringLen(rightStr);
while(index < leftStrLen && index < rightStrLen)
{
diff = leftStr[index] - rightStr[index];
if(diff != 0)
{
return diff;
}
index = index + 1;
}
return leftStrLen - rightStrLen;
} //DONE
/* /*
Name: privateCopyString Name: privateCopyString
@ -67,8 +92,19 @@ Dependencies: none
*/ */
void privateCopyString( char *dest, const char *source ) void privateCopyString( char *dest, const char *source )
{ {
int characterCount = 0;
// use a while loop, check for !NULL_CHAR
while(source[characterCount] != NULL_CHAR)
{
// copy the character accessed into dest string
dest[characterCount] = source[characterCount];
// iterate the number of characters
characterCount = characterCount + 1;
}
// add a NULL_CHAR once done, characterCount is in the right place for \0
dest[characterCount] = NULL_CHAR;
// no return, void // no return, void
} } //DONE
/* /*
Name: copyTree Name: copyTree
@ -83,9 +119,21 @@ Dependencies: initializeStateNodeFromNode, copyTree (recursively)
*/ */
StateDataType *copyTree( StateDataType *wkgPtr ) StateDataType *copyTree( StateDataType *wkgPtr )
{ {
// return NULL (temp) // make a storage pointer
return NULL; StateDataType *copyPtr = NULL;
} // check for not equal to null
if(wkgPtr != NULL)
{
// create the pointer given data
copyPtr = initializeStateNodeFromNode(*wkgPtr);
// recurse left child
copyPtr->leftChildPtr = copyTree(wkgPtr->leftChildPtr);
// recurse right child
copyPtr->rightChildPtr = copyTree(wkgPtr->rightChildPtr);
}
// return copyPtr
return copyPtr;
}//DONE
/* /*
Name: displayInOrder Name: displayInOrder
@ -99,8 +147,23 @@ Dependencies: stateDataToString, printf
*/ */
void displayInOrder( StateDataType *sdtPtr, int *itemNum ) void displayInOrder( StateDataType *sdtPtr, int *itemNum )
{ {
// create empty string
char displayString[STD_STR_LEN];
if(sdtPtr != NULL)
{
// recurse to left node in tree
displayInOrder(sdtPtr->leftChildPtr, itemNum);
// increment the item
*itemNum = *itemNum + 1;
// setup the string
stateDataToString(*sdtPtr, displayString);
// print out the string
printf("%2d) %s", *itemNum, displayString);
// recurse to right node in tree
displayInOrder(sdtPtr->rightChildPtr, itemNum);
}
// no return, void // no return, void
} } //DONE
/* /*
Name: displayPostOrder Name: displayPostOrder
@ -114,8 +177,23 @@ Dependencies: stateDataToString, printf
*/ */
void displayPostOrder( StateDataType *sdtPtr, int *itemNum ) void displayPostOrder( StateDataType *sdtPtr, int *itemNum )
{ {
// create empty string
char displayString[STD_STR_LEN];
if(sdtPtr != NULL)
{
// recurse to left node in tree
displayPostOrder(sdtPtr->leftChildPtr, itemNum);
// recurse to right node in tree
displayPostOrder(sdtPtr->rightChildPtr, itemNum);
// increment the item
*itemNum = *itemNum + 1;
// setup the string
stateDataToString(*sdtPtr, displayString);
// print out the string
printf("%2d) %s", *itemNum, displayString);
}
// no return, void // no return, void
} } //DONE
/* /*
Name: displayPreOrder Name: displayPreOrder
@ -129,8 +207,23 @@ Dependencies: stateDataToString, printf
*/ */
void displayPreOrder( StateDataType *sdtPtr, int *itemNum ) void displayPreOrder( StateDataType *sdtPtr, int *itemNum )
{ {
// create empty string
char displayString[STD_STR_LEN];
if(sdtPtr != NULL)
{
// increment the item
*itemNum = *itemNum + 1;
// setup the string
stateDataToString(*sdtPtr, displayString);
// print out the string
printf("%2d) %s", *itemNum, displayString);
// recurse to left node in tree
displayPostOrder(sdtPtr->leftChildPtr, itemNum);
// recurse to right node in tree
displayPostOrder(sdtPtr->rightChildPtr, itemNum);
}
// no return, void // no return, void
} } //DONE
/* /*
Name: privateGetStringLen Name: privateGetStringLen
@ -144,9 +237,16 @@ Dependencies: none
*/ */
int privateGetStringLen( const char *str ) int privateGetStringLen( const char *str )
{ {
// return 0 (temp) int strLen = 0;
return 0; // while through string until hit null.
} while(str[strLen] != NULL_CHAR)
{
// increment the length string iterator
strLen = strLen + 1;
}
// return strLen
return strLen;
} //DONE
/* /*
Name: initializeBST Name: initializeBST
@ -161,8 +261,10 @@ Dependencies: none
*/ */
void initializeBST( StateDataType **bstPtr ) void initializeBST( StateDataType **bstPtr )
{ {
// initialize the address of the BST to null
*bstPtr = NULL;
// no return, void // no return, void
} } //DONE
/* /*
Name: initializeStateNodeFromData Name: initializeStateNodeFromData
@ -181,9 +283,19 @@ Dependencies: malloc, sizeof, privateCopyString
StateDataType *initializeStateNodeFromData( const char *state, double avgTemp, StateDataType *initializeStateNodeFromData( const char *state, double avgTemp,
double lowTemp, double highTemp ) double lowTemp, double highTemp )
{ {
// return NULL (temp) // declare node
return NULL; StateDataType *initNode;
} // create the node using malloc
initNode = malloc(sizeof(StateDataType));
// copy the state into the node
privateCopyString(initNode->state, state);
// copy the avgTemp, lowTemp, and highTemp into node
initNode->avgTemp = avgTemp;
initNode->lowestTemp = lowTemp;
initNode->highestTemp = highTemp;
// return the node
return initNode;
} //DONE
/* /*
Name: initializeStateNodeFromNode Name: initializeStateNodeFromNode
@ -199,9 +311,10 @@ Notes: one line of code
*/ */
StateDataType *initializeStateNodeFromNode( const StateDataType source ) StateDataType *initializeStateNodeFromNode( const StateDataType source )
{ {
// return NULL (temp) // return the initializeStateNodeFromData function with correct data
return NULL; return initializeStateNodeFromData(source.state, source.avgTemp,
} source.lowestTemp, source.highestTemp);
} //DONE
/* /*
Name: insert Name: insert
@ -220,9 +333,44 @@ Notes: No redundant code permitted
*/ */
StateDataType *insert( StateDataType *wkgPtr, const StateDataType inData ) StateDataType *insert( StateDataType *wkgPtr, const StateDataType inData )
{ {
// return NULL (temp) // define space for node
return NULL; StateDataType *insertNode;
} int stringComparator;
// check to see if the working pointer is not null
if(wkgPtr != NULL)
{
// set string comparator
stringComparator = privateCompareStrings(inData.state, wkgPtr->state);
// compare the value less than
if(stringComparator < 0)
{
// workingPtr -> nextPtr = insert(workingPtr->nextPtr, data);
wkgPtr->leftChildPtr = insert(wkgPtr->leftChildPtr, inData);
}
// compare the value greater than
if(stringComparator > 0)
{
// workingPtr -> nextPtr = insert(workingPtr->nextPtr, data);
wkgPtr->rightChildPtr = insert(wkgPtr->rightChildPtr, inData);
}
// compare the value equal to
if(stringComparator == 0)
{
// replaces the data (using setStateNodeData?)
setStateNodeData(wkgPtr, inData);
}
}
// otherwise, create node
else
{
// create the node using function
insertNode = initializeStateNodeFromNode(inData);
// return created node
return insertNode;
}
// return working pointer to prev recursion
return wkgPtr;
} //DONE
/* /*
Name: isEmpty Name: isEmpty
@ -236,8 +384,9 @@ Dependencies: none
*/ */
bool isEmpty( StateDataType *sdtPtr) bool isEmpty( StateDataType *sdtPtr)
{ {
// return false (temp) // return check for rootNode being NULL
} return sdtPtr == NULL;
} //DONE
/* /*
Name: removeFromMin Name: removeFromMin
@ -253,9 +402,20 @@ Dependencies: removeFromMin (recursively)
StateDataType *removeFromMin( StateDataType *parentPtr, StateDataType *removeFromMin( StateDataType *parentPtr,
StateDataType *childPtr ) StateDataType *childPtr )
{ {
// return NULL (temp) // check to see if the left child pointer is null
return NULL; if(childPtr->leftChildPtr != NULL)
} {
// keep going left until end
childPtr->leftChildPtr = removeFromMin(childPtr, childPtr->leftChildPtr);
}
else
{
// once found, delink between the parent and child
parentPtr->leftChildPtr = childPtr->rightChildPtr;
}
// return removed ptr
return childPtr;
} //DONE
/* /*
Name: removeItem Name: removeItem
@ -319,9 +479,43 @@ Notes: No redundant code permitted
*/ */
StateDataType *search( StateDataType *wkgPtr, const StateDataType searchData ) StateDataType *search( StateDataType *wkgPtr, const StateDataType searchData )
{ {
// return NULL (temp) // define space for node
StateDataType *searchNode;
int stringComparator;
// check to see if the working pointer is not null
if(wkgPtr != NULL)
{
// set string comparator
stringComparator = privateCompareStrings(searchData.state, wkgPtr->state);
// compare the value less than
if(stringComparator < 0)
{
// workingPtr -> nextPtr = search(workingPtr->nextPtr, data);
wkgPtr->leftChildPtr = search(wkgPtr->leftChildPtr, searchData);
}
// compare the value greater than
if(stringComparator > 0)
{
// workingPtr -> nextPtr = search(workingPtr->nextPtr, data);
wkgPtr->rightChildPtr = search(wkgPtr->rightChildPtr, searchData);
}
// compare the value equal to
if(stringComparator == 0)
{
searchNode = wkgPtr;
// returns the data, finishes find.
return searchNode;
}
}
// otherwise, the data was not found, and must fail.
else
{
// return NULL
return NULL; return NULL;
} }
// return working pointer to prev recursion
return wkgPtr;
} //DONE
/* /*
Name: setStateNodeToEmpty Name: setStateNodeToEmpty
@ -335,8 +529,19 @@ Dependencies: none
*/ */
void setStateNodeToEmpty( StateDataType *node ) void setStateNodeToEmpty( StateDataType *node )
{ {
// set the state to null character
node->state[0] = '\0';
// set the average temp to 0
node->avgTemp = 0;
// set the lowest temp to 0
node->lowestTemp = 0;
// set the highest temp to 0
node->highestTemp = 0;
// (not sure if messing with pointers)
node->leftChildPtr = NULL;
node->rightChildPtr = NULL;
// no return, void // no return, void
} } //DONE
/* /*
Name: setStateNodeData Name: setStateNodeData
@ -350,8 +555,19 @@ Dependencies: copyString
*/ */
void setStateNodeData( StateDataType *destPtr, const StateDataType source ) void setStateNodeData( StateDataType *destPtr, const StateDataType source )
{ {
// copy the state from source to dest with privateCopyString
privateCopyString(destPtr->state, source.state);
// copy the average temp from source to dest
destPtr->avgTemp = source.avgTemp;
// copy the lowest temp from source to dest
destPtr->lowestTemp = source.lowestTemp;
// copy the highest temp from source to dest
destPtr->highestTemp = source.highestTemp;
// (not sure if messing with pointers)
// destPtr->leftChildPtr = source.leftChildPtr;
// destPtr->rightChildPtr = source.rightChildPtr;
// no return, void // no return, void
} } //DONE
/* /*
Name: stateDataToString Name: stateDataToString
@ -365,5 +581,12 @@ Dependencies: sprintf
*/ */
void stateDataToString( StateDataType stateData, char str[] ) void stateDataToString( StateDataType stateData, char str[] )
{ {
// use sprintf to load the state name into str
// use sprintf to load the average temp into str
// use sprintf to load the lowest temp into str
// use sprintf to load the highest temp into str
// this is done all at once to avoid many lines
sprintf(str, "%s/%0.2f/%0.2f/%0.2f\n", stateData.state, stateData.avgTemp,
stateData.lowestTemp, stateData.highestTemp);
// no return, void // no return, void
} } //DONE

BIN
PA07-1025/PA07_Main Executable file

Binary file not shown.