-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbtree.c
More file actions
152 lines (127 loc) · 2.06 KB
/
btree.c
File metadata and controls
152 lines (127 loc) · 2.06 KB
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
#include <stdio.h>
#include <stdlib.h>
#define RSIZE 1
#define WORD 16
typedef struct tnode Tnode;
struct tnode {
char *word;
int count;
Tnode *left;
Tnode *right;
};
int scmp(char *, char*);
char *sdup(char *);
int slen(char *);
void scpy(char *, char *, int);
Tnode *talloc();
Tnode *addtree(Tnode *, char *);
void treeprint(Tnode *);
int getwd(char *, int);
int isalph(int);
int getch(int);
void ungetch();
char buf[RSIZE];
char *sp, *ep;
int fd;
char wd[WORD];
Tnode *root;
int main(int argc, char **argv)
{
if (!(fd = open(argv[1], 0))) {
printf("err: open\n");
return -1;
}
while (getwd(wd, fd))
root = addtree(root, wd);
treeprint(root);
return 0;
}
Tnode *addtree(Tnode *p, char *w)
{
int cond;
if (!p) {
p = talloc();
p->word = sdup(w);
p->count = 1;
p->left = p->right = 0;
} else if ((cond = scmp(w, p->word)) == 0)
p->count++;
else if (cond < 0)
p->left = addtree(p->left, w);
else
p->right = addtree(p->right, w);
return p;
}
Tnode *talloc(void)
{
return (Tnode*) malloc(sizeof(Tnode));
}
void treeprint(Tnode *p)
{
if (p) {
treeprint(p->left);
printf("%4d %s\n", p->count, p->word);
treeprint(p->right);
}
}
int getwd(char *word, int fd)
{
int c;
char *w;
for (w = word; !isalph(c = getch(fd)); )
if (c <= 0)
goto term;
for ( ; isalph(c); c = getch(fd))
*w++ = c;
ungetch();
term: *w = 0;
return word[0];
}
void ungetch()
{
sp--;
}
int getch(int fd)
{
if (sp == ep) {
if ((read(fd, buf, RSIZE)) <= 0) //reconsider
return 0;
sp = buf;
ep = sp + RSIZE;
}
return *sp++;
}
int isalph(int c)
{
return (c >= 65 && c <= 90 || c >= 97 && c <= 122) ? 1 : 0;
}
void scpy(char *src, char *dst, int len)
{
int c;
for (c = 0; c < len; c++)
dst[c] = src[c];
}
char *sdup(char *s)
{
char *p;
int len;
len = slen(s);
p = malloc(sizeof(len + 1));
scpy(s, p, len);
return p;
}
int slen(char *s)
{
int n;
for (n = 0; s[n]; n++)
;
return n;
}
int scmp(char *s1, char *s2)
{
int c;
for (c = 0; s1[c] == s2[c]; c++)
if (s1[c] == 0)
return 0;
return s1[c] - s2[c];
}