-1

テキスト ファイルから単語を読み取り、それらの単語を使用してアルファベット順に並べ替えられた二分探索木を作成するプログラムを開発するにはどうすればよいですか? ここに私のコードがあります

 #include "stdio.h"
#include "stdlib.h"
#include "string.h"

struct treeNode {
       char data[20];
       int count;
       struct treeNode *leftPtr, *rightPtr;
 };

int number;

typedef struct treeNode TreeNode;
typedef TreeNode *TreeNodePtr;

void insertNode (TreeNodePtr *treePtr,char word[]);
void alphabetic(TreeNodePtr treePtr);




int main(){

    /*reading strings from the file and add them to the tree*/

    char first[20];
    FILE *fp1;
    TreeNodePtr rootPtr=NULL;
    int c;
    fp1=fopen("output.txt","r");
    do{
        c=fscanf(fp1,"%s",first);
        insertNode(&rootPtr,first);




    }while(c!=EOF);


    fclose(fp1);

    alphabetic(rootPtr);

    system("PAUSE");

}

/*for adding nodes to tree*/

void insertNode (TreeNodePtr *treePtr,char word[20]){
    TreeNode *temp = NULL;
    if(*treePtr == NULL)
    {
        temp = (TreeNode *)malloc(sizeof(TreeNode));
        temp->leftPtr = NULL;
        temp->rightPtr = NULL;
        temp->data[20] =  word[20];
        *treePtr = temp;

    }
    else if(strcmp(word,(*treePtr)->data)<0){


        insertNode(&((*treePtr)->leftPtr),word);
    }
    else if (strcmp(word,(*treePtr)->data)>0){


        insertNode(&((*treePtr)->rightPtr),word);
    }
    else{
        number++;
    }
}

/*for sorting alphabetically*/
void alphabetic(TreeNodePtr treePtr){
    if(treePtr!=NULL){
        alphabetic(treePtr->leftPtr);
        printf("%3d\n",treePtr->leftPtr);
        alphabetic(treePtr->rightPtr);
    }
}

4 つの単語を含む .txt がある場合、プログラムは出力として 4 つの 0 のみを書き込みます。

4

1 に答える 1