Ok the commands are:
a or A followed by a number to add to the tree (ex: a123)(finished)
d or D followed by number to delete 1st node from tree that matches(ex: d123)(not finished)
s or S followed by number to delete all nodes in tree that matches(ex: s123)(not finished)
p or P to print tree
e or E to exit
So far I have the print and add function working. The tree is preloaded with an array with a value greater than insert left, less or equal insert right.
If I print the initial list it works. If I hit print again I get some funky numbers. It looks as if the numbers are being duplicated in the print. Whats wrong. Can't figure out what is happening. Thanks.
Code:
#include <stdio.h>
#define SIZE 15
#define ARRAYSIZE 80
#define PRINT 15
//*************STRUCTS*****************
struct treeNode
{
int data;
struct treeNode *rChild;
struct treeNode *lChild;
};
//**************************************//
//***********FUNCTION PROTOTYPES********//
struct treeNode *makeTreeNode(int number);
void readArrayIntoTree(int *treeArray, struct treeNode **rootPtr);
void insertInTree(struct treeNode **rootPtr, int number);
void printTraversal(struct treeNode *rootPtr);
void preOrderTraversal(struct treeNode *rootPtr);
void inOrderTraversal(struct treeNode *rootPtr);
void postOrderTraversal(struct treeNode *rootPtr);
//******END OF FUNCTION PROTOTYPES******//
//************************MAIN************************//
int main()
{
struct treeNode *rootPtr = '\0';
int number;
int index = 1;
int treeArray[15] ={60,75,40,32,55,80,70,74,68,69,85,90,84,65,28};
char ch;
char selection;
char inputArray[256];
char numberArray[256];
printf("-->[E] or [e] will exit the program<--\n\n");
do
{
readArrayIntoTree(treeArray, &rootPtr);
printf("Enter a command---> ");
gets(inputArray);
selection = inputArray[0];
while(inputArray[index] != '\0' && index < ARRAYSIZE)
{
ch = inputArray[index];
numberArray[index-1] = ch;
index++;
}
numberArray[index-1];
number = atoi(numberArray);
switch(selection)
{
case 'a':
insertInTree(&rootPtr, number);
break;
case 'A':
insertInTree(&rootPtr, number);
break;
case 'd':
break;
case 'D':
break;
case 's':
break;
case 'S':
break;
case 'p':
printTraversal(rootPtr);
break;
case 'P':
printTraversal(rootPtr);
break;
case 'e':
break;
case 'E':
break;
default:
printf("\n\n*****You have entered an invalid command [a/A, d/D, s/S, p/P, e/E]*****\n\n\n");
}
}while(selection != 'e' && selection != 'E');
printf("\n\n\n");
system("PAUSE");
return 0;
}
//************END OF MAIN**************//
//**********MAIN PROTOTYPED FUNCTIONS**//
struct treeNode *makeTreeNode(int number)
{
struct treeNode *newTreePtr = '\0';
newTreePtr = (struct treeNode*)malloc(sizeof(struct treeNode));
newTreePtr->lChild = '\0';
newTreePtr->rChild = '\0';
newTreePtr->data = number;
return newTreePtr;
}
void readArrayIntoTree(int *treeArray, struct treeNode **rootPtr)
{
int index;
for(index=0;index<SIZE;index++)
{
insertInTree(rootPtr, (*(treeArray + index)));
}
}
void insertInTree(struct treeNode **rootPtr, int number)
{
struct treeNode *newTreePtr = '\0';
struct treeNode *currTreePtr = '\0';
struct treeNode *insertLocationPtr = '\0';
newTreePtr = makeTreeNode(number);
if(*(rootPtr) == '\0')
{
*rootPtr = newTreePtr;
}
else
{
currTreePtr = *rootPtr;
while(currTreePtr != '\0')
{
insertLocationPtr = currTreePtr;
if(newTreePtr->data > currTreePtr->data)
{
currTreePtr = currTreePtr->lChild;
}
else
{
currTreePtr = currTreePtr->rChild;
}
}
if(newTreePtr->data > insertLocationPtr->data)
{
insertLocationPtr->lChild = newTreePtr;
}
else
{
insertLocationPtr->rChild = newTreePtr;
}
}
}
void preOrderTraversal(struct treeNode *rootPtr)
{
static int post_counter = 1;
if(rootPtr != '\0')
{
printf("%-3i",rootPtr->data);
post_counter++;
if(post_counter > PRINT)
{
printf("\n\t\t\t");
post_counter = 1;
}
preOrderTraversal(rootPtr->lChild);
preOrderTraversal(rootPtr->rChild);
}
}
void inOrderTraversal(struct treeNode *rootPtr)
{
static int post_counter = 1;
if(rootPtr != '\0')
{
inOrderTraversal(rootPtr->lChild);
printf("%-3i",rootPtr->data);
post_counter++;
if(post_counter > PRINT)
{
printf("\n\t\t\t");
post_counter = 1;
}
inOrderTraversal(rootPtr->rChild);
}
}
void postOrderTraversal(struct treeNode *rootPtr)
{
static int post_counter = 1;
if(rootPtr != '\0')
{
postOrderTraversal(rootPtr->lChild);
postOrderTraversal(rootPtr->rChild);
printf("%-3i",rootPtr->data);
post_counter++;
if(post_counter > PRINT)
{
printf("\n\t\t\t");
post_counter = 1;
}
}
}
void printTraversal(struct treeNode *rootPtr)
{
if(rootPtr == '\0')
{
printf("*****No nodes to print*****");
}
else
{
printf("\n\t\tThe preorder traversal is:\n\n");
printf("\t\t\t");
preOrderTraversal(rootPtr);
printf("\n\n");
printf("\t\tThe inorder traversal is:\n\n");
printf("\t\t\t");
inOrderTraversal(rootPtr);
printf("\n\n");
printf("\t\tThe postorder traversal is:\n\n");
printf("\t\t\t");
postOrderTraversal(rootPtr);
printf("\n\n");
}
}
//**********END OF MAIN FUNCTIONS**********//