-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathexample.c
More file actions
44 lines (41 loc) · 1.1 KB
/
example.c
File metadata and controls
44 lines (41 loc) · 1.1 KB
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
#include <stdio.h>
#include "splay.h"
int compare_int(void *l, void *r) {
return *((int*)l) - *((int*)r);
}
void output(splay_tree *tree) {
splay_node *node = first(tree);
while(node != NULL) {
printf("%d ", *(int*)node->value);
node = next(node);
}
printf("\n");
void **t = contents(tree);
for(int i = 0; i < tree->size; i++) {
printf("%d ", *(int*)t[i]);
}
printf("\n");
}
int main() {
splay_tree *tree = new_tree(&compare_int);
int values[] = {3, 4, 1, 2, 8, 5, 7};
output(tree);
for(int i = 0; i < 7; i++) {
insert(tree, &values[i]);
printf("+%d: min %d max %d\n", values[i],
*(int*)first(tree)->value, *(int*)last(tree)->value);
output(tree);
}
for(int i = 0; i < 7; i++) {
delete(tree, &values[i]);
printf("-%d: ", values[i]);
if(first(tree) == NULL) {
printf("EMPTY\n");
} else {
printf("min %d max %d\n",
*(int*)first(tree)->value, *(int*)last(tree)->value);
}
output(tree);
}
return 0;
}