/
P92WordFrequency.c
110 lines (94 loc) · 2.82 KB
/
P92WordFrequency.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
// Program P9.2
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#define MaxWordSize 20
typedef struct {
char word[MaxWordSize+1];
int freq;
} NodeData;
typedef struct treeNode {
NodeData data;
struct treeNode *left, *right;
} TreeNode, *TreeNodePtr;
typedef struct {
TreeNodePtr root;
} BinaryTree;
int main() {
int getWord(FILE *, char[]);
TreeNodePtr newTreeNode(NodeData);
NodeData newNodeData(char[], int);
TreeNodePtr findOrInsert(BinaryTree, NodeData);
void inOrder(FILE *, TreeNodePtr);
char word[MaxWordSize+1];
FILE * in = fopen("wordFreq.in", "r");
FILE * out = fopen("wordFreq.out", "w");
BinaryTree bst;
bst.root = NULL;
while (getWord(in, word) != 0) {
if (bst.root == NULL)
bst.root = newTreeNode(newNodeData(word, 1));
else {
TreeNodePtr node = findOrInsert(bst, newNodeData(word, 0));
node -> data.freq++;
}
}
fprintf(out, "\nWords Frequency\n\n");
inOrder(out, bst.root); fprintf(out, "\n\n");
fclose(in);
fclose(out);
} // end main
int getWord(FILE * in, char str[]) {
// stores the next word, if any, in str; word is converted to lowercase
// returns 1 if a word is found; 0, otherwise
char ch;
int n = 0;
// read over non-letters
while (!isalpha(ch = getc(in)) && ch != EOF) ; //empty while body
if (ch == EOF) return 0;
str[n++] = tolower(ch);
while (isalpha(ch = getc(in)) && ch != EOF)
if (n < MaxWordSize) str[n++] = tolower(ch);
str[n] = '\0';
return 1;
} // end getWord
TreeNodePtr findOrInsert(BinaryTree bt, NodeData d) {
//searches for d; if found, return pointer to node containing d
//else insert a node containing d and return pointer to new node
TreeNodePtr newTreeNode(NodeData);
if (bt.root == NULL) return newTreeNode(d);
TreeNodePtr curr = bt.root;
int cmp;
while ((cmp = strcmp(d.word, curr -> data.word)) != 0) {
if (cmp < 0) { //try left
if (curr -> left == NULL) return curr -> left = newTreeNode(d);
curr = curr -> left;
}
else { //try right
if (curr -> right == NULL) return curr -> right = newTreeNode(d);
curr = curr -> right;
}
}
//d is in the tree; return pointer to the node
return curr;
} //end findOrInsert
TreeNodePtr newTreeNode(NodeData d) {
TreeNodePtr p = (TreeNodePtr) malloc(sizeof(TreeNode));
p -> data = d;
p -> left = p -> right = NULL;
return p;
} //end newTreeNode
void inOrder(FILE * out, TreeNodePtr node) {
if (node!= NULL) {
inOrder(out, node -> left);
fprintf(out, "%-15s %2d\n", node -> data.word, node -> data.freq);
inOrder(out, node -> right);
}
} //end inOrder
NodeData newNodeData(char str[], int n) {
NodeData temp;
strcpy(temp.word, str);
temp.freq = n;
return temp;
} //end newNodeData