]> git.sur5r.net Git - openldap/blob - libraries/libavl/testavl.c
#ifdef out non-reentrant prototypes.
[openldap] / libraries / libavl / testavl.c
1 /* testavl.c - Test Tim Howes AVL code */
2
3 #include "portable.h"
4
5 #include <stdio.h>
6 #include <stdlib.h>
7
8 #include <ac/string.h>
9
10 #define AVL_NONREENTRANT 
11 #include "avl.h"
12
13 static void ravl_print LDAP_P(( Avlnode *root, int depth ));
14 static void myprint LDAP_P(( Avlnode *root ));
15
16 int
17 main( int argc, char **argv )
18 {
19         Avlnode *tree = NULLAVL;
20         char    command[ 10 ];
21         char    name[ 80 ];
22         char    *p;
23
24         printf( "> " );
25         while ( fgets( command, sizeof( command ), stdin ) != NULL ) {
26                 switch( *command ) {
27                 case 'n':       /* new tree */
28                         ( void ) avl_free( tree, (AVL_FREE) free );
29                         tree = NULLAVL;
30                         break;
31                 case 'p':       /* print */
32                         ( void ) myprint( tree );
33                         break;
34                 case 't':       /* traverse with first, next */
35 #ifdef AVL_NONREENTRANT
36                         printf( "***\n" );
37                         for ( p = (char * ) avl_getfirst( tree );
38                             p != NULL;
39                                 p = (char *) avl_getnext())
40                                 printf( "%s\n", p );
41                         printf( "***\n" );
42 #else
43                         printf( "*** reentrant interface not implemented ***" );
44 #endif
45                         break;
46                 case 'f':       /* find */
47                         printf( "data? " );
48                         if ( fgets( name, sizeof( name ), stdin ) == NULL )
49                                 exit( 0 );
50                         name[ strlen( name ) - 1 ] = '\0';
51                         if ( (p = (char *) avl_find( tree, name, (AVL_CMP) strcmp ))
52                             == NULL )
53                                 printf( "Not found.\n\n" );
54                         else
55                                 printf( "%s\n\n", p );
56                         break;
57                 case 'i':       /* insert */
58                         printf( "data? " );
59                         if ( fgets( name, sizeof( name ), stdin ) == NULL )
60                                 exit( 0 );
61                         name[ strlen( name ) - 1 ] = '\0';
62                         if ( avl_insert( &tree, strdup( name ), (AVL_CMP) strcmp, 
63                             avl_dup_error ) != 0 )
64                                 printf( "\nNot inserted!\n" );
65                         break;
66                 case 'd':       /* delete */
67                         printf( "data? " );
68                         if ( fgets( name, sizeof( name ), stdin ) == NULL )
69                                 exit( 0 );
70                         name[ strlen( name ) - 1 ] = '\0';
71                         if ( avl_delete( &tree, name, (AVL_CMP) strcmp ) == NULL )
72                                 printf( "\nNot found!\n" );
73                         break;
74                 case 'q':       /* quit */
75                         exit( 0 );
76                         break;
77                 case '\n':
78                         break;
79                 default:
80                         printf("Commands: insert, delete, print, new, quit\n");
81                 }
82
83                 printf( "> " );
84         }
85
86         return( 0 );
87 }
88
89 static void ravl_print( Avlnode *root, int depth )
90 {
91         int     i;
92
93         if ( root == 0 )
94                 return;
95
96         ravl_print( root->avl_right, depth+1 );
97
98         for ( i = 0; i < depth; i++ )
99                 printf( "   " );
100         printf( "%s %d\n", (char *) root->avl_data, root->avl_bf );
101
102         ravl_print( root->avl_left, depth+1 );
103 }
104
105 static void myprint( Avlnode *root )
106 {
107         printf( "********\n" );
108
109         if ( root == 0 )
110                 printf( "\tNULL\n" );
111         else
112                 ravl_print( root, 0 );
113
114         printf( "********\n" );
115 }