test_red_black_tree.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /**********************************************************
  2. * See the LICENSE file for copyright information. *
  3. **********************************************************/
  4. #include "config.h"
  5. #include<rbtree/red_black_tree.h>
  6. #include<stdio.h>
  7. #include<ctype.h>
  8. /* this file has functions to test a red-black tree of integers */
  9. void IntDest(void* a) {
  10. free(a);
  11. }
  12. int IntComp(const void* a,const void* b) {
  13. if( *(int*)a > *(int*)b) return(1);
  14. if( *(int*)a < *(int*)b) return(-1);
  15. return(0);
  16. }
  17. int main() {
  18. int option=0;
  19. int newKey,newKey2;
  20. int* newInt;
  21. rb_red_blk_node* newNode;
  22. rb_red_blk_tree* tree;
  23. tree=RBTreeCreate(IntComp, IntDest);
  24. while (option != 6) {
  25. printf("choose one of the following:\n");
  26. printf("(1) add to tree\n(2) delete from tree\n(3) query\n");
  27. printf("(4) find predecessor\n(5) find successor\n");
  28. printf("(6) quit\n");
  29. do option=fgetc(stdin); while(-1 != option && isspace(option));
  30. option-='0';
  31. switch(option)
  32. {
  33. case 1:
  34. {
  35. printf("type key for new node\n");
  36. scanf("%i",&newKey);
  37. newInt= malloc(sizeof(int));
  38. *newInt=newKey;
  39. RBTreeInsert(tree, newInt);
  40. }
  41. break;
  42. case 2:
  43. {
  44. printf("type key of node to remove\n");
  45. scanf("%i",&newKey);
  46. if ( ( newNode=RBExactQuery(tree,&newKey ) ) ) RBDelete(tree,newNode);/*assignment*/
  47. else printf("key not found in tree, no action taken\n");
  48. }
  49. break;
  50. case 3:
  51. {
  52. printf("type key of node to query for\n");
  53. scanf("%i",&newKey);
  54. if ( ( newNode = RBExactQuery(tree,&newKey) ) ) {/*assignment*/
  55. printf("data found in tree at location %i\n",(int)newNode);
  56. } else {
  57. printf("data not in tree\n");
  58. }
  59. }
  60. break;
  61. case 4:
  62. {
  63. printf("type key of node to find predecessor of\n");
  64. scanf("%i",&newKey);
  65. if ( ( newNode = RBExactQuery(tree,&newKey) ) ) {/*assignment*/
  66. newNode=TreePredecessor(tree,newNode);
  67. if(tree->nil == newNode) {
  68. printf("there is no predecessor for that node (it is a minimum)\n");
  69. } else {
  70. printf("predecessor has key %i\n",*(int*)newNode->key);
  71. }
  72. } else {
  73. printf("data not in tree\n");
  74. }
  75. }
  76. break;
  77. case 5:
  78. {
  79. printf("type key of node to find successor of\n");
  80. scanf("%i",&newKey);
  81. if ( (newNode = RBExactQuery(tree,&newKey) ) ) {
  82. newNode=TreeSuccessor(tree,newNode);
  83. if(tree->nil == newNode) {
  84. printf("there is no successor for that node (it is a maximum)\n");
  85. } else {
  86. printf("successor has key %i\n",*(int*)newNode->key);
  87. }
  88. } else {
  89. printf("data not in tree\n");
  90. }
  91. }
  92. break;
  93. case 6:
  94. {
  95. RBTreeDestroy(tree);
  96. return 0;
  97. }
  98. break;
  99. default:
  100. printf("Invalid input; Please try again.\n");
  101. }
  102. }
  103. return 0;
  104. }