1

I have written a program(for homework)that creates a binary search tree. It will read from a file and on each line take a string and an int. ex. I 23, where I tells the program to insert and 23 is what gets inserted. The next line would be I 45...and so on. It will perform all of the commands correctly and then output to the screen in-order, pre-order, post-order and level-order traversals. My problem is that I need to output it to a file instead and cannot for the life of me figure it out. I have tried opening another file to write to it and pass the file to each traversal function and used fprintf instead of printf but that obviously didn't work. Any help would be great!

EDIT to show file passing:

My code is as follows:

#include <stdio.h>
#include <stdlib.h>

typedef int boo;
#define TRUE 1;
#define FALSE 0;

struct tree
{
    int c;
    struct tree* left;
    struct tree* right;
};
FILE* fo = fopen( "treedata.txt", "a+" );

//Prototypes
struct tree* create_tree( char );
struct tree* insert( struct tree*, char );
int height( struct tree* );
void print_in_order( struct tree*, FILE* );
void print_pre_order( struct tree*, FILE* );
void print_post_order( struct tree*, FILE* );
void print_level_order( struct tree*, FILE* );
void print_given_level( struct tree*, int, FILE* );
boo search_tree( struct tree*, char );
struct tree* trim_tree( struct tree*, char );
struct tree* chop_tree( struct tree* root );

int main()
{
    struct tree* root = NULL;
    char target;
char line[6];
char com[6];
int num;


FILE* fp = fopen( "data.txt", "r" );    
    if( fp )
    {
        while( fgets ( line, sizeof(line), fp ) != NULL  )
        {
        sscanf(line, "%s %d", &com, &num);

        if ( strcmp( com, "C" ) == 0 )
        {
            //root = insert( root, 0 );
            create_tree( 0 );
        }
        else if(strcmp( com, "I" ) == 0 )
        {
            root = insert(root, num);
        }
        else if(strcmp( com, "D" ) == 0 )
        {
            root = trim_tree( root, num );
        }
        else if(strcmp( com, "F" ) == 0 )
        {
            if( search_tree( root , num ) )
            {
                fprintf(fo, "\n%d was found!\n\n", num );
            }else
            {
                fprintf(fo, "\n%d was not found!\n", num );
            }
        }
        else if(strcmp( com, "In" ) == 0 )
        {
            fprintf(fo, "In Order: \n");
            print_in_order( root, fo );
            fprintf(fo, "\n");
        }
        else if(strcmp( com, "Pr" ) == 0 )
        {
            fprintf(fo, "Pre-Order: \n");
            print_pre_order( root, fo );
            fprintf(fo, "\n");
        }
        else if(strcmp( com, "Po" ) == 0 )
        {
            fprintf(fo, "Post-Order: \n");
            print_post_order( root, fo );
            fprintf(fo, "\n");
        }
        else if(strcmp( com, "L" ) == 0 )
        {
            printf( "Level Order: \n");
            print_level_order( root, fo );
        }

    }
        fclose( fp );
    }else
    {
        printf("Could not open file!!\n");
    }

fclose( fo );   
return 0;
}

//Functions
struct tree* create_tree( int c )
{
    struct tree* temp_tree = ( struct tree* ) malloc ( sizeof( struct tree ) );
    temp_tree->c = c;
    temp_tree->left = NULL;
    temp_tree->right = NULL;

    return temp_tree;
}

struct tree* insert( struct tree* t, int c)
{
    if( t == NULL )
    {
        return create_tree( c );
    }else
    {
        if( c <= t->c )
        {
            t->left = insert( t->left, c );
        }else
        {
            t->right = insert( t->right, c );
        }
        return t;
    }
} 

void print_in_order( struct tree* t, FILE* fo ) //print in order traversal
{
    if( !t )
    {
        return;
    }

    print_in_order( t->left );
    fprintf(fo, "%d\n", t->c );
    print_in_order( t->right );

}

void print_pre_order( struct tree* t, FILE* fo  ) //print pre order traversal
{
    if( !t )
    {
        return;
    }
    fprintf(fo, "%d\n", t->c );
    print_pre_order( t->left );
    print_pre_order( t->right );
}

void print_post_order( struct tree* t, FILE* fo  ) // print post order traversal
{
    if( !t )
    {
        return;
    }

    print_post_order( t->left );
    print_post_order( t->right );
    fprintf(fo, "%d\n", t->c );

}

int find_height(struct tree* t)
{
   if ( t == NULL )
       return 0;
   else
   {

     int lheight = find_height(t->left);
     int rheight = find_height(t->right);


     if (lheight > rheight)
         return(lheight+1);
     else return(rheight+1);
   }
} 

void print_given_level( struct tree* t, int level, FILE* fo  )
{

        if(t == NULL)
        return;
    if( level == 1 )
        fprintf(fo, "%d\n", t->c );
    else if( level > 1 )
    {
    print_given_level(t->left, level-1);
    print_given_level(t->right, level-1);
    }
}

void print_level_order( struct tree* t, FILE* fo  ) //print level order traversal
{
    int h = find_height(t);
int i;
for( i = 1; i <= h ; i++ )
    print_given_level( t, i, fo );

}
boo search_tree( struct tree* root, char target ) //find leaf
{
    if( !root )
    {
        return FALSE;
    }else if( target < root->c )
    {
        return search_tree( root->left, target );
    }else if( target > root->c )
    {
        return search_tree( root->right, target );
    }else
    {
        return TRUE;
    }
}

struct tree* trim_tree( struct tree* root, char target ) //delete leaf
{
    struct tree* p;
    struct tree* p2;

    if( !root )
    {
        return NULL;
    }
    if( root->c == target )
    {
        if( root->left == root->right )
        {
            free( root );
            return NULL;
        }else if( root->left == NULL )
        {
            p = root->right;
            free( root );
            return p;
        }else if( root->right == NULL )
        {
            p = root->left;
            free( root );
            return p;
        }else
        {
            p2 = root->right;
            p = root->right;

            while( p->left )
            {
                p = p->left;    
            }
            p->left = root->left;
            free( root );
            return p2;
        }        
    }else if( root->c < target )
    {
        root->right = trim_tree( root->right, target );
    }else
    {
        root->left = trim_tree( root->left, target );
    }
    return root;

}

4

0 回答 0