-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
135 lines (108 loc) · 3.33 KB
/
main.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
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
#include <stdio.h>
#include <memory.h>
#include "eprintf.h"
typedef struct Nameval Nameval;
struct Nameval {
char *name;
int value;
Nameval *left; /* меньшее значение */
Nameval *right; /* большее значение */
};
/* insert: вставляет newp в дерево treep, возвращает treep */
Nameval *insert(Nameval *treep, Nameval *newp) {
int cmp;
if (treep == NULL) {
return newp;
}
cmp = strcmp(newp->name, treep->name);
if (cmp == 0) {
weprintf("insert: duplicate entry %s ignored",
newp->name);
} else if (cmp < 0) {
treep->left = insert(treep->left, newp);
} else {
treep->right = insert(treep->right, newp);
}
return treep;
}
/* lookup: ищет имя name в дереве treep */
Nameval *lookup(Nameval *treep, char *name) {
int cmp;
if (treep == NULL) {
return NULL;
}
cmp = strcmp(name, treep->name);
if (cmp == 0) {
return treep;
} else if (cmp < 0) {
return lookup(treep->left, name);
} else {
return lookup(treep->right, name);
}
}
/* nrlookup: нерекурсивный поиск имени name в дереве treep */
Nameval *nrlookup(Nameval *treep, char *name) {
int cmp;
while (treep != NULL) {
cmp = strcmp(name, treep->name);
if (cmp == 0) {
return treep;
} else if (cmp < 0) {
treep = treep->left;
} else {
treep = treep->right;
}
}
return NULL;
}
/* applyinorder: симметричное применение функции fn к treep */
void applyinorder(Nameval *treep,
void (*fn)(Nameval*, void*), void *arg) {
if (treep == NULL) {
return;
}
applyinorder(treep->left, fn, arg);
(*fn)(treep, arg);
applyinorder(treep->right, fn, arg);
}
/* applypostorder: концевой обход с вызовом fn */
void applypostorder(Nameval *treep,
void (*fn)(Nameval*, void*), void *arg) {
if (treep == NULL) {
return;
}
applypostorder(treep->left, fn, arg);
applypostorder(treep->right, fn, arg);
(*fn)(treep, arg);
}
/* printv: вывести имя и значения по строке формата arg */
void printv(Nameval *p, void *arg) {
char *fmt;
fmt = (char *) arg;
printf(fmt, p->name, p->value);
}
/* newitem: создает новый элемент по имени и значению */
Nameval *newitem(char *name, int value) {
Nameval *newp;
newp = (Nameval *) emalloc(sizeof(Nameval));
newp->name = name;
newp->value = value;
newp->right = NULL;
newp->left = NULL;
return newp;
}
int main() {
Nameval *treep = newitem("AElig", 0x00c6);
treep = insert(treep, newitem("zeta", 0x03b6));
treep = insert(treep, newitem("Acicrc", 0x00c2));
treep = insert(treep, newitem("AAcute", 0x00c1));
printf("\nApply in order printv: \n");
applyinorder(treep, printv, "%s: %x\n");
printf("\nApply post order printv: \n");
applypostorder(treep, printv, "%s: %x\n");
Nameval *zeta = lookup(treep, "zeta");
printf("\nlookup: %s: %x\n", zeta->name, zeta->value);
Nameval *aacute = nrlookup(treep, "AAcute");
printf("\nnrlookup: %s: %x\n", aacute->name, aacute->value);
return 0;
}