-
Notifications
You must be signed in to change notification settings - Fork 0
/
6.binary_tree.c
66 lines (58 loc) · 1.43 KB
/
6.binary_tree.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_WORD_LEN 1000
int getword(char *word, int limit);
struct t_node {
char *word;
int count;
struct t_node *left;
struct t_node *right;
};
void print_tree(struct t_node *p) {
if (p != NULL) {
print_tree(p->left);
printf("word=%s, count=%d\n", p->word, p->count);
print_tree(p->right);
}
}
char *mystrdup(char *s) {
char *p = malloc(strlen(s)+1);
strcpy(p, s);
return p;
}
struct t_node *add_node(struct t_node *p, struct t_node *node) {
if (p == NULL) {
p = malloc(sizeof(struct t_node));
p->word = mystrdup(node->word);
p->count = 1;
p->left = NULL;
p->right =NULL;
} else {
int cond = strcmp(node->word, p->word);
if (cond > 0) {
p->right = add_node(p->right, node);
} else if (cond < 0) {
p->left = add_node(p->left, node);
} else {
p->count += 1;
}
}
return p;
}
int main() {
int len;
char word[MAX_WORD_LEN];
struct t_node *root;
struct t_node *node;
while (getword(word, MAX_WORD_LEN) != EOF) {
printf("%s\n", word);
node = (struct t_node *)malloc(sizeof(struct t_node));
node->word = mystrdup(word);
node->count = 1;
node->left = NULL;
node->right = NULL;
root = add_node(root, node);
}
print_tree(root);
}