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