Partially Initialize PA08

++ Files for PA08
++ Makefile for PA08
This commit is contained in:
Charles Descamps 2023-10-26 10:16:25 -07:00
parent a3ec33a917
commit 37f0a97939
14 changed files with 4368 additions and 0 deletions

View file

@ -0,0 +1,278 @@
// Header file
#include "RedBlackTreeUtility.h"
// constants
const char SEMICOLON_BREAK[] = "; ";
const char THICK_DIVIDER = '=';
const char THIN_DIVIDER = '-';
// functions
// TODO: Set functions here
////////////////////////////////////////////////////////////////////////////////
// No student coding below this point
////////////////////////////////////////////////////////////////////////////////
/*
Name: displayAtTreeLevel
Process: displays one horizontal "level" of a tree using text graphics
with appropriate spacing and appropriate number of nodes
Function input/parameters: recursive working node (RedBlackNodeType *),
node height, display level, working level (int),
row start flag (bool),
control code for either data or color
Function output/parameters: none
Function output/returned: updated row start flag (bool *)
Device input/file: none
Device output/monitor: none
Dependencies: displayValue, displayEmptyNodeSpaces
*/
void displayAtTreeLevel( RedBlackNodeType *workingNode, int nodeHeight,
int displayLevel, int workingLevel,
bool *rowStartFlag, ControlCodes ctrlCode )
{
char charOut = workingNode->data;
if( ctrlCode == NODE_COLOR )
{
charOut = (char)workingNode->color;
}
if( workingLevel == displayLevel )
{
displayValue( charOut, nodeHeight, workingLevel, rowStartFlag );
}
else
{
if( workingNode->leftChildPtr != NULL )
{
displayAtTreeLevel( workingNode->leftChildPtr, nodeHeight,
displayLevel, workingLevel + 1, rowStartFlag, ctrlCode );
}
else
{
displayEmptyNodeSpaces( nodeHeight, displayLevel,
workingLevel + 1, rowStartFlag, ctrlCode );
}
if( workingNode->rightChildPtr != NULL )
{
displayAtTreeLevel( workingNode->rightChildPtr, nodeHeight,
displayLevel, workingLevel + 1, rowStartFlag, ctrlCode );
}
else
{
displayEmptyNodeSpaces( nodeHeight, displayLevel,
workingLevel + 1, rowStartFlag, ctrlCode );
}
}
}
/*
Name: displayChars
Process: recursively displays a specified number of specified characters
Function input/parameters: number of characters (int),
character to be displayed (char)
Function output/parameters: none
Function output/returned: none
Device input/file: none
Device output/monitor: characters displayed as specified
Dependencies: printf
*/
void displayChars( int numChars, char outChar )
{
if( numChars > 0 )
{
printf( "%c", outChar );
displayChars( numChars - 1, outChar );
}
}
/*
Name: displayDivider
Process: displays divider of correct length for tree
using either THICK_DIVIDER or THIN_DIVIDER
depending on the control code,
adds one endline to thick divider, two to thin for spacing
Function input/parameters: number of characters (int),
character to be displayed (char)
Function output/parameters: none
Function output/returned: none
Device input/file: none
Device output/monitor: characters displayed as specified
Dependencies: printf
*/
void displayDivider( RedBlackNodeType *rootPtr, char dividerChar )
{
int treeHeight = findTreeHeight( rootPtr );
int numChars = toPower( 2, treeHeight + 2 );
displayChars( numChars, dividerChar );
if( dividerChar == THIN_DIVIDER )
{
printf( "\n" );
}
printf( "\n" );
}
/*
Name: displayEmptyNodeSpaces
Process: displays the appropriate number of dashes for a given level
for null nodes, can display either dashes or 'B's
Function input/parameters: node height, display level, working level (int)
pointer to row start flag (bool *),
control code for either data or color
Function output/parameters: none
Function output/returned: updated row start flag (bool *)
Device input/file: none
Device output/monitor: characters displayed as specified
Dependencies: toPower, displayValue
*/
void displayEmptyNodeSpaces( int nodeHeight,
int displayLevel, int workingLevel,
bool *rowStartFlag, ControlCodes ctrlCode )
{
int nodesToDisplay = toPower( 2, displayLevel - workingLevel );
char charOut = SPACE;
if( displayLevel == workingLevel )
{
charOut = DASH;
if( ctrlCode == NODE_COLOR )
{
charOut = 'B';
}
}
while( nodesToDisplay > 0 )
{
displayValue( charOut, nodeHeight, displayLevel, rowStartFlag );
nodesToDisplay--;
}
}
/*
Name: displayTreeStructure
Process: repeatedly calls other function to display
the structure of an RB tree, can display either dashes or 'B's,
displays error message if empty tree
Function input/parameters: tree root pointer (RedBlackNodeType *),
control code for either data or color
Function output/parameters: none
Function output/returned: none
Device input/file: none
Device output/monitor: characters displayed as specified
Dependencies: findTreeHeight, displayAtTreeLevel, printf
*/
void displayTreeStructure( RedBlackNodeType *rbTreeRoot, ControlCodes ctrlCode )
{
int displayLevel, nodeHeight = findTreeHeight( rbTreeRoot ) + 2;
int workingLevel = 1;
bool rowStartFlag;
if( rbTreeRoot != NULL )
{
for( displayLevel = 1; displayLevel <= nodeHeight; displayLevel++ )
{
rowStartFlag = true;
displayAtTreeLevel( rbTreeRoot, nodeHeight,
displayLevel, workingLevel, &rowStartFlag, ctrlCode );
printf( "\n" );
}
}
else
{
printf( "\nEmpty Tree - No Display" );
}
}
/*
Name: displayValue
Process: displays a tree character value or color letter (R/B)
after a calculated set of leading spaces
Function input/parameters: character data (char),
node height, working level (int),
pointer to row start flag (bool *)
Function output/parameters: updated row start flag (bool *)
Function output/returned: none
Device input/file: none
Device output/monitor: character displayed as specified
Dependencies: toPower, displayChars, printf
*/
void displayValue( char data, int nodeHeight,
int workingLevel, bool *rowStartFlag )
{
int leadingSpaces;
if( *rowStartFlag )
{
leadingSpaces = toPower( 2, nodeHeight - workingLevel );
*rowStartFlag = false;
}
else
{
leadingSpaces = toPower( 2, nodeHeight - workingLevel + 1 ) - 1;
}
displayChars( leadingSpaces, SPACE );
printf( "%c", data );
}
/*
Name: getMax
Process: finds the maximum between two values, returns
Function input/parameters: two values (int)
Function output/parameters: none
Function output/returned: maximum of two values (int)
Device input/file: none
Device output/monitor: none
Dependencies: none
*/
int getMax( int one, int other )
{
int max = one;
if( other > max )
{
max = other;
}
return max;
}
/*
Name: toPower
Process: recursively calculates a base to exponent value
for positive integer exponents
Function input/parameters: base, exponent (int)
Function output/parameters: none
Function output/returned: calculated value (int)
Device input/file: none
Device output/monitor: none
Dependencies: none
*/
int toPower( int base, int exponent )
{
if( exponent > 0 )
{
return toPower( base, exponent - 1 ) * base;
}
return 1;
}