Graphviz 12.0.1~dev.20240715.2254
Loading...
Searching...
No Matches
test_red_black_tree.c
Go to the documentation of this file.
1/**********************************************************
2* See the LICENSE file for copyright information. *
3**********************************************************/
4
5#include "config.h"
6
8#include<stdio.h>
9#include<ctype.h>
10
11
12/* this file has functions to test a red-black tree of integers */
13
14void IntDest(void* a) {
15 free(a);
16}
17
18
19
20int 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
26int main() {
27 int option=0;
28 int newKey,newKey2;
29 int* newInt;
32
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*/
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) ) ) {
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 {
108 return 0;
109 }
110 break;
111 default:
112 printf("Invalid input; Please try again.\n");
113 }
114 }
115 return 0;
116}
void * malloc(YYSIZE_T)
void free(void *)
static node_list * newNode(Grid *g, Agnode_t *n, node_list *nxt)
Definition grid.c:138
@ tree
Definition gvgen.c:33
void RBDelete(rb_red_blk_tree *tree, rb_red_blk_node *z)
rb_red_blk_node * TreePredecessor(rb_red_blk_tree *tree, rb_red_blk_node *x)
void RBTreeDestroy(rb_red_blk_tree *tree)
rb_red_blk_node * RBExactQuery(rb_red_blk_tree *tree, void *q)
rb_red_blk_node * RBTreeInsert(rb_red_blk_tree *tree, void *key)
rb_red_blk_node * TreeSuccessor(rb_red_blk_tree *tree, rb_red_blk_node *x)
rb_red_blk_tree * RBTreeCreate(int(*CompFunc)(const void *, const void *), void(*DestFunc)(void *))
void IntDest(void *a)
int IntComp(const void *a, const void *b)
int main()