"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
|
// use insertHelper to get to where the data needs to go
|
||||||
insertedNode = insertHelper(treeRoot, inData);
|
insertedNode = insertHelper(treeRoot, inData);
|
||||||
// print out what data is being added, tell user being resolved
|
if(insertedNode != NULL)
|
||||||
printf("\nInserting %c and resolving\n", inData);
|
{
|
||||||
// use resolveRBT to get the tree balanced correctly
|
// print out what data is being added, tell user being resolved
|
||||||
resolveRBT(&treeRoot, insertedNode);
|
printf("\nInserting %c and resolving\n", inData);
|
||||||
|
// use resolveRBT to get the tree balanced correctly
|
||||||
|
resolveRBT(&treeRoot, insertedNode);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// if inserting the root node
|
// if inserting the root node
|
||||||
// set the root with createNodeFromData, node is black.
|
|
||||||
treeRoot = createNodeFromData(inData, BLACK);
|
|
||||||
// tell the user inserting the data
|
// tell the user inserting the data
|
||||||
printf("\nInserting %c and resolving\n", inData);
|
printf("\nInserting %c and resolving\n", inData);
|
||||||
// tell the user that the root was set, and is black.
|
// tell the user that the root was set, and is black.
|
||||||
printf(" - Root color set to black\n");
|
printf(" - Root color set to black\n");
|
||||||
/* AN ALTERNATIVE COULD BE TO INSTEAD
|
// set the root with createNodeFromData, node is black.
|
||||||
USE THE RESOLVE FUNCTION INSTEAD OF DOING IT BY HAND*/
|
return createNodeFromData(inData, BLACK);
|
||||||
}
|
}
|
||||||
// return treeRoot
|
// return treeRoot
|
||||||
return treeRoot;
|
return treeRoot;
|
||||||
|
|
@ -446,7 +447,7 @@ Dependencies: printf, rotateLeft, rotateRight, swapColors,
|
||||||
*/
|
*/
|
||||||
void resolveRBT(RedBlackNodeType **treeRoot, RedBlackNodeType *wkgPtr)
|
void resolveRBT(RedBlackNodeType **treeRoot, RedBlackNodeType *wkgPtr)
|
||||||
{
|
{
|
||||||
printf("\nAttempting to Resolve RBT\n");
|
// printf("\nAttempting to Resolve RBT\n");
|
||||||
// declare parPtr, grndParPtr, unclePtr, grtGrndPtr
|
// declare parPtr, grndParPtr, unclePtr, grtGrndPtr
|
||||||
RedBlackNodeType *wkgParentPtr;
|
RedBlackNodeType *wkgParentPtr;
|
||||||
RedBlackNodeType *wkgGrandParentPtr;
|
RedBlackNodeType *wkgGrandParentPtr;
|
||||||
|
|
@ -455,19 +456,19 @@ 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");
|
// 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 == wkgPtr)
|
if (wkgGrandParentPtr->leftChildPtr == wkgPtr)
|
||||||
{
|
{
|
||||||
// set the uncle pointer to the parent's right child
|
// set the uncle pointer to the grandparent's right child
|
||||||
wkgUnclePtr = wkgParentPtr->rightChildPtr;
|
wkgUnclePtr = wkgGrandParentPtr->rightChildPtr;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// set the uncle pointer to the parent's left child
|
// set the uncle pointer to the grandparent's left child
|
||||||
wkgUnclePtr = wkgParentPtr->leftChildPtr;
|
wkgUnclePtr = wkgGrandParentPtr->leftChildPtr;
|
||||||
}
|
}
|
||||||
wkgGreatGrandParentPtr = wkgGrandParentPtr->parentPtr;
|
wkgGreatGrandParentPtr = wkgGrandParentPtr->parentPtr;
|
||||||
// check for red uncle
|
// check for red uncle
|
||||||
|
|
@ -478,6 +479,9 @@ void resolveRBT(RedBlackNodeType **treeRoot, RedBlackNodeType *wkgPtr)
|
||||||
wkgUnclePtr->color = BLACK;
|
wkgUnclePtr->color = BLACK;
|
||||||
// change grandparent to red
|
// change grandparent to red
|
||||||
wkgGrandParentPtr->color = 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
|
// recurse with the grandparent
|
||||||
resolveRBT(treeRoot, wkgGrandParentPtr);
|
resolveRBT(treeRoot, wkgGrandParentPtr);
|
||||||
}
|
}
|
||||||
|
|
@ -490,17 +494,22 @@ void resolveRBT(RedBlackNodeType **treeRoot, RedBlackNodeType *wkgPtr)
|
||||||
// check for child is left of parent (Left Left case)
|
// check for child is left of parent (Left Left case)
|
||||||
if(wkgParentPtr->leftChildPtr == wkgPtr)
|
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)
|
||||||
|
rotateRight(wkgGrandParentPtr, wkgParentPtr);
|
||||||
// link the parent to grandparent's parent
|
// link the parent to grandparent's parent
|
||||||
|
wkgPtr->parentPtr = wkgGrandParentPtr->parentPtr;
|
||||||
// swap colors called with parent and grandparent
|
// swap colors called with parent and grandparent
|
||||||
|
swapColors(wkgParentPtr, wkgGrandParentPtr);
|
||||||
}
|
}
|
||||||
// check for child is right of parent (Left Right case)
|
// check for child is right of parent (Left Right case)
|
||||||
else
|
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)
|
||||||
|
rotateRight(wkgParentPtr, wkgPtr);
|
||||||
// call resolveRBT with parent as createdNode (will treat as LLC)
|
// call resolveRBT with parent as createdNode (will treat as LLC)
|
||||||
|
resolveRBT(treeRoot, wkgParentPtr);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// check for parent is right of grandparent
|
// 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)
|
// check for child is right of parent (Right Right case)
|
||||||
if(wkgParentPtr->rightChildPtr == wkgPtr)
|
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)
|
||||||
|
rotateLeft(wkgGrandParentPtr, wkgParentPtr);
|
||||||
// link the parent to grandparent's parent
|
// link the parent to grandparent's parent
|
||||||
|
wkgPtr->parentPtr = wkgGrandParentPtr->parentPtr;
|
||||||
// swap colors called with parent and grandparent
|
// swap colors called with parent and grandparent
|
||||||
|
swapColors(wkgParentPtr, wkgGrandParentPtr);
|
||||||
}
|
}
|
||||||
// check for child is left of parent (Right Left case)
|
// check for child is left of parent (Right Left case)
|
||||||
else
|
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)
|
||||||
|
rotateLeft(wkgParentPtr, wkgPtr);
|
||||||
// call resolveRBT with parent as createdNode (will treat as RRC)
|
// call resolveRBT with parent as createdNode (will treat as RRC)
|
||||||
|
resolveRBT(treeRoot, wkgParentPtr);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue