1 /* testavl.c - Test Tim Howes AVL code */
4 * Copyright 1998-2003 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 ));
21 static int avl_strcmp LDAP_P(( const void *s, const void *t ));
24 main( int argc, char **argv )
32 while ( fgets( command, sizeof( command ), stdin ) != NULL ) {
34 case 'n': /* new tree */
35 ( void ) avl_free( tree, free );
39 ( void ) myprint( tree );
41 case 't': /* traverse with first, next */
42 #ifdef AVL_NONREENTRANT
44 for ( p = (char * ) avl_getfirst( tree );
46 p = (char *) avl_getnext())
50 printf( "*** reentrant interface not implemented ***" );
55 if ( fgets( name, sizeof( name ), stdin ) == NULL )
57 name[ strlen( name ) - 1 ] = '\0';
58 if ( (p = (char *) avl_find( tree, name, avl_strcmp ))
60 printf( "Not found.\n\n" );
62 printf( "%s\n\n", p );
64 case 'i': /* insert */
66 if ( fgets( name, sizeof( name ), stdin ) == NULL )
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" );
73 case 'd': /* delete */
75 if ( fgets( name, sizeof( name ), stdin ) == NULL )
77 name[ strlen( name ) - 1 ] = '\0';
78 if ( avl_delete( &tree, name, avl_strcmp ) == NULL )
79 printf( "\nNot found!\n" );
87 printf("Commands: insert, delete, print, new, quit\n");
96 static void ravl_print( Avlnode *root, int depth )
103 ravl_print( root->avl_right, depth+1 );
105 for ( i = 0; i < depth; i++ )
107 printf( "%s %d\n", (char *) root->avl_data, root->avl_bf );
109 ravl_print( root->avl_left, depth+1 );
112 static void myprint( Avlnode *root )
114 printf( "********\n" );
117 printf( "\tNULL\n" );
119 ravl_print( root, 0 );
121 printf( "********\n" );
124 static int avl_strcmp( const void *s, const void *t )
126 return strcmp( s, t );