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