Wed, 23 Feb 2005 19:05:01 +0100
Added dlist iteration macros.
65 | 1 | /* |
2 | Generic C code for red-black trees. | |
3 | Copyright (C) 2000 James S. Plank | |
4 | ||
5 | This library is free software; you can redistribute it and/or | |
6 | modify it under the terms of the GNU Lesser General Public | |
7 | License as published by the Free Software Foundation; either | |
8 | version 2.1 of the License, or (at your option) any later version. | |
9 | ||
10 | This library is distributed in the hope that it will be useful, | |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 | Lesser General Public License for more details. | |
14 | ||
15 | You should have received a copy of the GNU Lesser General Public | |
16 | License along with this library; if not, write to the Free Software | |
17 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
18 | */ | |
19 | ||
20 | /* Revision 1.2. Jim Plank */ | |
21 | ||
22 | #include "rb.h" | |
23 | #include <stdio.h> | |
24 | ||
25 | /* an example -- this allocates a red-black tree for integers. For a | |
26 | * user-specified number of iterations, it does the following: | |
27 | ||
28 | * delete a random element in the tree. | |
29 | * make two new random elements, and insert them into the tree | |
30 | ||
31 | * At the end, it prints the sorted list of elements, and then prints | |
32 | * stats of the number of black nodes in any path in the tree, and | |
33 | * the minimum and maximum path lengths. | |
34 | ||
35 | * Rb_find_ikey and rb_inserti could have been used, but instead | |
36 | * rb_find_gkey and rb_insertg were used to show how they work. | |
37 | ||
38 | */ | |
39 | ||
40 | int icomp(char *i, char *j) | |
41 | { | |
42 | int I, J; | |
43 | ||
44 | I = (int) i; | |
45 | J = (int) j; | |
46 | if (I == J) return 0; | |
47 | if (I > J) return -1; else return 1; | |
48 | } | |
49 | ||
50 | main(int argc, char **argv) | |
51 | { | |
52 | int i, j, tb, nb, mxp, mnp, p; | |
53 | int iterations; | |
54 | Rb_node argt, t; | |
55 | int *a; | |
56 | ||
57 | if (argc != 2) { | |
58 | fprintf(stderr, "usage: main #iterations\n"); | |
59 | exit(1); | |
60 | } | |
61 | argt = make_rb(); | |
62 | srandom(time(0)); | |
63 | iterations = atoi(argv[1]); | |
64 | a = (int *) malloc (iterations*sizeof(int)); | |
65 | ||
66 | for (i = 0; i < atoi(argv[1]); i++) { | |
67 | if (i > 0) { | |
68 | j = random()%i; | |
69 | ||
70 | rb_delete_node(rb_find_gkey(argt, (char *) (a[j]), icomp)); | |
71 | a[j] = random() % 1000; | |
72 | rb_insertg(argt, (char *) (a[j]), NULL, icomp); | |
73 | } | |
74 | a[i] = random() % 1000; | |
75 | rb_insertg(argt, (char *) (a[i]), NULL, icomp); | |
76 | } | |
77 | tb = 0; | |
78 | mxp = 0; | |
79 | mnp = 0; | |
80 | rb_traverse(t, argt) { | |
81 | printf("%d ", t->k.ikey); | |
82 | nb = rb_nblack(t); | |
83 | p = rb_plength(t); | |
84 | if (tb == 0) { | |
85 | tb = nb; | |
86 | } else if (tb != nb) { | |
87 | printf("Conflict: tb=%d, nb=%d\n", tb, nb); | |
88 | exit(1); | |
89 | } | |
90 | mxp = (mxp == 0 || mxp < p) ? p : mxp; | |
91 | mnp = (mnp == 0 || mxp > p) ? p : mnp; | |
92 | } | |
93 | printf("\n"); | |
94 | ||
95 | printf("Nblack = %d\n", tb); | |
96 | printf("Max = %d\n", mxp); | |
97 | printf("Min = %d\n", mnp); | |
98 | } |