-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwsearch.c
158 lines (129 loc) · 2.74 KB
/
wsearch.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "tree.h"
#define ASSERT(cond) {if (!(cond)) (*((char *)0) = 0);}
#define WORD_DB "/usr/share/dict/words"
#define MAX_WORD_SIZE 80
struct word_node {
char *word;
RB_ENTRY(word_node) rb_node;
};
typedef struct word_node wnode_t;
typedef struct tree_handle {
RB_HEAD(word_tree, word_node) th_tree;
} tree_handle_t;
int
str_compare(const void *query_key, const void *cur)
{
char *x = ((wnode_t *)query_key)->word;
char *y = ((wnode_t *)cur)->word;
if (strcmp(x, y) < 0) {
return (-1);
} else if (strcmp(x, y) > 0) {
return (1);
} else {
return (0);
}
}
RB_PROTOTYPE(word_tree, word_node, rb_node, str_compare);
RB_GENERATE(word_tree, word_node, rb_node, str_compare);
wnode_t *get_node(char *str)
{
wnode_t *w = ((wnode_t *) malloc(sizeof(wnode_t)));
w->word = malloc(strlen(str));
strcpy(w->word, str);
return (w);
}
void
init_tree(tree_handle_t *handle)
{
RB_INIT(&handle->th_tree);
}
int
add_word_to_tree(tree_handle_t *handle, char *add_str)
{
wnode_t *node;
int ret = 0;
ASSERT(add_str != NULL);
node = get_node(add_str);
if (RB_FIND(word_tree, &handle->th_tree, node) != NULL) {
/* Node already present */
free(node);
ret = EEXIST;
} else {
RB_INSERT(word_tree, &handle->th_tree, (void *)node);
}
return (ret);
}
int
search_word_in_tree(tree_handle_t *handle, char *search_str)
{
wnode_t temp, *node;
int ret;
memset((void *)&temp, 0, sizeof(wnode_t));
temp.word = search_str;
if ((node = RB_FIND(word_tree, &handle->th_tree, &temp)) != NULL) {
/* Found */
ret = 0;
} else {
/* Not Found */
ret = ENOENT;
}
return (ret);
}
int
populate_tree(tree_handle_t *tree)
{
/* Assumption : no word in the WORD_DB is >= MAX_WORD_SIZE characters long */
FILE *fp;
char temp[MAX_WORD_SIZE];
fp = fopen(WORD_DB, "r");
if (fp == NULL) {
fprintf(stderr, "Could not open word database at : %s\n",
WORD_DB);
exit(1);
}
while(fscanf(fp, "%s", temp) != EOF) {
//printf("Adding %s to tree\n", temp);
if (add_word_to_tree(tree, temp) == EEXIST) {
fprintf(stderr, "%s already in tree\n", temp);
}
}
fclose(fp);
return (0);
}
int
query_word_from_user(char *temp)
{
int i;
printf("Enter word: ");
fflush(stdin);
fgets(temp, MAX_WORD_SIZE, stdin);
/* fgets() reads the newline into the buffer. Remove if present */
for (i = 0; i < strlen(temp); i++) {
if (temp[i] == '\n') {
temp[i] = '\0';
}
}
}
int
main()
{
tree_handle_t th;
int ret;
char temp[MAX_WORD_SIZE];
init_tree(&th);
populate_tree(&th);
while(1) {
query_word_from_user(temp);
ret = search_word_in_tree(&th, temp);
if (ret == 0) {
printf("%s found in tree\n", temp);
} else {
printf("%s not found in tree\n", temp);
}
}
return (0);
}