The data contained in this repository can be downloaded to your computer using one of several clients.
Please see the documentation of your version control software client for more information.
Please select the desired protocol below to get the URL.
This URL has Read-Only access.
main_repo / deps / libebb / test_rbtree.c @ 40c0f755
History | View | Annotate | Download (3.1 KB)
1 |
/* Copyright (c) 2008 Derrick Coetzee
|
---|---|
2 |
* http://en.literateprograms.org/Red-black_tree_(C)?oldid=7982
|
3 |
*
|
4 |
* Permission is hereby granted, free of charge, to any person obtaining a
|
5 |
* copy of this software and associated documentation files (the
|
6 |
* "Software"), to deal in the Software without restriction, including
|
7 |
* without limitation the rights to use, copy, modify, merge, publish,
|
8 |
* distribute, sublicense, and/or sell copies of the Software, and to permit
|
9 |
* persons to whom the Software is furnished to do so, subject to the
|
10 |
* following conditions:
|
11 |
*
|
12 |
* The above copyright notice and this permission notice shall be included
|
13 |
* in all copies or substantial portions of the Software.
|
14 |
*
|
15 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
16 |
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
17 |
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
18 |
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
19 |
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
|
20 |
* OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
|
21 |
* THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
22 |
*/
|
23 |
|
24 |
#include "rbtree.h" |
25 |
#include <stdio.h> |
26 |
#include <assert.h> |
27 |
#include <stdlib.h> /* rand(), malloc(), free() */ |
28 |
|
29 |
static int compare_int(void* left, void* right); |
30 |
static void print_tree(rbtree t); |
31 |
static void print_tree_helper(rbtree_node n, int indent); |
32 |
|
33 |
int compare_int(void* leftp, void* rightp) { |
34 |
int left = (int)leftp; |
35 |
int right = (int)rightp; |
36 |
if (left < right)
|
37 |
return -1; |
38 |
else if (left > right) |
39 |
return 1; |
40 |
else {
|
41 |
assert (left == right); |
42 |
return 0; |
43 |
} |
44 |
} |
45 |
|
46 |
#define INDENT_STEP 4 |
47 |
|
48 |
void print_tree_helper(rbtree_node n, int indent); |
49 |
|
50 |
void print_tree(rbtree t) {
|
51 |
print_tree_helper(t->root, 0);
|
52 |
puts("");
|
53 |
} |
54 |
|
55 |
void print_tree_helper(rbtree_node n, int indent) { |
56 |
int i;
|
57 |
if (n == NULL) { |
58 |
fputs("<empty tree>", stdout);
|
59 |
return;
|
60 |
} |
61 |
if (n->right != NULL) { |
62 |
print_tree_helper(n->right, indent + INDENT_STEP); |
63 |
} |
64 |
for(i=0; i<indent; i++) |
65 |
fputs(" ", stdout);
|
66 |
if (n->color == BLACK)
|
67 |
printf("%d\n", (int)n->key); |
68 |
else
|
69 |
printf("<%d>\n", (int)n->key); |
70 |
if (n->left != NULL) { |
71 |
print_tree_helper(n->left, indent + INDENT_STEP); |
72 |
} |
73 |
} |
74 |
|
75 |
int main() {
|
76 |
int i;
|
77 |
struct rbtree_t t;
|
78 |
rbtree_init(&t, compare_int); |
79 |
print_tree(&t); |
80 |
|
81 |
for(i=0; i<5000; i++) { |
82 |
int x = rand() % 10000; |
83 |
int y = rand() % 10000; |
84 |
#ifdef TRACE
|
85 |
print_tree(&t); |
86 |
printf("Inserting %d -> %d\n\n", x, y);
|
87 |
#endif
|
88 |
rbtree_node node = (rbtree_node) malloc(sizeof(struct rbtree_node_t)); |
89 |
node->key = (void*)x;
|
90 |
node->value = (void*)y;
|
91 |
rbtree_insert(&t, node); |
92 |
assert(rbtree_lookup(&t, (void*)x) == (void*)y); |
93 |
} |
94 |
for(i=0; i<60000; i++) { |
95 |
int x = rand() % 10000; |
96 |
#ifdef TRACE
|
97 |
print_tree(&t); |
98 |
printf("Deleting key %d\n\n", x);
|
99 |
#endif
|
100 |
rbtree_node n = rbtree_delete(&t, (void*)x);
|
101 |
if(n != NULL) { |
102 |
free(n); |
103 |
} |
104 |
} |
105 |
printf("Okay\n");
|
106 |
return 0; |
107 |
} |
108 |
|