5
5
* By Faisal Saadatmand
6
6
*/
7
7
8
- #include <stdio.h>
8
+ /* NOTE: I included a definition for sorttree and copytree for academic
9
+ * purposes. They are not used in the code */
10
+
9
11
#include <ctype.h>
10
- #include <string .h>
12
+ #include <stdio .h>
11
13
#include <stdlib.h>
14
+ #include <string.h>
12
15
13
16
#define MAXWORD 100
14
17
#define BUFSIZE 100
15
18
19
+ /* types */
16
20
struct tnode {
17
21
char * word ;
18
22
int count ;
@@ -22,98 +26,43 @@ struct tnode {
22
26
23
27
/* functions */
24
28
int getword (char * , int );
25
- struct tnode * talloc (void ); /* allocate memory to new tree node */
26
- char * strDup (char * ); /* copy string into safe place */
29
+ struct tnode * talloc (void ); /* allocate memory to new tree node */
30
+ char * strDup (char * ); /* copy string into safe place */
27
31
struct tnode * addtree (struct tnode * , char * );
28
- void treeprint (struct tnode * );
29
- struct tnode * copyTree (struct tnode * , struct tnode * );
30
- struct tnode * sortTree (struct tnode * , struct tnode * );
31
- struct tnode * freetree (struct tnode * );
32
+ void printtree (struct tnode * );
33
+ void freetree (struct tnode * );
34
+ struct tnode * sorttree (struct tnode * , struct tnode * ); /* optional */
32
35
33
36
/* globals */
34
- int buf [BUFSIZE ]; /* buffer from ungetch */
35
- int bufp = 0 ; /* next free position in buf */
36
-
37
- int getch (void ) /* get a (possibly pushed back) character */
38
- {
39
- return (bufp > 0 ) ? buf [-- bufp ] : getchar ();
40
- }
41
-
42
- void ungetch (int c ) /* push character back on input */
43
- {
44
- if (bufp >= BUFSIZE )
45
- printf ("ungetch: too many characters\n" );
46
- else
47
- buf [bufp ++ ] = c ;
48
- }
49
-
50
- /* getword: get next word or character from input */
51
- int getword (char * word , int lim )
52
- {
53
- int c , getch (void );
54
- void ungetch (int );
55
- char * w = word ;
56
-
57
- while (isspace (c = getch ()))
58
- ;
59
- if (c != EOF )
60
- * w ++ = c ;
61
- if (!isalpha (c )) {
62
- * w = '\0' ;
63
- return c ;
64
- }
65
- for ( ; -- lim > 0 ; w ++ )
66
- if (!isalnum (* w = getch ())) {
67
- ungetch (* w );
68
- break ;
69
- }
70
- * w = '\0' ;
71
- return word [0 ];
72
- }
73
-
74
- /* talloc: make a tnode */
75
- struct tnode * talloc (void )
76
- {
77
- return malloc (sizeof (struct tnode ));
78
- }
79
-
80
- /*strDup: make a duplicate of s */
81
- char * strDup (char * s )
82
- {
83
- char * p ;
84
-
85
- p = malloc (strlen (s ) + 1 ); /* +1 for '\0' */
86
- if (p )
87
- strcpy (p , s );
88
- return p ;
89
- }
37
+ int buf [BUFSIZE ]; /* buffer from ungetch */
38
+ int bufp = 0 ; /* next free position in buf */
90
39
91
40
/* addtree: add a node with w, at or below p */
92
41
struct tnode * addtree (struct tnode * p , char * w )
93
42
{
94
43
int cond ;
95
44
96
- if (!p ) { /* a new word has arrived */
45
+ if (!p ) { /* a new word has arrived */
97
46
p = talloc (); /* make a new node */
98
47
p -> word = strDup (w ); /* copy data to it */
99
48
p -> count = 1 ;
100
49
p -> left = p -> right = NULL ;
101
- } else if ((cond = strcmp (w , p -> word )) == 0 )
102
- p -> count ++ ; /* repeated word */
50
+ } else if (! (cond = strcmp (w , p -> word )))
51
+ ++ p -> count ; /* repeated word */
103
52
else if (cond < 0 ) /* less than into left subtree */
104
53
p -> left = addtree (p -> left , w );
105
54
else
106
55
p -> right = addtree (p -> right , w );
107
56
return p ;
108
57
}
109
58
110
- /* print: in-order print of tree p */
111
- void treeprint (struct tnode * p )
59
+ /* print: in-order print of tree p - decreasing order version */
60
+ void printtree (struct tnode * p )
112
61
{
113
62
if (p ) {
114
- treeprint (p -> right );
63
+ printtree (p -> right );
115
64
printf ("%4d %s\n" , p -> count , p -> word );
116
- treeprint (p -> left );
65
+ printtree (p -> left );
117
66
}
118
67
}
119
68
@@ -132,45 +81,98 @@ struct tnode *copyTree(struct tnode *p, struct tnode *root)
132
81
return p ;
133
82
}
134
83
135
- /* sortTree : performs inorder traversal on root and creates a BST p according
84
+ /* sorttree : performs inorder traversal on root and creates a BST p according
136
85
* to frequency of occurrence */
137
- struct tnode * sortTree (struct tnode * p , struct tnode * root )
86
+ struct tnode * sorttree (struct tnode * p , struct tnode * root )
138
87
{
88
+ struct tnode * copyTree (struct tnode * , struct tnode * );
89
+
139
90
if (root ) {
140
- p = sortTree (p , root -> left );
91
+ p = sorttree (p , root -> left );
141
92
p = copyTree (p , root );
142
- p = sortTree (p , root -> right );
93
+ p = sorttree (p , root -> right );
143
94
}
144
95
return p ;
145
96
}
146
97
98
+ /* talloc: make a tnode */
99
+ struct tnode * talloc (void )
100
+ {
101
+ return malloc (sizeof (struct tnode ));
102
+ }
103
+
104
+ /*strDup: make a duplicate of s */
105
+ char * strDup (char * s )
106
+ {
107
+ char * p ;
108
+
109
+ p = malloc (strlen (s ) + 1 ); /* +1 for '\0' */
110
+ if (p )
111
+ strcpy (p , s );
112
+ return p ;
113
+ }
114
+
147
115
/* freetree: free allocated heap memory of node tree */
148
- struct tnode * freetree (struct tnode * node )
116
+ void freetree (struct tnode * node )
149
117
{
150
- if (node ) {
151
- freetree (node -> left );
152
- freetree (node -> right );
153
- free (node -> word );
154
- free (node );
118
+ if (!node )
119
+ return ;
120
+ freetree (node -> left );
121
+ freetree (node -> right );
122
+ free (node -> word );
123
+ free (node );
124
+ }
125
+
126
+ /* getword: get next word or character from input */
127
+ int getword (char * word , int lim )
128
+ {
129
+ int c , getch (void );
130
+ void ungetch (int );
131
+ char * w = word ;
132
+
133
+ while (isblank (c = getch ()))
134
+ ;
135
+ if (c != EOF )
136
+ * w ++ = c ;
137
+ if (!isalpha (c )) {
138
+ * w = '\0' ;
139
+ return c ;
155
140
}
156
- return node ;
141
+ for ( ; -- lim > 0 ; w ++ )
142
+ if (!isalnum (* w = getch ())) {
143
+ ungetch (* w );
144
+ break ;
145
+ }
146
+ * w = '\0' ;
147
+ return word [0 ];
148
+ }
149
+
150
+ /* get a (possibly pushed back) character */
151
+ int getch (void )
152
+ {
153
+ return (bufp > 0 ) ? buf [-- bufp ] : getchar ();
154
+ }
155
+
156
+ /* push character back on input */
157
+ void ungetch (int c )
158
+ {
159
+ if (bufp >= BUFSIZE )
160
+ printf ("ungetch: too many characters\n" );
161
+ else
162
+ buf [bufp ++ ] = c ;
157
163
}
158
164
159
165
int main (void )
160
166
{
161
167
struct tnode * root ; /* root node */
162
- struct tnode * sorted ; /* root node to sorted tree */
163
168
char word [MAXWORD ]; /* currently read word */
164
169
165
- root = sorted = NULL ;
170
+ root = NULL ;
166
171
while (getword (word , MAXWORD ) != EOF )
167
172
if (isalpha (word [0 ]))
168
173
root = (addtree (root , word )); /* build tree */
169
- sorted = sortTree (sorted , root );
170
- treeprint (sorted );
171
- /* clean up */
172
- root = freetree (root );
173
- sorted = freetree (sorted );
174
- root = sorted = NULL ;
174
+ printtree (root );
175
+ freetree (root );
176
+ root = NULL ;
175
177
return 0 ;
176
178
}
0 commit comments