45 #define MAIN_MENU_ROW "--- AVL SEARCH TREE DEMO ---\nMENU: 0=Exit 1=Add_Node 2=Rem_Node 3=Search 4=Print\nSelection "
49 void my_destroy(
void *data);
50 void print(
const void *data);
51 int my_cmp(
const void *key1,
const void *key2);
58 void final_status(
AvlTree tree);
61 void create_nodes(
AvlTree tree,
int nr_of_nodes);
66 void my_destroy(
void *data)
72 void print(
const void *data)
74 printf(
" %02d", *(
int *)data);
78 int my_cmp(
const void *key1,
const void *key2)
80 return (*(
int *)key1 - *(
int *)key2);
84 void create_nodes(
AvlTree tree,
int nr_of_nodes)
86 int i=0, *pi, retval, dupctr=0;
90 pi = (
int *)malloc(
sizeof(
int));
109 }
while (++i < nr_of_nodes);
112 printf(
"--- INITIALIZING AN AVL TREE, %d NODES, RANDOM INTEGER DATA ---\n", NR_OF_ITEMS);
114 printf(
"\n\n%d/%d successful insertions -- %d duplicate(s) rejected...",
AVLTREEsize(tree), nr_of_nodes, dupctr);
121 int tmp, *pi, retval;
127 printf(
"--- INSERT NODE ---\n");
130 tmp =
read_int(
"\nEnter integer data for node to be inserted (-1=Quit): ", 0, 0);
135 pi = (
int *)malloc(
sizeof(
int));
144 sprintf(mess,
"Error: Duplicate - node %d already present..!", *pi);
157 sprintf(mess,
"Node %d will be inserted..", *(
int *)pi);
166 int tmp, *pi, retval;
172 printf(
"--- REMOVE NODE ---\n");
175 tmp =
read_int(
"\nEnter data for node to be removed (-1=Quit): ", 0, 0);
186 sprintf(mess,
"Error: Node %d not found..!", *(
int *)pi);
191 printf(
"\nFatal failure - bailing out...");
199 sprintf(mess,
"Node %d will be removed(=hidden)..!", *(
int *)pi);
209 int tmp, *pi, retval;
215 printf(
"--- SEARCH NODE ---\n");
218 tmp =
read_int(
"\nEnter data for node to be found (-1=Quit): ", 0, 0);
230 sprintf(mess,
"Node %d NOT found..!", *(
int *)pi);
235 printf(
"Fatal failure - bailing out...");
243 sprintf(mess,
"Node %d FOUND..!", *(
int *)pi);
258 void final_status(
AvlTree tree)
262 printf(
"--- FINAL AVL TREE STATUS---\n");
272 srand((
unsigned int)time(NULL));
274 if ((mytree =
AVLTREEinit(my_cmp, my_destroy)) == NULL)
276 printf(
"\nFatal error - bailing out...\n!");
282 create_nodes(mytree, NR_OF_ITEMS);
286 menu_choice =
menu(MAIN_MENU_ROW, 0, 4);
301 printf(
"--- PRINT TREE ---\n");
306 final_status(mytree);