diff --git a/PA08-1101/PA08_Main b/PA08-1101/PA08_Main index 2a663c2..eb25fd7 100755 Binary files a/PA08-1101/PA08_Main and b/PA08-1101/PA08_Main differ diff --git a/PA08-1101/RedBlackTreeUtility.c b/PA08-1101/RedBlackTreeUtility.c index d064cb2..146a230 100644 --- a/PA08-1101/RedBlackTreeUtility.c +++ b/PA08-1101/RedBlackTreeUtility.c @@ -37,14 +37,9 @@ Dependencies: free */ RedBlackNodeType *clearTree(RedBlackNodeType *localPtr) { - // recurse through the left children - if (localPtr->leftChildPtr != NULL) + if(localPtr->leftChildPtr != NULL && localPtr->rightChildPtr != NULL) { clearTree(localPtr->leftChildPtr); - } - // recurse through the right children - if (localPtr->rightChildPtr != NULL) - { clearTree(localPtr->rightChildPtr); } // 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 RedBlackNodeType *newNode; // allocate memory for the newNode (RedBlackNodeType) - newNode = malloc(sizeof(RedBlackNodeType)); + newNode = (RedBlackNodeType*)malloc(sizeof(RedBlackNodeType)); // define the color as the new node's color newNode->color = nodeColor; // define the data of the new node to be the input data @@ -290,6 +285,7 @@ Dependencies: getMax */ int findTreeHeight(RedBlackNodeType *localPtr) { + int leftTreeSize, rightTreeSize; // check to see if at null if(localPtr == NULL) { @@ -299,8 +295,8 @@ int findTreeHeight(RedBlackNodeType *localPtr) else { // traverse the left and right subtrees - int leftTreeSize = findTreeHeight(localPtr->leftChildPtr); - int rightTreeSize = findTreeHeight(localPtr->rightChildPtr); + leftTreeSize = findTreeHeight(localPtr->leftChildPtr); + rightTreeSize = findTreeHeight(localPtr->rightChildPtr); // return the max of the subtrees, add one for the local node return getMax(leftTreeSize, rightTreeSize) + 1; } @@ -398,7 +394,6 @@ RedBlackNodeType *insertHelper(RedBlackNodeType *wkgPtr, char inData) // return recursion return insertHelper(wkgPtr->leftChildPtr, inData); } - // otherwise, check for moving right (diffChar > 0) else if(diffChar > 0) { @@ -413,13 +408,9 @@ RedBlackNodeType *insertHelper(RedBlackNodeType *wkgPtr, char inData) // return recursion return insertHelper(wkgPtr->rightChildPtr, inData); } - // otherwise, there is a duplicate - else - { // return NULL - return NULL; - } + return NULL; } /* @@ -455,6 +446,7 @@ Dependencies: printf, rotateLeft, rotateRight, swapColors, */ void resolveRBT(RedBlackNodeType **treeRoot, RedBlackNodeType *wkgPtr) { + printf("\nAttempting to Resolve RBT\n"); // declare parPtr, grndParPtr, unclePtr, grtGrndPtr RedBlackNodeType *wkgParentPtr; RedBlackNodeType *wkgGrandParentPtr; @@ -463,10 +455,11 @@ 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"); // set all my relatives, will need to know which child I am wkgParentPtr = wkgPtr->parentPtr; wkgGrandParentPtr = wkgParentPtr->parentPtr; - if (wkgParentPtr->leftChildPtr->data == wkgPtr->data) + if (wkgParentPtr->leftChildPtr == wkgPtr) { // set the uncle pointer to the parent's right child wkgUnclePtr = wkgParentPtr->rightChildPtr; @@ -492,10 +485,10 @@ void resolveRBT(RedBlackNodeType **treeRoot, RedBlackNodeType *wkgPtr) else { // 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) - if(wkgParentPtr->leftChildPtr->data == wkgPtr->data) + if(wkgParentPtr->leftChildPtr == wkgPtr) { printf("\nLL Case, rotate right\n"); // 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 } // check for child is right of parent (Left Right case) - if(wkgParentPtr->rightChildPtr->data == wkgPtr->data) + else { printf("\nLR Case, rotate right, recurse\n"); // 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 - if(wkgGrandParentPtr->rightChildPtr->data == wkgParentPtr->data) + else { // 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"); // 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 } // check for child is left of parent (Right Left case) - if(wkgParentPtr->leftChildPtr->data == wkgPtr->data) + else { printf("\nRL Case, rotate left, recurse\n"); // use rotate left with parent(old) and createdNode(new) @@ -538,6 +531,14 @@ void resolveRBT(RedBlackNodeType **treeRoot, RedBlackNodeType *wkgPtr) // paint it 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. }