testSPLAVL.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167
  1. /*Dylan Jeffers
  2. *Tahmid Rahman
  3. *founders of RahmROMJeffers ltd.
  4. *
  5. *Test script for BST and AVL - AVL tests
  6. *are at the bottom of the script
  7. */
  8. #include <stdlib.h> // Used for pseudo-random number generation.
  9. #include <assert.h> // Used for testing below.
  10. #include <iostream>
  11. #include "pair.h"
  12. #include "BST.h"
  13. #include "library/circularArrayList.h"
  14. #include "library/queue.h"
  15. #include "AVLTree.h"
  16. #include "SPLAVL.h"
  17. #include "SPLAVL.h"
  18. using namespace std;
  19. void insertTest();
  20. void updateTest();
  21. void findTest();
  22. void testMaxMin();
  23. void testgetHeight();
  24. void SPLAYRemoveTest();
  25. void AVLRemoveTest();
  26. void AVLInsertTest();
  27. void SPLAVLinsertTest();
  28. int main() {
  29. insertTest();
  30. SPLAVLinsertTest();
  31. findTest();
  32. updateTest();
  33. SPLAYRemoveTest();
  34. AVLRemoveTest();
  35. AVLInsertTest();
  36. cout << "Passed SPLAVL tests!" << endl;
  37. /*
  38. removeTest();
  39. findTest();
  40. testMaxMin();
  41. testgetHeight();
  42. AVLInsertTest();
  43. AVLRemoveTest();
  44. */
  45. return 0;
  46. }
  47. /* insertTest - accomplishes the following
  48. * *tests getSize
  49. *
  50. * *ensures a new tree is indeed empty
  51. *
  52. * *ensures each insert increases the size by 1
  53. *
  54. * *tests that each inserted element is in the tree
  55. *
  56. * *tests that each inserted element is splayed to the right spot
  57. * by checking all four traversal algorithms on all five
  58. * elementary subtrees (i.e. with 3 nodes)
  59. */
  60. void insertTest() {
  61. SPLAVL<int,int> BST;
  62. BST.setMaxCount(1000);
  63. //Queue< Pair<int,int> >* queue0;
  64. SPLAVL<int, char> SBST1, SBST2, SBST3, SBST4, SBST5;
  65. SBST1.setMaxCount(1000);
  66. SBST2.setMaxCount(1000);
  67. SBST3.setMaxCount(1000);
  68. SBST4.setMaxCount(1000);
  69. SBST5.setMaxCount(1000);
  70. Queue< Pair<int,char> >* queue1;
  71. Queue< Pair<int,char> >* queue2;
  72. Queue< Pair<int,char> >* queue3;
  73. Queue< Pair<int,char> >* queue4;
  74. assert(BST.getSize() == 0); // Checks that initial size is correct. assert
  75. // causes the program to immediately crash if
  76. // the condition is false.
  77. for (int i = 0; i < 100; ++i) {
  78. BST.insert(2*i + 1, i);
  79. assert(BST.getSize() == i+1);
  80. assert(BST.getRootKey() == 2*i+1);
  81. }
  82. /* queue0 = BST.getInOrder();
  83. while (!queue0->isEmpty()) {
  84. cout << queue0->dequeue().second << " ";
  85. cout.flush();
  86. }
  87. */
  88. for (int i = 0; i < 100; ++i) { // Checks that keys are in the tree.
  89. assert(BST.contains(2*i + 1));
  90. assert(BST.getRootKey() == 2*i+1);
  91. }
  92. for (int i = 0; i < 100; ++i) {
  93. BST.insert(-2*i - 1, i);
  94. assert(BST.getSize() == i+1 + 100);
  95. }
  96. for (int i = 0; i < 100; ++i) { // Checks that keys are in the tree.
  97. assert(BST.contains(-2*i - 1));
  98. }
  99. for (int i = 0; i < 100; ++i) { //Error returned if key already exists.
  100. try{
  101. BST.insert(2*i + 1, i);
  102. assert(false);
  103. } catch(runtime_error& exc){}
  104. }
  105. //===============================================
  106. /* The following tests each tree traversal algorithm on
  107. * all five elementary subtrees.
  108. */
  109. SBST1.insert(1, 'A');
  110. SBST1.insert(2, 'B');
  111. SBST1.insert(3, 'C');
  112. queue1 = SBST1.getPostOrder();
  113. queue2 = SBST1.getPreOrder();
  114. queue3 = SBST1.getInOrder();
  115. queue4 = SBST1.getLevelOrder();
  116. assert(queue1->dequeue().second == 'A');
  117. assert(queue1->dequeue().second == 'B');
  118. assert(queue1->dequeue().second == 'C');
  119. assert(queue2->dequeue().second == 'C');
  120. assert(queue2->dequeue().second == 'B');
  121. assert(queue2->dequeue().second == 'A');
  122. assert(queue3->dequeue().second == 'A');
  123. assert(queue3->dequeue().second == 'B');
  124. assert(queue3->dequeue().second == 'C');
  125. assert(queue4->dequeue().second == 'C');
  126. assert(queue4->dequeue().second == 'B');
  127. assert(queue4->dequeue().second == 'A');
  128. delete queue1;
  129. delete queue2;
  130. delete queue3;
  131. delete queue4;
  132. //===============================================
  133. SBST2.insert(3, 'C');
  134. SBST2.insert(2, 'B');
  135. SBST2.insert(1, 'A');
  136. queue1 = SBST2.getPostOrder();
  137. queue2 = SBST2.getPreOrder();
  138. queue3 = SBST2.getInOrder();
  139. queue4 = SBST2.getLevelOrder();
  140. assert(queue1->dequeue().second == 'C');
  141. assert(queue1->dequeue().second == 'B');
  142. assert(queue1->dequeue().second == 'A');
  143. assert(queue2->dequeue().second == 'A');
  144. assert(queue2->dequeue().second == 'B');
  145. assert(queue2->dequeue().second == 'C');
  146. assert(queue3->dequeue().second == 'A');
  147. assert(queue3->dequeue().second == 'B');
  148. assert(queue3->dequeue().second == 'C');
  149. assert(queue4->dequeue().second == 'A');
  150. assert(queue4->dequeue().second == 'B');
  151. assert(queue4->dequeue().second == 'C');
  152. delete queue1;
  153. delete queue2;
  154. delete queue3;
  155. delete queue4;
  156. //===============================================
  157. SBST3.insert(2, 'B');
  158. SBST3.insert(1, 'A');
  159. SBST3.insert(3, 'C');
  160. queue1 = SBST3.getPostOrder();
  161. queue2 = SBST3.getPreOrder();
  162. queue3 = SBST3.getInOrder();
  163. queue4 = SBST3.getLevelOrder();
  164. assert(queue1->dequeue().second == 'B');
  165. assert(queue1->dequeue().second == 'A');
  166. assert(queue1->dequeue().second == 'C');
  167. assert(queue2->dequeue().second == 'C');
  168. assert(queue2->dequeue().second == 'A');
  169. assert(queue2->dequeue().second == 'B');
  170. assert(queue3->dequeue().second == 'A');
  171. assert(queue3->dequeue().second == 'B');
  172. assert(queue3->dequeue().second == 'C');
  173. assert(queue4->dequeue().second == 'C');
  174. assert(queue4->dequeue().second == 'A');
  175. assert(queue4->dequeue().second == 'B');
  176. delete queue1;
  177. delete queue2;
  178. delete queue3;
  179. delete queue4;
  180. //===============================================
  181. SBST4.insert(3, 'C');
  182. SBST4.insert(1, 'A');
  183. SBST4.insert(2, 'B');
  184. queue1 = SBST4.getPostOrder();
  185. queue2 = SBST4.getPreOrder();
  186. queue3 = SBST4.getInOrder();
  187. queue4 = SBST4.getLevelOrder();
  188. assert(queue1->dequeue().second == 'A');
  189. assert(queue1->dequeue().second == 'C');
  190. assert(queue1->dequeue().second == 'B');
  191. assert(queue2->dequeue().second == 'B');
  192. assert(queue2->dequeue().second == 'A');
  193. assert(queue2->dequeue().second == 'C');
  194. assert(queue3->dequeue().second == 'A');
  195. assert(queue3->dequeue().second == 'B');
  196. assert(queue3->dequeue().second == 'C');
  197. assert(queue4->dequeue().second == 'B');
  198. assert(queue4->dequeue().second == 'A');
  199. assert(queue4->dequeue().second == 'C');
  200. delete queue1;
  201. delete queue2;
  202. delete queue3;
  203. delete queue4;
  204. //===============================================
  205. SBST5.insert(1, 'A');
  206. SBST5.insert(3, 'C');
  207. SBST5.insert(2, 'B');
  208. queue1 = SBST5.getPostOrder();
  209. queue2 = SBST5.getPreOrder();
  210. queue3 = SBST5.getInOrder();
  211. queue4 = SBST5.getLevelOrder();
  212. assert(queue1->dequeue().second == 'A');
  213. assert(queue1->dequeue().second == 'C');
  214. assert(queue1->dequeue().second == 'B');
  215. assert(queue2->dequeue().second == 'B');
  216. assert(queue2->dequeue().second == 'A');
  217. assert(queue2->dequeue().second == 'C');
  218. assert(queue3->dequeue().second == 'A');
  219. assert(queue3->dequeue().second == 'B');
  220. assert(queue3->dequeue().second == 'C');
  221. assert(queue4->dequeue().second == 'B');
  222. assert(queue4->dequeue().second == 'A');
  223. assert(queue4->dequeue().second == 'C');
  224. delete queue1;
  225. delete queue2;
  226. delete queue3;
  227. delete queue4;
  228. }
  229. /* findTest - accomplishes the following
  230. *
  231. * *tests that each removed element
  232. * is not in the tree
  233. *
  234. * *tests that right value is returned when find is called
  235. */
  236. void findTest() {
  237. SPLAVL<int,int> BST;
  238. SPLAVL<int, char> BST2;
  239. BST.setMaxCount(1000);
  240. BST2.setMaxCount(1000);
  241. assert(BST.getSize() == 0); // Checks that initial size is correct.
  242. for (int i = 0; i < 100; ++i) {
  243. BST.insert(2*i + 1, i);
  244. assert(BST.getSize() == i+1);
  245. }
  246. for (int i = 0; i < 100; ++i) { // Checks that keys are in the tree.
  247. assert(BST.find(2*i + 1) == i);
  248. assert(BST.getRootKey() == 2*i+1);
  249. }
  250. for (int i = 0; i < 100; ++i) { // Checks that key returns proper update.
  251. BST.update(2*i + 1, 2*i);
  252. assert(BST.find(2*i + 1) == 2*i);
  253. assert(BST.getRootKey() == 2*i+1);
  254. }
  255. try{ // Testing edge cases
  256. BST.find(0);
  257. assert(BST.getRootKey() == 199);
  258. assert(false);
  259. } catch(runtime_error& exc){}
  260. try{
  261. BST.find(2*99 + 2);
  262. assert(false);
  263. } catch(runtime_error& exc){}
  264. BST2.insert(4, 'D');
  265. assert(BST2.getRootKey() == 4);
  266. BST2.insert(3, 'C');
  267. assert(BST2.getRootKey() == 3);
  268. BST2.insert(5, 'E');
  269. assert(BST2.getRootKey() == 5);
  270. BST2.insert(1, 'A');
  271. assert(BST2.getRootKey() == 1);
  272. BST2.insert(2, 'B');
  273. assert(BST2.getRootKey() == 2);
  274. assert(BST2.find(4) == 'D');
  275. assert(BST2.getRootKey() == 4);
  276. BST2.remove(4);
  277. try{
  278. BST2.find(4);
  279. assert(false);
  280. } catch(runtime_error& exc){}
  281. assert(BST2.find(2) == 'B');
  282. assert(BST2.getRootKey() == 2);
  283. BST2.remove(2);
  284. try{
  285. BST2.find(2);
  286. assert(false);
  287. } catch(runtime_error& exc){}
  288. assert(BST2.find(1) == 'A');
  289. assert(BST2.getRootKey() == 1);
  290. BST2.remove(1);
  291. try{
  292. BST2.find(1);
  293. assert(false);
  294. } catch(runtime_error& exc){}
  295. assert(BST2.find(3) == 'C');
  296. assert(BST2.getRootKey() == 3);
  297. BST2.remove(3);
  298. try{
  299. BST2.find(3);
  300. assert(false);
  301. } catch(runtime_error& exc){}
  302. assert(BST2.find(5) == 'E');
  303. assert(BST2.getRootKey() == 5);
  304. BST2.remove(5);
  305. try{
  306. BST2.find(5);
  307. assert(false);
  308. } catch(runtime_error& exc){}
  309. }
  310. /* updateTest - accomplishes the following
  311. * *tests that update returns error if key is
  312. * not in tree
  313. *
  314. * *tests that each updated element is in the right spot
  315. * by checking on all five elementary subtrees (i.e. with 3 nodes)
  316. */
  317. void updateTest(){
  318. SPLAVL<int, char> SBST1, SBST2, SBST3, SBST4, SBST5;
  319. Queue< Pair<int,char> >* queue;
  320. SPLAVL<int, int> BST;
  321. SBST1.setMaxCount(1000);
  322. SBST2.setMaxCount(1000);
  323. SBST3.setMaxCount(1000);
  324. SBST4.setMaxCount(1000);
  325. SBST5.setMaxCount(1000);
  326. BST.setMaxCount(1000);
  327. assert(BST.getSize() == 0); // Checks that initial size is correct.
  328. try{ // errors returned when key does not exist in subtree
  329. BST.update(5, 10);
  330. assert(false);
  331. } catch(runtime_error& exc){}
  332. for (int i = 0; i < 100; ++i) { //inserts and updates 100 elements
  333. BST.insert(2*i + 1, i);
  334. assert(BST.contains(2*i + 1));
  335. BST.update(2*i + 1, 2*i);
  336. assert(BST.getSize() == i+1);
  337. }
  338. try{
  339. BST.update(2*99 + 2, 100);
  340. assert(false);
  341. } catch(runtime_error& exc){}
  342. for (int i = 0; i < 100; ++i) { // Checks that keys haven't been changed
  343. assert(BST.contains(2*i + 1));
  344. }
  345. SBST1.insert(1, 'A');
  346. SBST1.insert(2, 'B');
  347. SBST1.insert(3, 'C');
  348. SBST1.update(2, 'D');
  349. queue = SBST1.getPostOrder();
  350. assert(queue->dequeue().second == 'A');
  351. assert(queue->dequeue().second == 'C');
  352. assert(queue->dequeue().second == 'D');
  353. delete queue;
  354. //===============================================
  355. SBST2.insert(3, 'C');
  356. SBST2.insert(2, 'B');
  357. SBST2.insert(1, 'A');
  358. SBST2.update(1, 'E');
  359. SBST2.update(2, 'B');
  360. queue = SBST2.getPostOrder();
  361. assert(queue->dequeue().second == 'E');
  362. assert(queue->dequeue().second == 'C');
  363. assert(queue->dequeue().second == 'B');
  364. delete queue;
  365. //===============================================
  366. SBST3.insert(2, 'B');
  367. SBST3.insert(1, 'A');
  368. SBST3.insert(3, 'C');
  369. SBST3.update(3, 'F');
  370. queue = SBST3.getPostOrder();
  371. assert(queue->dequeue().second == 'B');
  372. assert(queue->dequeue().second == 'A');
  373. assert(queue->dequeue().second == 'F');
  374. delete queue;
  375. //===============================================
  376. SBST4.insert(3, 'C');
  377. SBST4.insert(1, 'A');
  378. SBST4.insert(2, 'B');
  379. SBST4.update(3, 'D');
  380. SBST4.update(1, 'E');
  381. SBST4.update(2, 'F');
  382. queue = SBST4.getPostOrder();
  383. assert(queue->dequeue().second == 'E');
  384. assert(queue->dequeue().second == 'D');
  385. assert(queue->dequeue().second == 'F');
  386. delete queue;
  387. //===============================================
  388. SBST5.insert(1, 'A');
  389. SBST5.insert(3, 'C');
  390. SBST5.insert(2, 'B');
  391. SBST5.update(2, 'G');
  392. SBST5.update(2, 'E');
  393. SBST5.update(3, 'F');
  394. SBST5.update(2, 'M');
  395. queue = SBST5.getPostOrder();
  396. assert(queue->dequeue().second == 'A');
  397. assert(queue->dequeue().second == 'F');
  398. assert(queue->dequeue().second == 'M');
  399. delete queue;
  400. }
  401. /* removeTest - accomplishes the following
  402. *
  403. * *ensures we can't delete in empty tree
  404. *
  405. * *ensures each remove decreases the size by 1
  406. *
  407. * *for tests that check each removed element
  408. * is not in the tree, look at findTest
  409. *
  410. * *tests that each removed element results in tree
  411. * with elements in the right spot
  412. * by checking all four traversal algorithms on all remove
  413. * situations
  414. */
  415. void SPLAYRemoveTest() {
  416. SPLAVL<int, char> BST;
  417. BST.setMaxCount(1000);
  418. Queue< Pair<int,char> >* queue1;
  419. Queue< Pair<int,char> >* queue2;
  420. Queue< Pair<int,char> >* queue3;
  421. Queue< Pair<int,char> >* queue4;
  422. try{ // testing removing on an empty tree
  423. BST.remove(1);
  424. assert(false);
  425. } catch(runtime_error& exc){}
  426. BST.insert(2, 'B');
  427. BST.insert(1, 'A');
  428. BST.insert(5, 'E');
  429. BST.insert(3, 'C');
  430. BST.insert(4, 'D');
  431. assert(BST.getSize() == 5);
  432. try { // Testing remove on non-existant keys
  433. BST.remove(6);
  434. assert(false);
  435. } catch(runtime_error& exc){}
  436. try {
  437. BST.remove(0);
  438. assert(false);
  439. } catch(runtime_error& exc){}
  440. //===============================================
  441. /* The following is a comprehensive test of the SPLAVL::remove
  442. * function. We have designed the tree to test all possible
  443. * removal algorithms (right/left leaves, parent nodes with one
  444. * right/left child node, and parent node with two subchildren).
  445. * Tested with all four search algorithms.
  446. * The original tree looks like:
  447. */
  448. queue1 = BST.getPostOrder();
  449. queue2 = BST.getPreOrder();
  450. queue3 = BST.getInOrder();
  451. queue4 = BST.getLevelOrder();
  452. assert(queue1->dequeue().second == 'B');
  453. assert(queue1->dequeue().second == 'A');
  454. assert(queue1->dequeue().second == 'C');
  455. assert(queue1->dequeue().second == 'E');
  456. assert(queue1->dequeue().second == 'D');
  457. assert(queue2->dequeue().second == 'D');
  458. assert(queue2->dequeue().second == 'C');
  459. assert(queue2->dequeue().second == 'A');
  460. assert(queue2->dequeue().second == 'B');
  461. assert(queue2->dequeue().second == 'E');
  462. assert(queue3->dequeue().second == 'A');
  463. assert(queue3->dequeue().second == 'B');
  464. assert(queue3->dequeue().second == 'C');
  465. assert(queue3->dequeue().second == 'D');
  466. assert(queue3->dequeue().second == 'E');
  467. assert(queue4->dequeue().second == 'D');
  468. assert(queue4->dequeue().second == 'C');
  469. assert(queue4->dequeue().second == 'E');
  470. assert(queue4->dequeue().second == 'A');
  471. assert(queue4->dequeue().second == 'B');
  472. delete queue1;
  473. delete queue2;
  474. delete queue3;
  475. delete queue4;
  476. //===============================================
  477. BST.remove(2);
  478. assert(BST.getSize() == 4);
  479. queue1 = BST.getPostOrder();
  480. queue2 = BST.getPreOrder();
  481. queue3 = BST.getInOrder();
  482. queue4 = BST.getLevelOrder();
  483. assert(queue1->dequeue().second == 'A');
  484. assert(queue1->dequeue().second == 'C');
  485. assert(queue1->dequeue().second == 'E');
  486. assert(queue1->dequeue().second == 'D');
  487. assert(queue2->dequeue().second == 'D');
  488. assert(queue2->dequeue().second == 'C');
  489. assert(queue2->dequeue().second == 'A');
  490. assert(queue2->dequeue().second == 'E');
  491. assert(queue3->dequeue().second == 'A');
  492. assert(queue3->dequeue().second == 'C');
  493. assert(queue3->dequeue().second == 'D');
  494. assert(queue3->dequeue().second == 'E');
  495. assert(queue4->dequeue().second == 'D');
  496. assert(queue4->dequeue().second == 'C');
  497. assert(queue4->dequeue().second == 'E');
  498. assert(queue4->dequeue().second == 'A');
  499. delete queue1;
  500. delete queue2;
  501. delete queue3;
  502. delete queue4;
  503. //===============================================
  504. BST.remove(5);
  505. assert(BST.getSize() == 3);
  506. queue1 = BST.getPostOrder();
  507. queue2 = BST.getPreOrder();
  508. queue3 = BST.getInOrder();
  509. queue4 = BST.getLevelOrder();
  510. assert(queue1->dequeue().second == 'A');
  511. assert(queue1->dequeue().second == 'C');
  512. assert(queue1->dequeue().second == 'D');
  513. assert(queue2->dequeue().second == 'D');
  514. assert(queue2->dequeue().second == 'C');
  515. assert(queue2->dequeue().second == 'A');
  516. assert(queue3->dequeue().second == 'A');
  517. assert(queue3->dequeue().second == 'C');
  518. assert(queue3->dequeue().second == 'D');
  519. assert(queue4->dequeue().second == 'D');
  520. assert(queue4->dequeue().second == 'C');
  521. assert(queue4->dequeue().second == 'A');
  522. delete queue1;
  523. delete queue2;
  524. delete queue3;
  525. delete queue4;
  526. //===============================================
  527. BST.remove(1);
  528. assert(BST.getSize() == 2);
  529. queue1 = BST.getPostOrder();
  530. queue2 = BST.getPreOrder();
  531. queue3 = BST.getInOrder();
  532. queue4 = BST.getLevelOrder();
  533. assert(queue1->dequeue().second == 'C');
  534. assert(queue1->dequeue().second == 'D');
  535. assert(queue2->dequeue().second == 'D');
  536. assert(queue2->dequeue().second == 'C');
  537. assert(queue3->dequeue().second == 'C');
  538. assert(queue3->dequeue().second == 'D');
  539. assert(queue4->dequeue().second == 'D');
  540. assert(queue4->dequeue().second == 'C');
  541. delete queue1;
  542. delete queue2;
  543. delete queue3;
  544. delete queue4;
  545. //===============================================
  546. BST.remove(4);
  547. assert(BST.getSize() == 1);
  548. queue1 = BST.getPostOrder();
  549. queue2 = BST.getPreOrder();
  550. queue3 = BST.getInOrder();
  551. queue4 = BST.getLevelOrder();
  552. assert(queue1->dequeue().second == 'C');
  553. assert(queue2->dequeue().second == 'C');
  554. assert(queue3->dequeue().second == 'C');
  555. assert(queue4->dequeue().second == 'C');
  556. delete queue1;
  557. delete queue2;
  558. delete queue3;
  559. delete queue4;
  560. //===============================================
  561. BST.remove(3);
  562. assert(BST.getSize() == 0);
  563. queue1 = BST.getPostOrder();
  564. queue2 = BST.getPreOrder();
  565. queue3 = BST.getInOrder();
  566. queue4 = BST.getLevelOrder();
  567. assert(queue1->getSize() == 0);
  568. assert(queue2->getSize() == 0);
  569. assert(queue3->getSize() == 0);
  570. assert(queue4->getSize() == 0);
  571. delete queue1;
  572. delete queue2;
  573. delete queue3;
  574. delete queue4;
  575. }
  576. void AVLRemoveTest(){
  577. SPLAVL<int,int> AVL;
  578. AVL.setMaxRatio(-1);
  579. //Our insertTest method makes sure that the balancing calls
  580. //do what they should. The only case to take care of is
  581. //when we delete a leaf node (and we call balance on NULL.
  582. //This adds a bunch of elements and then
  583. //deletes a bunch of elements, and makes sure things still stay
  584. //balanced.
  585. assert(AVL.getSize() == 0); // Checks that initial size is correct. assert
  586. // causes the program to immediately crash if
  587. // the condition is false.
  588. for (int i = 0; i < 100; ++i) {
  589. AVL.insert(2*i + 1, i);
  590. assert(AVL.isBalanced());
  591. assert(AVL.getSize() == i+1);
  592. }
  593. for (int i = 0; i < 100; ++i) { // Checks that keys are in the tree.
  594. assert(AVL.contains(2*i + 1));
  595. }
  596. for (int i = 0; i < 100; ++i) {
  597. AVL.insert(-2*i - 1, i);
  598. assert(AVL.isBalanced());
  599. assert(AVL.getSize() == i+1 + 100);
  600. }
  601. for (int i = 0; i < 100; ++i) { // Checks that keys are in the tree.
  602. assert(AVL.contains(-2*i - 1));
  603. }
  604. for (int i = 0; i < 100; ++i) {
  605. AVL.remove(-2*i - 1);
  606. assert(AVL.isBalanced());
  607. assert(AVL.getSize() == 199 - i);
  608. }
  609. for (int i = 0; i < 100; ++i) {
  610. AVL.remove(2*(99-i) + 1);
  611. assert(AVL.isBalanced());
  612. assert(AVL.getSize() == 99 - i);
  613. }
  614. assert(AVL.getSize() == 0);
  615. }
  616. /* AVLInsertTest - accomplishes the following
  617. *
  618. * *tests tree is balanced after series of insertions and removals
  619. *
  620. * *tests that each kind of imbalance is properly adjusted for
  621. */
  622. void AVLInsertTest(){
  623. SPLAVL<int,int> AVL;
  624. SPLAVL<int, char> SAVL1, SAVL2, SAVL3, SAVL4, SAVL5;
  625. AVL.setMaxRatio(-1);
  626. SAVL1.setMaxRatio(0);
  627. SAVL2.setMaxRatio(0);
  628. SAVL3.setMaxRatio(0);
  629. SAVL4.setMaxRatio(0);
  630. SAVL5.setMaxRatio(0);
  631. Queue< Pair<int,char> >* queue1;
  632. Queue< Pair<int,char> >* queue2;
  633. Queue< Pair<int,char> >* queue3;
  634. Queue< Pair<int,char> >* queue4;
  635. assert(AVL.getSize() == 0); // Checks that initial size is correct. assert
  636. // causes the program to immediately crash if
  637. // the condition is false.
  638. for (int i = 0; i < 100; ++i) {
  639. AVL.insert(2*i + 1, i);
  640. assert(AVL.isBalanced());
  641. assert(AVL.getSize() == i+1);
  642. }
  643. for (int i = 0; i < 100; ++i) { // Checks that keys are in the tree.
  644. assert(AVL.contains(2*i + 1));
  645. }
  646. for (int i = 0; i < 100; ++i) {
  647. AVL.insert(-2*i - 1, i);
  648. assert(AVL.isBalanced());
  649. assert(AVL.getSize() == i+1 + 100);
  650. }
  651. for (int i = 0; i < 100; ++i) { // Checks that keys are in the tree.
  652. assert(AVL.contains(-2*i - 1));
  653. }
  654. for (int i = 0; i < 100; ++i) { //Error returned if key already exists.
  655. try{
  656. AVL.insert(2*i + 1, i);
  657. assert(false);
  658. } catch(runtime_error& exc){}
  659. }
  660. assert(AVL.isBalanced());
  661. //===============================================
  662. // The following tests use inserts that create LL, LR, RL, RR imbalances.
  663. // They ensure that the tree is balanced in the right way.
  664. assert(SAVL1.getSize() == 0);
  665. SAVL1.insert(1, 'A');
  666. SAVL1.insert(2, 'B');
  667. SAVL1.insert(3, 'C');
  668. queue1 = SAVL1.getPostOrder();
  669. queue2 = SAVL1.getPreOrder();
  670. queue3 = SAVL1.getInOrder();
  671. queue4 = SAVL1.getLevelOrder();
  672. assert(queue1->dequeue().second == 'A');
  673. assert(queue1->dequeue().second == 'C');
  674. assert(queue1->dequeue().second == 'B');
  675. assert(queue2->dequeue().second == 'B');
  676. assert(queue2->dequeue().second == 'A');
  677. assert(queue2->dequeue().second == 'C');
  678. assert(queue3->dequeue().second == 'A');
  679. assert(queue3->dequeue().second == 'B');
  680. assert(queue3->dequeue().second == 'C');
  681. assert(queue4->dequeue().second == 'B');
  682. assert(queue4->dequeue().second == 'A');
  683. assert(queue4->dequeue().second == 'C');
  684. delete queue1;
  685. delete queue2;
  686. delete queue3;
  687. delete queue4;
  688. //===============================================
  689. assert(SAVL2.getSize() == 0);
  690. SAVL2.insert(3, 'C');
  691. SAVL2.insert(2, 'B');
  692. SAVL2.insert(1, 'A');
  693. queue1 = SAVL2.getPostOrder();
  694. queue2 = SAVL2.getPreOrder();
  695. queue3 = SAVL2.getInOrder();
  696. queue4 = SAVL2.getLevelOrder();
  697. assert(queue1->dequeue().second == 'A');
  698. assert(queue1->dequeue().second == 'C');
  699. assert(queue1->dequeue().second == 'B');
  700. assert(queue2->dequeue().second == 'B');
  701. assert(queue2->dequeue().second == 'A');
  702. assert(queue2->dequeue().second == 'C');
  703. assert(queue3->dequeue().second == 'A');
  704. assert(queue3->dequeue().second == 'B');
  705. assert(queue3->dequeue().second == 'C');
  706. assert(queue4->dequeue().second == 'B');
  707. assert(queue4->dequeue().second == 'A');
  708. assert(queue4->dequeue().second == 'C');
  709. delete queue1;
  710. delete queue2;
  711. delete queue3;
  712. delete queue4;
  713. //===============================================
  714. assert(SAVL3.getSize() == 0);
  715. SAVL3.insert(2, 'B');
  716. SAVL3.insert(1, 'A');
  717. SAVL3.insert(3, 'C');
  718. queue1 = SAVL3.getPostOrder();
  719. queue2 = SAVL3.getPreOrder();
  720. queue3 = SAVL3.getInOrder();
  721. queue4 = SAVL3.getLevelOrder();
  722. assert(queue1->dequeue().second == 'A');
  723. assert(queue1->dequeue().second == 'C');
  724. assert(queue1->dequeue().second == 'B');
  725. assert(queue2->dequeue().second == 'B');
  726. assert(queue2->dequeue().second == 'A');
  727. assert(queue2->dequeue().second == 'C');
  728. assert(queue3->dequeue().second == 'A');
  729. assert(queue3->dequeue().second == 'B');
  730. assert(queue3->dequeue().second == 'C');
  731. assert(queue4->dequeue().second == 'B');
  732. assert(queue4->dequeue().second == 'A');
  733. assert(queue4->dequeue().second == 'C');
  734. delete queue1;
  735. delete queue2;
  736. delete queue3;
  737. delete queue4;
  738. //===============================================
  739. assert(SAVL4.getSize() == 0);
  740. SAVL4.insert(3, 'C');
  741. SAVL4.insert(1, 'A');
  742. SAVL4.insert(2, 'B');
  743. queue1 = SAVL4.getPostOrder();
  744. queue2 = SAVL4.getPreOrder();
  745. queue3 = SAVL4.getInOrder();
  746. queue4 = SAVL4.getLevelOrder();
  747. assert(queue1->dequeue().second == 'A');
  748. assert(queue1->dequeue().second == 'C');
  749. assert(queue1->dequeue().second == 'B');
  750. assert(queue2->dequeue().second == 'B');
  751. assert(queue2->dequeue().second == 'A');
  752. assert(queue2->dequeue().second == 'C');
  753. assert(queue3->dequeue().second == 'A');
  754. assert(queue3->dequeue().second == 'B');
  755. assert(queue3->dequeue().second == 'C');
  756. assert(queue4->dequeue().second == 'B');
  757. assert(queue4->dequeue().second == 'A');
  758. assert(queue4->dequeue().second == 'C');
  759. delete queue1;
  760. delete queue2;
  761. delete queue3;
  762. delete queue4;
  763. //===============================================
  764. assert(SAVL5.getSize() == 0);
  765. SAVL5.insert(1, 'A');
  766. SAVL5.insert(3, 'C');
  767. SAVL5.insert(2, 'B');
  768. queue1 = SAVL5.getPostOrder();
  769. queue2 = SAVL5.getPreOrder();
  770. queue3 = SAVL5.getInOrder();
  771. queue4 = SAVL5.getLevelOrder();
  772. assert(queue1->dequeue().second == 'A');
  773. assert(queue1->dequeue().second == 'C');
  774. assert(queue1->dequeue().second == 'B');
  775. assert(queue2->dequeue().second == 'B');
  776. assert(queue2->dequeue().second == 'A');
  777. assert(queue2->dequeue().second == 'C');
  778. assert(queue3->dequeue().second == 'A');
  779. assert(queue3->dequeue().second == 'B');
  780. assert(queue3->dequeue().second == 'C');
  781. assert(queue4->dequeue().second == 'B');
  782. assert(queue4->dequeue().second == 'A');
  783. assert(queue4->dequeue().second == 'C');
  784. delete queue1;
  785. delete queue2;
  786. delete queue3;
  787. delete queue4;
  788. }
  789. void SPLAVLinsertTest(){
  790. SPLAVL<int, int> SPLAVL;
  791. Queue< Pair<int,int> >* queue1;
  792. Queue< Pair<int,int> >* queue2;
  793. Queue< Pair<int,int> >* queue3;
  794. Queue< Pair<int,int> >* queue4;
  795. SPLAVL.setMaxRatio(0);
  796. SPLAVL.setMaxCount(5);
  797. for (int i = 1; i<=4; i++){
  798. SPLAVL.insert(i, i);
  799. }
  800. //===============================================
  801. queue1 = SPLAVL.getPostOrder();
  802. queue2 = SPLAVL.getPreOrder();
  803. queue3 = SPLAVL.getInOrder();
  804. queue4 = SPLAVL.getLevelOrder();
  805. /*
  806. cout << "HEY" << queue1->dequeue().second << " ";
  807. cout << queue1->dequeue().second << " ";
  808. cout << queue1->dequeue().second << " ";
  809. cout << queue1->dequeue().second << " ";
  810. cout << endl;
  811. cout << queue4->dequeue().second << " ";
  812. cout << queue4->dequeue().second << " ";
  813. cout << queue4->dequeue().second << " ";
  814. cout << queue4->dequeue().second << " ";
  815. cout << endl;
  816. */
  817. assert(queue1->dequeue().second == 1);
  818. assert(queue1->dequeue().second == 2);
  819. assert(queue1->dequeue().second == 3);
  820. assert(queue1->dequeue().second == 4);
  821. assert(queue2->dequeue().second == 4);
  822. assert(queue2->dequeue().second == 3);
  823. assert(queue2->dequeue().second == 2);
  824. assert(queue2->dequeue().second == 1);
  825. assert(queue3->dequeue().second == 1);
  826. assert(queue3->dequeue().second == 2);
  827. assert(queue3->dequeue().second == 3);
  828. assert(queue3->dequeue().second == 4);
  829. assert(queue4->dequeue().second == 4);
  830. assert(queue4->dequeue().second == 3);
  831. assert(queue4->dequeue().second == 2);
  832. assert(queue4->dequeue().second == 1);
  833. delete queue1;
  834. delete queue2;
  835. delete queue3;
  836. delete queue4;
  837. //===============================================
  838. SPLAVL.insert(5,5);
  839. queue1 = SPLAVL.getPostOrder();
  840. queue2 = SPLAVL.getPreOrder();
  841. queue3 = SPLAVL.getInOrder();
  842. queue4 = SPLAVL.getLevelOrder();
  843. assert(queue1->dequeue().second == 1);
  844. assert(queue1->dequeue().second == 2);
  845. assert(queue1->dequeue().second == 5);
  846. assert(queue1->dequeue().second == 4);
  847. assert(queue1->dequeue().second == 3);
  848. assert(queue2->dequeue().second == 3);
  849. assert(queue2->dequeue().second == 2);
  850. assert(queue2->dequeue().second == 1);
  851. assert(queue2->dequeue().second == 4);
  852. assert(queue2->dequeue().second == 5);
  853. assert(queue3->dequeue().second == 1);
  854. assert(queue3->dequeue().second == 2);
  855. assert(queue3->dequeue().second == 3);
  856. assert(queue3->dequeue().second == 4);
  857. assert(queue3->dequeue().second == 5);
  858. assert(queue4->dequeue().second == 3);
  859. assert(queue4->dequeue().second == 2);
  860. assert(queue4->dequeue().second == 4);
  861. assert(queue4->dequeue().second == 1);
  862. assert(queue4->dequeue().second == 5);
  863. delete queue1;
  864. delete queue2;
  865. delete queue3;
  866. delete queue4;
  867. //===============================================
  868. SPLAVL.insert(6,6);
  869. queue1 = SPLAVL.getPostOrder();
  870. queue2 = SPLAVL.getPreOrder();
  871. queue3 = SPLAVL.getInOrder();
  872. queue4 = SPLAVL.getLevelOrder();
  873. assert(queue1->dequeue().second == 1);
  874. assert(queue1->dequeue().second == 2);
  875. assert(queue1->dequeue().second == 5);
  876. assert(queue1->dequeue().second == 4);
  877. assert(queue1->dequeue().second == 3);
  878. assert(queue1->dequeue().second == 6);
  879. assert(queue2->dequeue().second == 6);
  880. assert(queue2->dequeue().second == 3);
  881. assert(queue2->dequeue().second == 2);
  882. assert(queue2->dequeue().second == 1);
  883. assert(queue2->dequeue().second == 4);
  884. assert(queue2->dequeue().second == 5);
  885. assert(queue3->dequeue().second == 1);
  886. assert(queue3->dequeue().second == 2);
  887. assert(queue3->dequeue().second == 3);
  888. assert(queue3->dequeue().second == 4);
  889. assert(queue3->dequeue().second == 5);
  890. assert(queue3->dequeue().second == 6);
  891. assert(queue4->dequeue().second == 6);
  892. assert(queue4->dequeue().second == 3);
  893. assert(queue4->dequeue().second == 2);
  894. assert(queue4->dequeue().second == 4);
  895. assert(queue4->dequeue().second == 1);
  896. assert(queue4->dequeue().second == 5);
  897. delete queue1;
  898. delete queue2;
  899. delete queue3;
  900. delete queue4;
  901. }