C的前缀树实现

时间:2017-02-13 17:56:30

标签: c tree nodes prefix

我正在尝试编写一个C程序,它提供查看目录内容的自动完成建议。我已经完成了大部分工作,但是当目录名称包含非字母符号(例如。,/ _)时,我会收到错误。每当我尝试更改内容时,我都会收到错误,包括数组大小和循环值。您能否看一下我的代码,并建议我需要做些哪些更改才能在自动填充建议中包含符号。

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

#define MAX 1000

struct node {
        int data;
        struct node *array[26];
};

struct node* new_node(struct node *h)
{
        int i;
        h = malloc(sizeof (struct node));
        for (i = 0; i < 24; ++i)
                h->array[i] = 0;
        return h;
}

struct node* insert(struct node *h, char *c, int value)
{
        int i;
        if (strlen(c) == 0)
                return h;
        if (h == NULL)
                h = new_node(h);
        struct node *p = h;
        for (i = 0; i < strlen(c); ++i) {
                if (p->array[c[i] - 'a'] == NULL)
                        p->array[c[i] - 'a'] = malloc(sizeof (struct node));
                p = p->array[c[i] - 'a'];
        }
        p->data = value;
        return h;
}

int dfs(struct node *h, char *dat)
{
        int i;
        char k[1000] = {0};
        char a[2] = {0};
        if (h == NULL)
                return 0;
        if (h->data > 0)
                printf("Match: %s\n", dat);
        for(i = 0; i < 26; ++i) {
                strcpy(k, dat);
                a[0] = i + 'a';
                a[1] = '\0';
                strcat(k, a);
                dfs(h->array[i], k);
        }
}

void search(struct node *h, char *s, char *dat)
{
        int i;
        char l[1000] = {0};
        char a[2];
        strcpy(l, dat);
        if (strlen(s) > 0) {
                a[0] = s[0];
                a[1] = '\0';
                if(h->array[a[0]-'a'] != NULL) {
                        strcat(dat, a);
                        search(h->array[a[0]-'a'], s+1, dat);
                } else
                        printf("No Match \n");
        } else {
                if (h->data != 0)
                        printf("Match: ");
                for (i = 0; i < 26; ++i) {
                        strcpy(l, dat);
                        a[0] = i + 'a';
                        a[1] = '\0';
                        strcat(l, a);
                        dfs(h->array[i], l);
                }
        }
}

struct node* read_keys(struct node *h, char *file)
{
        char s[MAX];
        FILE *a = fopen(file, "r");
        if (a == NULL)
                printf("Error while opening file");
        else
                while (feof(a) == 0) {
                        fscanf(a, "%s", s);
                        h = insert(h, s, 1);
                }    
        return h;
}


int main()
{
        DIR *d;
        struct dirent *dir;
        char direct[MAX];
        printf("Enter a folder name: ");
        scanf("%s",direct);
        d = opendir(direct);

        FILE *f = fopen("file.txt", "w");
        if (f == NULL)
        {
                printf("Error opening file!\n");
                exit(1);
        }

        if (d)
        {
                while ((dir = readdir(d)) != NULL)
                {
                        fprintf(f,"%s\n", dir->d_name);
                }
        }

        fclose(f);
        char c[1000];
        FILE *fptr;
        if ((fptr = fopen("file.txt", "r")) == NULL)
        {
                printf("Error! opening file");
                exit(1);
        }

        FILE* file = fopen("file.txt", "r");
        char line[1000];
        while (fgets(line, sizeof(line), file)) {
                printf("%s", line);
        }

        fclose(fptr);

        printf("Enter beginning of filename \n");
        struct node *h = 0;
        h = read_keys(h, "file.txt");
        char s[MAX];
        scanf("%s", s);
        char dat[1000] = "";
        search(h, s, dat);
        return 0;
}

0 个答案:

没有答案