Fixed Leverington Issues, resolve still no worky
+- PA08_Main +- RedBlackTreeUtility.c
This commit is contained in:
parent
c2068d6874
commit
9c24cee2df
2 changed files with 23 additions and 22 deletions
Binary file not shown.
|
|
@ -37,14 +37,9 @@ Dependencies: free
|
||||||
*/
|
*/
|
||||||
RedBlackNodeType *clearTree(RedBlackNodeType *localPtr)
|
RedBlackNodeType *clearTree(RedBlackNodeType *localPtr)
|
||||||
{
|
{
|
||||||
// recurse through the left children
|
if(localPtr->leftChildPtr != NULL && localPtr->rightChildPtr != NULL)
|
||||||
if (localPtr->leftChildPtr != NULL)
|
|
||||||
{
|
{
|
||||||
clearTree(localPtr->leftChildPtr);
|
clearTree(localPtr->leftChildPtr);
|
||||||
}
|
|
||||||
// recurse through the right children
|
|
||||||
if (localPtr->rightChildPtr != NULL)
|
|
||||||
{
|
|
||||||
clearTree(localPtr->rightChildPtr);
|
clearTree(localPtr->rightChildPtr);
|
||||||
}
|
}
|
||||||
// otherwise, free the local pointer if at end of getting to bottom tree
|
// otherwise, free the local pointer if at end of getting to bottom tree
|
||||||
|
|
@ -99,7 +94,7 @@ RedBlackNodeType *createNodeFromData(char data, int nodeColor)
|
||||||
// define a new node
|
// define a new node
|
||||||
RedBlackNodeType *newNode;
|
RedBlackNodeType *newNode;
|
||||||
// allocate memory for the newNode (RedBlackNodeType)
|
// allocate memory for the newNode (RedBlackNodeType)
|
||||||
newNode = malloc(sizeof(RedBlackNodeType));
|
newNode = (RedBlackNodeType*)malloc(sizeof(RedBlackNodeType));
|
||||||
// define the color as the new node's color
|
// define the color as the new node's color
|
||||||
newNode->color = nodeColor;
|
newNode->color = nodeColor;
|
||||||
// define the data of the new node to be the input data
|
// define the data of the new node to be the input data
|
||||||
|
|
@ -290,6 +285,7 @@ Dependencies: getMax
|
||||||
*/
|
*/
|
||||||
int findTreeHeight(RedBlackNodeType *localPtr)
|
int findTreeHeight(RedBlackNodeType *localPtr)
|
||||||
{
|
{
|
||||||
|
int leftTreeSize, rightTreeSize;
|
||||||
// check to see if at null
|
// check to see if at null
|
||||||
if(localPtr == NULL)
|
if(localPtr == NULL)
|
||||||
{
|
{
|
||||||
|
|
@ -299,8 +295,8 @@ int findTreeHeight(RedBlackNodeType *localPtr)
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// traverse the left and right subtrees
|
// traverse the left and right subtrees
|
||||||
int leftTreeSize = findTreeHeight(localPtr->leftChildPtr);
|
leftTreeSize = findTreeHeight(localPtr->leftChildPtr);
|
||||||
int rightTreeSize = findTreeHeight(localPtr->rightChildPtr);
|
rightTreeSize = findTreeHeight(localPtr->rightChildPtr);
|
||||||
// return the max of the subtrees, add one for the local node
|
// return the max of the subtrees, add one for the local node
|
||||||
return getMax(leftTreeSize, rightTreeSize) + 1;
|
return getMax(leftTreeSize, rightTreeSize) + 1;
|
||||||
}
|
}
|
||||||
|
|
@ -398,7 +394,6 @@ RedBlackNodeType *insertHelper(RedBlackNodeType *wkgPtr, char inData)
|
||||||
// return recursion
|
// return recursion
|
||||||
return insertHelper(wkgPtr->leftChildPtr, inData);
|
return insertHelper(wkgPtr->leftChildPtr, inData);
|
||||||
}
|
}
|
||||||
|
|
||||||
// otherwise, check for moving right (diffChar > 0)
|
// otherwise, check for moving right (diffChar > 0)
|
||||||
else if(diffChar > 0)
|
else if(diffChar > 0)
|
||||||
{
|
{
|
||||||
|
|
@ -413,14 +408,10 @@ RedBlackNodeType *insertHelper(RedBlackNodeType *wkgPtr, char inData)
|
||||||
// return recursion
|
// return recursion
|
||||||
return insertHelper(wkgPtr->rightChildPtr, inData);
|
return insertHelper(wkgPtr->rightChildPtr, inData);
|
||||||
}
|
}
|
||||||
|
|
||||||
// otherwise, there is a duplicate
|
// otherwise, there is a duplicate
|
||||||
else
|
|
||||||
{
|
|
||||||
// return NULL
|
// return NULL
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Name: isEmpty
|
Name: isEmpty
|
||||||
|
|
@ -455,6 +446,7 @@ Dependencies: printf, rotateLeft, rotateRight, swapColors,
|
||||||
*/
|
*/
|
||||||
void resolveRBT(RedBlackNodeType **treeRoot, RedBlackNodeType *wkgPtr)
|
void resolveRBT(RedBlackNodeType **treeRoot, RedBlackNodeType *wkgPtr)
|
||||||
{
|
{
|
||||||
|
printf("\nAttempting to Resolve RBT\n");
|
||||||
// declare parPtr, grndParPtr, unclePtr, grtGrndPtr
|
// declare parPtr, grndParPtr, unclePtr, grtGrndPtr
|
||||||
RedBlackNodeType *wkgParentPtr;
|
RedBlackNodeType *wkgParentPtr;
|
||||||
RedBlackNodeType *wkgGrandParentPtr;
|
RedBlackNodeType *wkgGrandParentPtr;
|
||||||
|
|
@ -463,10 +455,11 @@ void resolveRBT(RedBlackNodeType **treeRoot, RedBlackNodeType *wkgPtr)
|
||||||
// check for parent and grandparent exists
|
// check for parent and grandparent exists
|
||||||
if(wkgPtr->parentPtr != NULL && wkgPtr->parentPtr->parentPtr != NULL)
|
if(wkgPtr->parentPtr != NULL && wkgPtr->parentPtr->parentPtr != NULL)
|
||||||
{
|
{
|
||||||
|
printf("\nStarting resolution process.\n");
|
||||||
// set all my relatives, will need to know which child I am
|
// set all my relatives, will need to know which child I am
|
||||||
wkgParentPtr = wkgPtr->parentPtr;
|
wkgParentPtr = wkgPtr->parentPtr;
|
||||||
wkgGrandParentPtr = wkgParentPtr->parentPtr;
|
wkgGrandParentPtr = wkgParentPtr->parentPtr;
|
||||||
if (wkgParentPtr->leftChildPtr->data == wkgPtr->data)
|
if (wkgParentPtr->leftChildPtr == wkgPtr)
|
||||||
{
|
{
|
||||||
// set the uncle pointer to the parent's right child
|
// set the uncle pointer to the parent's right child
|
||||||
wkgUnclePtr = wkgParentPtr->rightChildPtr;
|
wkgUnclePtr = wkgParentPtr->rightChildPtr;
|
||||||
|
|
@ -492,10 +485,10 @@ void resolveRBT(RedBlackNodeType **treeRoot, RedBlackNodeType *wkgPtr)
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// check for parent is left of grandparent
|
// check for parent is left of grandparent
|
||||||
if(wkgGrandParentPtr->leftChildPtr->data == wkgParentPtr->data)
|
if(wkgGrandParentPtr->leftChildPtr == wkgParentPtr)
|
||||||
{
|
{
|
||||||
// check for child is left of parent (Left Left case)
|
// check for child is left of parent (Left Left case)
|
||||||
if(wkgParentPtr->leftChildPtr->data == wkgPtr->data)
|
if(wkgParentPtr->leftChildPtr == wkgPtr)
|
||||||
{
|
{
|
||||||
printf("\nLL Case, rotate right\n");
|
printf("\nLL Case, rotate right\n");
|
||||||
// use rotate right with grandparent(old) and parent(new)
|
// use rotate right with grandparent(old) and parent(new)
|
||||||
|
|
@ -503,7 +496,7 @@ void resolveRBT(RedBlackNodeType **treeRoot, RedBlackNodeType *wkgPtr)
|
||||||
// swap colors called with parent and grandparent
|
// swap colors called with parent and grandparent
|
||||||
}
|
}
|
||||||
// check for child is right of parent (Left Right case)
|
// check for child is right of parent (Left Right case)
|
||||||
if(wkgParentPtr->rightChildPtr->data == wkgPtr->data)
|
else
|
||||||
{
|
{
|
||||||
printf("\nLR Case, rotate right, recurse\n");
|
printf("\nLR Case, rotate right, recurse\n");
|
||||||
// use rotate right with parent(old) and createdNode(new)
|
// use rotate right with parent(old) and createdNode(new)
|
||||||
|
|
@ -511,10 +504,10 @@ void resolveRBT(RedBlackNodeType **treeRoot, RedBlackNodeType *wkgPtr)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// check for parent is right of grandparent
|
// check for parent is right of grandparent
|
||||||
if(wkgGrandParentPtr->rightChildPtr->data == wkgParentPtr->data)
|
else
|
||||||
{
|
{
|
||||||
// check for child is right of parent (Right Right case)
|
// check for child is right of parent (Right Right case)
|
||||||
if(wkgParentPtr->rightChildPtr->data == wkgPtr->data)
|
if(wkgParentPtr->rightChildPtr == wkgPtr)
|
||||||
{
|
{
|
||||||
printf("\nRR Case, rotate left\n");
|
printf("\nRR Case, rotate left\n");
|
||||||
// use rotate left with grandparent(old) and parent(new)
|
// use rotate left with grandparent(old) and parent(new)
|
||||||
|
|
@ -522,7 +515,7 @@ void resolveRBT(RedBlackNodeType **treeRoot, RedBlackNodeType *wkgPtr)
|
||||||
// swap colors called with parent and grandparent
|
// swap colors called with parent and grandparent
|
||||||
}
|
}
|
||||||
// check for child is left of parent (Right Left case)
|
// check for child is left of parent (Right Left case)
|
||||||
if(wkgParentPtr->leftChildPtr->data == wkgPtr->data)
|
else
|
||||||
{
|
{
|
||||||
printf("\nRL Case, rotate left, recurse\n");
|
printf("\nRL Case, rotate left, recurse\n");
|
||||||
// use rotate left with parent(old) and createdNode(new)
|
// use rotate left with parent(old) and createdNode(new)
|
||||||
|
|
@ -538,6 +531,14 @@ void resolveRBT(RedBlackNodeType **treeRoot, RedBlackNodeType *wkgPtr)
|
||||||
// paint it black
|
// paint it black
|
||||||
wkgPtr->color = BLACK;
|
wkgPtr->color = BLACK;
|
||||||
}
|
}
|
||||||
|
// print out the structure, version data
|
||||||
|
displayTreeStructure(*treeRoot, NODE_DATA);
|
||||||
|
// print the thin divider
|
||||||
|
displayDivider(*treeRoot, THIN_DIVIDER);
|
||||||
|
// print out the structure, version color
|
||||||
|
displayTreeStructure(*treeRoot, NODE_COLOR);
|
||||||
|
// print the thick divider
|
||||||
|
displayDivider(*treeRoot, THICK_DIVIDER);
|
||||||
// no return, void.
|
// no return, void.
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue