二分探索木を作成するプログラムを(宿題用に)書きました。ファイルから読み取り、各行で文字列と整数を取ります。元。I 23 は、プログラムに挿入するように指示し、23 は挿入されるものです。次の行は I 45... などです。すべてのコマンドを正しく実行し、順番、前順、後順、およびレベル順のトラバーサルを画面に出力します。私の問題は、代わりにファイルに出力する必要があり、私の人生ではそれを理解できないことです。別のファイルを開いて書き込みを試み、そのファイルを各トラバーサル関数に渡し、printf の代わりに fprintf を使用しましたが、明らかにうまくいきませんでした。どんな助けでも素晴らしいでしょう!
ファイルの受け渡しを表示する編集:
私のコードは次のとおりです。
#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;
}