>But, how about the buffer array for the individual words? And,
>how would you pass the individual word to the Insert function?
Just create a char array to hold each word, use scanf to read a single word from the file and pass that to the Insert function. The changes really are minor:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct TreeNode
{
char Data[BUFSIZ];
struct TreeNode *Left, *Right;
};
typedef struct TreeNode *TreePointer;
static void InsertTree(TreePointer *Root, char *rec);
static void Inorder(TreePointer Root);
int main ( void )
{
char word[BUFSIZ] = {'\0'};
TreePointer Root = NULL;
while ( scanf("%s", word) == 1 ) {
InsertTree(&Root,word);
}
if ( Root != NULL )Inorder(Root);
(void)getchar();
return EXIT_SUCCESS;
}
void Inorder(TreePointer Root)
{
if(Root!=NULL)
{
Inorder(Root->Left);
printf("%s\n", Root->Data);
Inorder(Root->Right);
}
}
void InsertTree(TreePointer *Root, char *rec)
{
if (*Root == NULL)
{
*Root = malloc(sizeof **Root);
if ( *Root != NULL ) {
strcpy ( (*Root)->Data, rec );
(*Root)->Left = NULL;
(*Root)->Right = NULL;
}
}
else if ( strcmp ( rec, (*Root)->Data ) < 0 )
InsertTree(&(*Root)->Left,rec);
else if ( strcmp ( rec, (*Root)->Data ) > 0 )
InsertTree(&(*Root)->Right,rec);
}
>Perhaps it should be moved to FAQ or just globally clarified somehow.
I doubt either could be done easily, binary trees just aren't easy to grasp at first.
-Prelude