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