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"
/* Data Structures
@ -27,9 +28,21 @@ Dependencies: free, clearTree (recursively)
*/
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;
}
} //DONE
/*
Name: privateCompareStrings
@ -50,9 +63,21 @@ Dependencies: privateGetStringLen
*/
int privateCompareStrings( const char *leftStr, const char *rightStr )
{
// return 0 (temp)
return 0;
}
int diff, index = 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
@ -67,8 +92,19 @@ Dependencies: none
*/
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
}
} //DONE
/*
Name: copyTree
@ -83,9 +119,21 @@ Dependencies: initializeStateNodeFromNode, copyTree (recursively)
*/
StateDataType *copyTree( StateDataType *wkgPtr )
{
// return NULL (temp)
return NULL;
}
// make a storage pointer
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
@ -99,8 +147,23 @@ Dependencies: stateDataToString, printf
*/
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
}
} //DONE
/*
Name: displayPostOrder
@ -114,8 +177,23 @@ Dependencies: stateDataToString, printf
*/
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
}
} //DONE
/*
Name: displayPreOrder
@ -129,8 +207,23 @@ Dependencies: stateDataToString, printf
*/
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
}
} //DONE
/*
Name: privateGetStringLen
@ -144,9 +237,16 @@ Dependencies: none
*/
int privateGetStringLen( const char *str )
{
// return 0 (temp)
return 0;
}
int strLen = 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
@ -161,8 +261,10 @@ Dependencies: none
*/
void initializeBST( StateDataType **bstPtr )
{
// initialize the address of the BST to null
*bstPtr = NULL;
// no return, void
}
} //DONE
/*
Name: initializeStateNodeFromData
@ -181,9 +283,19 @@ Dependencies: malloc, sizeof, privateCopyString
StateDataType *initializeStateNodeFromData( const char *state, double avgTemp,
double lowTemp, double highTemp )
{
// return NULL (temp)
return NULL;
}
// declare node
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
@ -199,9 +311,10 @@ Notes: one line of code
*/
StateDataType *initializeStateNodeFromNode( const StateDataType source )
{
// return NULL (temp)
return NULL;
}
// return the initializeStateNodeFromData function with correct data
return initializeStateNodeFromData(source.state, source.avgTemp,
source.lowestTemp, source.highestTemp);
} //DONE
/*
Name: insert
@ -220,9 +333,44 @@ Notes: No redundant code permitted
*/
StateDataType *insert( StateDataType *wkgPtr, const StateDataType inData )
{
// return NULL (temp)
return NULL;
}
// define space for node
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
@ -236,8 +384,9 @@ Dependencies: none
*/
bool isEmpty( StateDataType *sdtPtr)
{
// return false (temp)
}
// return check for rootNode being NULL
return sdtPtr == NULL;
} //DONE
/*
Name: removeFromMin
@ -253,9 +402,20 @@ Dependencies: removeFromMin (recursively)
StateDataType *removeFromMin( StateDataType *parentPtr,
StateDataType *childPtr )
{
// return NULL (temp)
return NULL;
}
// check to see if the left child pointer is 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
@ -319,9 +479,43 @@ Notes: No redundant code permitted
*/
StateDataType *search( StateDataType *wkgPtr, const StateDataType searchData )
{
// return NULL (temp)
return NULL;
}
// 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 working pointer to prev recursion
return wkgPtr;
} //DONE
/*
Name: setStateNodeToEmpty
@ -335,8 +529,19 @@ Dependencies: none
*/
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
}
} //DONE
/*
Name: setStateNodeData
@ -350,8 +555,19 @@ Dependencies: copyString
*/
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
}
} //DONE
/*
Name: stateDataToString
@ -365,5 +581,12 @@ Dependencies: sprintf
*/
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
}
} //DONE