00001
00002
00003
00004
00005
00006
00007
00008
00009 #include <stdlib.h>
00010 #include <time.h>
00011 #include <iostream.h>
00012 #include <stdio.h>
00013
00014 #include <afx.h>
00015
00016 #include "TestTrees.h"
00017 #include "RedBlackTree.h"
00018 #include "AVLRedBlackTree.h"
00019 #include "TestAVLRedBlackTree.h"
00020 #include "AVLTree.h"
00021 #include "GLIBTree.h"
00022
00023 void TestRedBlackTree()
00024 {
00025 int *ptr;
00026 int *val, *ret;
00027 bool IsInsert;
00028
00029 RedBlackTree<int, int>* RBTree;
00030 SimpleCompareNodesAlgorithm<int, int> CompareAlgorithm;
00031
00032
00033
00034
00035 RBTree = new RedBlackTree<int, int>(&CompareAlgorithm);
00036
00037
00038 srand( (unsigned)time( NULL ) );
00039
00040 for (int i = 0; i < 12; i++)
00041 {
00042 ptr = new int;
00043 *ptr = i;
00044 val = RBTree->Search(ptr, &IsInsert);
00045 if(val != NULL)
00046 {
00047 cout << "ERROR" << endl;
00048 }
00049 }
00050
00051
00052 for(val=RBTree->Lookup(RBFIRST, NULL); val!=NULL; val=RBTree->Lookup(RBNEXT, val))
00053 cout << *val << endl;
00054
00055
00056 for(val=RBTree->Lookup(RBFIRST, NULL); val!=NULL; val=RBTree->Lookup(RBFIRST, val))
00057 {
00058 ret = RBTree->Delete(val);
00059 delete (int*) ret;
00060 }
00061
00062 delete RBTree;
00063
00064
00065
00066
00067
00068 RBTree = new RedBlackTree<int, int>(&CompareAlgorithm);
00069
00070 for (int j = 200; j > 0; j--)
00071 {
00072 ptr = new int;
00073 *ptr = j;
00074 val = RBTree->Search(ptr, &IsInsert);
00075 if(val != NULL)
00076 {
00077 cout << "ERROR" << endl;
00078 }
00079 }
00080
00081
00082 for(val=RBTree->Lookup(RBFIRST, NULL); val!=NULL; val=RBTree->Lookup(RBNEXT, val))
00083 cout << *val << endl;
00084
00085
00086 for(val=RBTree->Lookup(RBFIRST, NULL); val!=NULL; val=RBTree->Lookup(RBFIRST, val))
00087 {
00088 ret = RBTree->Delete(val);
00089 delete (int*) ret;
00090 }
00091
00092 delete RBTree;
00093
00094
00095
00096
00097
00098 RBTree = new RedBlackTree<int, int>(&CompareAlgorithm);
00099
00100 for (int k = 400; k > 0; k--)
00101 {
00102 ptr = new int;
00103 *ptr = k;
00104 val = RBTree->Search(ptr, &IsInsert);
00105 if(val != NULL)
00106 {
00107 cout << "ERROR" << endl;
00108 }
00109 }
00110
00111
00112
00113
00114
00115 for (int k1 = 400; k1 > 0; k1--)
00116 {
00117 val = RBTree->Delete(&k1);
00118 if(val == NULL)
00119 {
00120 cout << "ERROR" << endl;
00121 }
00122
00123 delete (int*) val;
00124 }
00125
00126 delete RBTree;
00127
00128
00129
00130
00131
00132 RBTree = new RedBlackTree<int, int>(&CompareAlgorithm);
00133
00134 for (int m = 200; m > 0; m--)
00135 {
00136 ptr = new int;
00137 *ptr = m;
00138 val = RBTree->Find(ptr);
00139 if(val != NULL)
00140 {
00141 cout << "ERROR" << endl;
00142 }
00143
00144
00145 delete ptr;
00146 }
00147
00148
00149 for(val=RBTree->Lookup(RBFIRST, NULL); val!=NULL; val=RBTree->Lookup(RBFIRST, val))
00150 {
00151 cout << *val << endl;
00152 ret = RBTree->Delete(val);
00153 delete (int*) ret;
00154 }
00155
00156 delete RBTree;
00157 }
00158
00160 #define TREE_SIZE 16
00161
00162 #define N_ITERATIONS 1024
00163
00164 void TestAVLRedBlackTree(int argc, char **argv)
00165 {
00166 int array[TREE_SIZE];
00167 int seed;
00168 bool IsDone = false;
00169 bool IsInsert;
00170
00171 if (argc == 2)
00172 seed = atoi(argv[1]);
00173 else
00174 seed = time(0) *257%32768;
00175
00176 SimpleCompareNodesAlgorithm<int, int> CompareAlgorithm;
00177
00178 for (int iteration = 1; iteration <= N_ITERATIONS; iteration++)
00179 {
00180 AVLRedBlackTree<int, int> *tree;
00181 int i;
00182
00183 printf("Iteration %4d/%4d: seed=%5d", iteration, N_ITERATIONS, seed);
00184 fflush(stdout);
00185
00186 srand(seed++);
00187
00188 for (i = 0; i < TREE_SIZE; i++)
00189 array[i] = i;
00190 Shuffle(array, TREE_SIZE);
00191
00192 tree = new AVLRedBlackTree<int, int>(&CompareAlgorithm);
00193 for (i = 0; i< TREE_SIZE; i++)
00194 tree->Search(&(array[i]), &IsInsert);
00195
00196
00197
00198
00199 int copy_array[TREE_SIZE];
00200 for (i = 0; i<TREE_SIZE; i++)
00201 copy_array[i] = array[i];
00202
00203 Shuffle(copy_array, TREE_SIZE);
00204 for(i = 0; i < TREE_SIZE; i++)
00205 {
00206 tree->Delete(©_array[i]);
00207
00208
00209
00210 AVLRedBlackTree<int, int> *copy;
00211 copy = tree->Copy();
00212
00213
00214
00215 bool same = tree->Compare(copy);
00216 delete copy;
00217
00218 if (!same)
00219 return;
00220 }
00221
00222 if (i%128 == 0)
00223 {
00224 printf(".");
00225 }
00226
00227 printf("\ttest good.\n");
00228 fflush(stdout);
00229
00230 delete tree;
00231 }
00232 }
00233
00234 void TestAVLTree(int argc, char **argv)
00235 {
00236 int array[TREE_SIZE];
00237 int seed;
00238 bool IsInsert;
00239
00240 if (argc == 2)
00241 seed = atoi(argv[1]);
00242 else
00243 seed = time(0) *257%32768;
00244
00245 SimpleCompareNodesAlgorithm<int, int> CompareAlgorithm;
00246
00247 for (int iteration = 1; iteration <= N_ITERATIONS; iteration++)
00248 {
00249 AVLTree<int, int> *tree;
00250 int i;
00251
00252 printf("Iteration %4d/%4d: seed=%5d", iteration, N_ITERATIONS, seed);
00253 fflush(stdout);
00254
00255 srand(seed++);
00256
00257 for (i = 0; i < TREE_SIZE; i++)
00258 array[i] = i;
00259
00260 Shuffle(array, TREE_SIZE);
00261
00262 tree = new AVLTree<int, int>(&CompareAlgorithm);
00263 for (i = 0; i< TREE_SIZE; i++)
00264 tree->Search(&(array[i]), &IsInsert);
00265
00266
00267
00268
00269 for(i = 0; i < TREE_SIZE; i++)
00270 {
00271 tree->Delete(&(array[i]));
00272
00273
00274
00275 }
00276
00277 printf("\ttest good.\n");
00278 fflush(stdout);
00279
00280 delete tree;
00281 }
00282 }
00283
00284 void TestGLIBTree(int argc, char **argv)
00285 {
00286 int array[TREE_SIZE];
00287 int seed;
00288 bool IsInsert;
00289
00290 if (argc == 2)
00291 seed = atoi(argv[1]);
00292 else
00293 seed = time(0) *257%32768;
00294
00295 SimpleCompareNodesAlgorithm<int, int> CompareAlgorithm;
00296
00297 for (int iteration = 1; iteration <= N_ITERATIONS; iteration++)
00298 {
00299 GLIBTree<int, int> *tree;
00300 int i;
00301
00302 printf("Iteration %4d/%4d: seed=%5d", iteration, N_ITERATIONS, seed);
00303 fflush(stdout);
00304
00305 srand(seed++);
00306
00307 for (i = 0; i < TREE_SIZE; i++)
00308 array[i] = i;
00309
00310 Shuffle(array, TREE_SIZE);
00311
00312 tree = new GLIBTree<int, int>(&CompareAlgorithm);
00313 for (i = 0; i< TREE_SIZE; i++)
00314 tree->Search(&(array[i]), &IsInsert);
00315
00316
00317
00318
00319 for(i = 0; i < TREE_SIZE; i++)
00320 {
00321 tree->Delete(&(array[i]));
00322
00323
00324
00325 }
00326
00327 printf("\ttest good.\n");
00328 fflush(stdout);
00329
00330 delete tree;
00331 }
00332 }