"Complete" PA08 for processing.
This commit is contained in:
parent
9c24cee2df
commit
ba683c4f44
2 changed files with 34 additions and 20 deletions
Binary file not shown.
|
|
@ -340,22 +340,23 @@ RedBlackNodeType *insert(RedBlackNodeType *treeRoot, char inData)
|
|||
{
|
||||
// use insertHelper to get to where the data needs to go
|
||||
insertedNode = insertHelper(treeRoot, inData);
|
||||
// print out what data is being added, tell user being resolved
|
||||
printf("\nInserting %c and resolving\n", inData);
|
||||
// use resolveRBT to get the tree balanced correctly
|
||||
resolveRBT(&treeRoot, insertedNode);
|
||||
if(insertedNode != NULL)
|
||||
{
|
||||
// print out what data is being added, tell user being resolved
|
||||
printf("\nInserting %c and resolving\n", inData);
|
||||
// use resolveRBT to get the tree balanced correctly
|
||||
resolveRBT(&treeRoot, insertedNode);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// if inserting the root node
|
||||
// set the root with createNodeFromData, node is black.
|
||||
treeRoot = createNodeFromData(inData, BLACK);
|
||||
// tell the user inserting the data
|
||||
printf("\nInserting %c and resolving\n", inData);
|
||||
// tell the user that the root was set, and is black.
|
||||
printf(" - Root color set to black\n");
|
||||
/* AN ALTERNATIVE COULD BE TO INSTEAD
|
||||
USE THE RESOLVE FUNCTION INSTEAD OF DOING IT BY HAND*/
|
||||
printf(" - Root color set to black\n");
|
||||
// set the root with createNodeFromData, node is black.
|
||||
return createNodeFromData(inData, BLACK);
|
||||
}
|
||||
// return treeRoot
|
||||
return treeRoot;
|
||||
|
|
@ -446,7 +447,7 @@ Dependencies: printf, rotateLeft, rotateRight, swapColors,
|
|||
*/
|
||||
void resolveRBT(RedBlackNodeType **treeRoot, RedBlackNodeType *wkgPtr)
|
||||
{
|
||||
printf("\nAttempting to Resolve RBT\n");
|
||||
// printf("\nAttempting to Resolve RBT\n");
|
||||
// declare parPtr, grndParPtr, unclePtr, grtGrndPtr
|
||||
RedBlackNodeType *wkgParentPtr;
|
||||
RedBlackNodeType *wkgGrandParentPtr;
|
||||
|
|
@ -455,19 +456,19 @@ void resolveRBT(RedBlackNodeType **treeRoot, RedBlackNodeType *wkgPtr)
|
|||
// check for parent and grandparent exists
|
||||
if(wkgPtr->parentPtr != NULL && wkgPtr->parentPtr->parentPtr != NULL)
|
||||
{
|
||||
printf("\nStarting resolution process.\n");
|
||||
// printf("\nStarting resolution process.\n");
|
||||
// set all my relatives, will need to know which child I am
|
||||
wkgParentPtr = wkgPtr->parentPtr;
|
||||
wkgGrandParentPtr = wkgParentPtr->parentPtr;
|
||||
if (wkgParentPtr->leftChildPtr == wkgPtr)
|
||||
if (wkgGrandParentPtr->leftChildPtr == wkgPtr)
|
||||
{
|
||||
// set the uncle pointer to the parent's right child
|
||||
wkgUnclePtr = wkgParentPtr->rightChildPtr;
|
||||
// set the uncle pointer to the grandparent's right child
|
||||
wkgUnclePtr = wkgGrandParentPtr->rightChildPtr;
|
||||
}
|
||||
else
|
||||
{
|
||||
// set the uncle pointer to the parent's left child
|
||||
wkgUnclePtr = wkgParentPtr->leftChildPtr;
|
||||
// set the uncle pointer to the grandparent's left child
|
||||
wkgUnclePtr = wkgGrandParentPtr->leftChildPtr;
|
||||
}
|
||||
wkgGreatGrandParentPtr = wkgGrandParentPtr->parentPtr;
|
||||
// check for red uncle
|
||||
|
|
@ -478,6 +479,9 @@ void resolveRBT(RedBlackNodeType **treeRoot, RedBlackNodeType *wkgPtr)
|
|||
wkgUnclePtr->color = BLACK;
|
||||
// change grandparent to red
|
||||
wkgGrandParentPtr->color = RED;
|
||||
// print out parent and uncle are red.
|
||||
printf(" - Parent is red\n");
|
||||
printf(" - Uncle is red\n");
|
||||
// recurse with the grandparent
|
||||
resolveRBT(treeRoot, wkgGrandParentPtr);
|
||||
}
|
||||
|
|
@ -490,17 +494,22 @@ void resolveRBT(RedBlackNodeType **treeRoot, RedBlackNodeType *wkgPtr)
|
|||
// check for child is left of parent (Left Left case)
|
||||
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)
|
||||
rotateRight(wkgGrandParentPtr, wkgParentPtr);
|
||||
// link the parent to grandparent's parent
|
||||
wkgPtr->parentPtr = wkgGrandParentPtr->parentPtr;
|
||||
// swap colors called with parent and grandparent
|
||||
swapColors(wkgParentPtr, wkgGrandParentPtr);
|
||||
}
|
||||
// check for child is right of parent (Left Right case)
|
||||
else
|
||||
{
|
||||
printf("\nLR Case, rotate right, recurse\n");
|
||||
// printf("\nLR Case, rotate right, recurse\n");
|
||||
// use rotate right with parent(old) and createdNode(new)
|
||||
rotateRight(wkgParentPtr, wkgPtr);
|
||||
// call resolveRBT with parent as createdNode (will treat as LLC)
|
||||
resolveRBT(treeRoot, wkgParentPtr);
|
||||
}
|
||||
}
|
||||
// check for parent is right of grandparent
|
||||
|
|
@ -509,17 +518,22 @@ void resolveRBT(RedBlackNodeType **treeRoot, RedBlackNodeType *wkgPtr)
|
|||
// check for child is right of parent (Right Right case)
|
||||
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)
|
||||
rotateLeft(wkgGrandParentPtr, wkgParentPtr);
|
||||
// link the parent to grandparent's parent
|
||||
wkgPtr->parentPtr = wkgGrandParentPtr->parentPtr;
|
||||
// swap colors called with parent and grandparent
|
||||
swapColors(wkgParentPtr, wkgGrandParentPtr);
|
||||
}
|
||||
// check for child is left of parent (Right Left case)
|
||||
else
|
||||
{
|
||||
printf("\nRL Case, rotate left, recurse\n");
|
||||
// printf("\nRL Case, rotate left, recurse\n");
|
||||
// use rotate left with parent(old) and createdNode(new)
|
||||
rotateLeft(wkgParentPtr, wkgPtr);
|
||||
// call resolveRBT with parent as createdNode (will treat as RRC)
|
||||
resolveRBT(treeRoot, wkgParentPtr);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue