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