-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_bst.c
More file actions
154 lines (143 loc) · 3.95 KB
/
Copy pathtest_bst.c
File metadata and controls
154 lines (143 loc) · 3.95 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
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include <stdlib.h>
#include <stdio.h>
#include "xl_util.h"
#include "xl_bst.h"
#include "xl_visual.h"
/* test code */
typedef struct st {
char * a;
int key;
char* pad;
}st;
int compare_st(const void *a, const void *b){
// printf("a:0x%016lx\tb:0x%016lx\n", (int64_t)a, (int64_t)b);
return ((st*)a)->key - ((st*)b)->key;
}
void visit_st(const xl_bst_node_t *node, void *data){
char *a = (char*)malloc(sizeof(char) * 17);
printf("visit:\t%s %d(%lu)\n", xl_print_addr(node, a), ((st*)(node->data))->key, node->cnt);
printf("visit:\t0x%016lx %d(%lu)\n", (int64_t)node, ((st*)(node->data))->key, node->cnt);
free(a);
}
int get_key(const void *data){
return ((st*)data)->key;
}
int
main(int argc, char* argv[]){
xl_bst_t *tree = xl_bst_init(compare_st);
int tmp;
st *st_tmp;
xl_bst_node_t *st_ret;
int insert_ret, find_ret, delete_ret, rotate_ret, visual_ret;
//insert loop
while(1){
printf("insert loop:\n");
while(scanf("%d", &tmp) != EOF){
st_tmp = (st*)malloc(sizeof(st));
st_tmp->a = "a";
st_tmp->key = tmp;
st_tmp->pad = "pad";
printf("st_tmp:");
xl_print_addr(st_tmp, NULL);
printf("\n");
printf("key:%d\n", st_tmp->key);
insert_ret = xl_bst_insert(tree, st_tmp);
if(insert_ret != 0) {
printf("insert error!\n");
}
xl_bst_inorder_traverse(tree, visit_st, NULL);
printf("////////////\n");
xl_bst_preorder_traverse(tree, visit_st, NULL);
printf("////////////\n");
xl_bst_postorder_traverse(tree, visit_st, NULL);
printf("\n");
}
visual_ret = xl_visual_bst(tree, "/root/algo/visual/bst.gv", get_key);
if(0 != visual_ret) printf("visual error!\n");
printf("find loop:\n");
//search loop
while(scanf("%d", &tmp) != EOF){
st_tmp = (st*)malloc(sizeof(st));
st_tmp->a = "a";
st_tmp->key = tmp;
st_tmp->pad = "pad";
find_ret = xl_bst_find(tree, st_tmp, &st_ret);
if(find_ret != 0) {
printf("find fail!\n");
}
free(st_tmp);
xl_bst_inorder_traverse(tree, visit_st, NULL);
printf("////////////\n");
xl_bst_preorder_traverse(tree, visit_st, NULL);
printf("////////////\n");
xl_bst_postorder_traverse(tree, visit_st, NULL);
printf("\n");
}
printf("delete loop:\n");
//search loop
while(scanf("%d", &tmp) != EOF){
st_tmp = (st*)malloc(sizeof(st));
st_tmp->a = "a";
st_tmp->key = tmp;
st_tmp->pad = "pad";
delete_ret = xl_bst_delete(tree, st_tmp);
printf("root:");
xl_print_addr(tree->root, NULL);
printf("\n");
if(delete_ret != 0) {
printf("delete fail!\n");
}
free(st_tmp);
xl_bst_inorder_traverse(tree, visit_st, NULL);
printf("////////////\n");
xl_bst_preorder_traverse(tree, visit_st, NULL);
printf("////////////\n");
xl_bst_postorder_traverse(tree, visit_st, NULL);
printf("\n");
}
printf("left rotate loop:\n");
//search loop
while(scanf("%d", &tmp) != EOF){
st_tmp = (st*)malloc(sizeof(st));
st_tmp->a = "a";
st_tmp->key = tmp;
st_tmp->pad = "pad";
rotate_ret = xl_bst_left_rotate(tree, st_tmp);
printf("root:");
xl_print_addr(tree->root, NULL);
printf("\n");
if(rotate_ret != 0) {
printf("left rotate fail!\n");
}
free(st_tmp);
xl_bst_inorder_traverse(tree, visit_st, NULL);
printf("////////////\n");
xl_bst_preorder_traverse(tree, visit_st, NULL);
printf("////////////\n");
xl_bst_postorder_traverse(tree, visit_st, NULL);
printf("\n");
}
printf("right rotate loop:\n");
//search loop
while(scanf("%d", &tmp) != EOF){
st_tmp = (st*)malloc(sizeof(st));
st_tmp->a = "a";
st_tmp->key = tmp;
st_tmp->pad = "pad";
rotate_ret = xl_bst_right_rotate(tree, st_tmp);
printf("root:");
xl_print_addr(tree->root, NULL);
printf("\n");
if(rotate_ret != 0) {
printf("right rotate fail!\n");
}
free(st_tmp);
xl_bst_inorder_traverse(tree, visit_st, NULL);
printf("////////////\n");
xl_bst_preorder_traverse(tree, visit_st, NULL);
printf("////////////\n");
xl_bst_postorder_traverse(tree, visit_st, NULL);
printf("\n");
}
}
}