for tree.h:
#include <stdio.h>
#include <stdlib.h>
/************************/
/* tree.h for Project 4*/
/**********************/
#define kFileName "../../Data.txt"
/************************/
/* TreeNode structure */
/**********************/
typedef struct TreeNode TreeNode;
typedef TreeNode *TreeNodePtr;
struct TreeNode {
struct TreeNode *leftPtr;
int value;
struct TreeNode *rightPtr;
};
/************************/
/* function prototypes */
/**********************/
void InsertInTree( TreeNodePtr *treePtr, int value );
TreeNodePtr SearchTree( TreeNodePtr treePtr, const int key );
void BuildTree( void );
int GetNumberFromFile( int *numPtr, FILE *fp );
void DeleteTree(TreeNodePtr treePtr);
for tree.c:
#include <stdio.h>
#include <stdlib.h>
#include "tree.h"
/*****************************************/
/*InsertInTree- Directs the numbers */
/* on where they are placed in the tree*/
/**************************************/
void InsertInTree( TreeNodePtr *treePtr, int value )
{
/* if treePtr is NULL */
if ( *treePtr == NULL ) {
/* dynamically allocate memory */
*treePtr = malloc( sizeof( TreeNode ) );
/* if memory was allocated, insert node */
if ( *treePtr != NULL ) {
( *treePtr )->value = value;
( *treePtr )->leftPtr = NULL;
( *treePtr )->rightPtr = NULL;
}/*endif*/
else {
printf("ERROR: Could not allocate memory!\n" );
return; //I ADDED THIS
}/*end else*/
}/*endif*/
else { /* recursively call InsertInTree */
/* insert node in left subtree */
if (value<( *treePtr )->value ) {
InsertInTree( &( ( *treePtr )->leftPtr ), value );
}/*endif*/
else { /* insert node in right subtree */
if (value>( *treePtr )->value ) {
InsertInTree( &(( *treePtr )->rightPtr ), value );
}
else { /* duplicate value */
printf( "Duplicate Found" );
}/*endelse*/
}/*endelse*/
} /* end else */
} /* end function InsertInTree */
/********************************************/
/*BuildTree- Helper Function to read the */
/* Data.txt file and print those on screen*/
/*****************************************/
void BuildTree( void )
{
int num;
FILE *fp;
if ( ( fp = fopen( "kFileName", "r" ) ) == NULL )
printf( "Could not read numbers file!\n" );
printf( "The numbers being placed in the tree are:\n" );
while ( GetNumberFromFile( &num, fp ) )
{
printf( "%3d ", num );
InsertInTree( &rootPtr, num );
}
fclose( fp );
}
/*********************************************/
/*GetNumberFromFile- Specific function to */
/* read the numbers from Data.txt */
/*****************************************/
int GetNumberFromFile( int *numPtr, FILE *fp )
{
if ( fscanf( fp, "%d\n", numPtr ) == EOF )
return false;
else
return true;
}
/*********************************************/
/*Search- Helper function to SearchTree */
/*****************************************/
void Search(TreeNodePtr treePtr) {
int searchKey;
printf("Enter number to be searched: ");
scanf( "%d", &searchKey );
searchResultPtr = SearchTree( rootPtr, searchKey );
}
/********************************************/
/*SearchTree- Searches tree in a preorder */
/*traversal and stores required values */
/*****************************************/
TreeNodePtr SearchTree( TreeNodePtr treePtr, const int key )
{
/* traverse the tree inOrder */
if ( treePtr == NULL ) {
return NULL; /* key not found */
} /* end if */
else if ( treePtr->value == key ) {
return treePtr; /* key found */
} /* end else if */
else if ( key < treePtr->value ) {
SearchTree( treePtr->leftPtr, key ); /* search left */
} /* end else if */
else if ( key > treePtr->value ) {
SearchTree( treePtr->rightPtr, key ); /* search right */
} /* end else if */
} /* end function SearchTree */
/**********************************************/
/*DeleteTree- Destroy's the tree after search*/
/********************************************/
void DeleteTree (TreeNodePtr treePtr)
{ if(treePtr!=NULL)
{
DeleteTree(treePtr->left);
DeleteTree(treePtr->right);
free(treePtr);
}
}
for main.c:
#include <stdio.h>
#include <stdlib.h>
#include "tree.h"
/************************/
/* Main Function */
/**********************/
int main()
{
int num; /* random value to insert in tree */
int searchKey; /* value to search for */
TreeNodePtr rootPtr = NULL; /* points to the tree root */
TreeNodePtr searchResultPtr; /* pointer to search result */
BuildTree();
Search(TreeNodePtr treePtr) //maybe rootPtr ?
DeleteTree (TreeNodePtr treePtr)
return;
}
Thanks for any help anyone might be able to provide.

New Topic/Question
Reply




MultiQuote





|