1 /* testavl.c - Test Tim Howes AVL code */
4 * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
12 #include <ac/stdlib.h>
13 #include <ac/string.h>
16 #define AVL_NONREENTRANT
19 static void ravl_print LDAP_P(( Avlnode *root, int depth ));
20 static void myprint LDAP_P(( Avlnode *root ));
23 main( int argc, char **argv )
31 while ( fgets( command, sizeof( command ), stdin ) != NULL ) {
33 case 'n': /* new tree */
34 ( void ) avl_free( tree, (AVL_FREE) free );
38 ( void ) myprint( tree );
40 case 't': /* traverse with first, next */
41 #ifdef AVL_NONREENTRANT
43 for ( p = (char * ) avl_getfirst( tree );
45 p = (char *) avl_getnext())
49 printf( "*** reentrant interface not implemented ***" );
54 if ( fgets( name, sizeof( name ), stdin ) == NULL )
56 name[ strlen( name ) - 1 ] = '\0';
57 if ( (p = (char *) avl_find( tree, name, (AVL_CMP) strcmp ))
59 printf( "Not found.\n\n" );
61 printf( "%s\n\n", p );
63 case 'i': /* insert */
65 if ( fgets( name, sizeof( name ), stdin ) == NULL )
67 name[ strlen( name ) - 1 ] = '\0';
68 if ( avl_insert( &tree, strdup( name ), (AVL_CMP) strcmp,
69 avl_dup_error ) != 0 )
70 printf( "\nNot inserted!\n" );
72 case 'd': /* delete */
74 if ( fgets( name, sizeof( name ), stdin ) == NULL )
76 name[ strlen( name ) - 1 ] = '\0';
77 if ( avl_delete( &tree, name, (AVL_CMP) strcmp ) == NULL )
78 printf( "\nNot found!\n" );
86 printf("Commands: insert, delete, print, new, quit\n");
95 static void ravl_print( Avlnode *root, int depth )
102 ravl_print( root->avl_right, depth+1 );
104 for ( i = 0; i < depth; i++ )
106 printf( "%s %d\n", (char *) root->avl_data, root->avl_bf );
108 ravl_print( root->avl_left, depth+1 );
111 static void myprint( Avlnode *root )
113 printf( "********\n" );
116 printf( "\tNULL\n" );
118 ravl_print( root, 0 );
120 printf( "********\n" );