Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 1f4f404

Browse files
minor tweaks
1 parent bc411df commit 1f4f404

File tree

1 file changed

+92
-90
lines changed

1 file changed

+92
-90
lines changed

‎chapter06/6-4.c

Lines changed: 92 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,18 @@
55
* By Faisal Saadatmand
66
*/
77

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+
911
#include <ctype.h>
10-
#include <string.h>
12+
#include <stdio.h>
1113
#include <stdlib.h>
14+
#include <string.h>
1215

1316
#define MAXWORD 100
1417
#define BUFSIZE 100
1518

19+
/* types */
1620
struct tnode {
1721
char *word;
1822
int count;
@@ -22,98 +26,43 @@ struct tnode {
2226

2327
/* functions */
2428
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 */
2731
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 */
3235

3336
/* 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 */
9039

9140
/* addtree: add a node with w, at or below p */
9241
struct tnode *addtree(struct tnode *p, char *w)
9342
{
9443
int cond;
9544

96-
if (!p) { /* a new word has arrived */
45+
if (!p) { /* a new word has arrived */
9746
p = talloc(); /* make a new node */
9847
p->word = strDup(w); /* copy data to it */
9948
p->count = 1;
10049
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 */
10352
else if (cond < 0) /* less than into left subtree */
10453
p->left = addtree(p->left, w);
10554
else
10655
p->right = addtree(p->right, w);
10756
return p;
10857
}
10958

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)
11261
{
11362
if (p) {
114-
treeprint(p->right);
63+
printtree(p->right);
11564
printf("%4d %s\n", p->count, p->word);
116-
treeprint(p->left);
65+
printtree(p->left);
11766
}
11867
}
11968

@@ -132,45 +81,98 @@ struct tnode *copyTree(struct tnode *p, struct tnode *root)
13281
return p;
13382
}
13483

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
13685
* to frequency of occurrence */
137-
struct tnode *sortTree(struct tnode *p, struct tnode *root)
86+
struct tnode *sorttree(struct tnode *p, struct tnode *root)
13887
{
88+
struct tnode *copyTree(struct tnode *, struct tnode *);
89+
13990
if (root) {
140-
p = sortTree(p, root->left);
91+
p = sorttree(p, root->left);
14192
p = copyTree(p, root);
142-
p = sortTree(p, root->right);
93+
p = sorttree(p, root->right);
14394
}
14495
return p;
14596
}
14697

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+
147115
/* freetree: free allocated heap memory of node tree */
148-
structtnode*freetree(struct tnode *node)
116+
voidfreetree(struct tnode *node)
149117
{
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;
155140
}
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;
157163
}
158164

159165
int main(void)
160166
{
161167
struct tnode *root; /* root node */
162-
struct tnode *sorted; /* root node to sorted tree */
163168
char word[MAXWORD]; /* currently read word */
164169

165-
root = sorted=NULL;
170+
root = NULL;
166171
while (getword(word, MAXWORD) != EOF)
167172
if (isalpha(word[0]))
168173
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;
175177
return 0;
176178
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /