1 /* testavl.c - Test Tim Howes AVL code */
12 #define AVL_NONREENTRANT
15 static void ravl_print LDAP_P(( Avlnode *root, int depth ));
16 static void myprint LDAP_P(( Avlnode *root ));
19 main( int argc, char **argv )
27 while ( fgets( command, sizeof( command ), stdin ) != NULL ) {
29 case 'n': /* new tree */
30 ( void ) avl_free( tree, (AVL_FREE) free );
34 ( void ) myprint( tree );
36 case 't': /* traverse with first, next */
37 #ifdef AVL_NONREENTRANT
39 for ( p = (char * ) avl_getfirst( tree );
41 p = (char *) avl_getnext())
45 printf( "*** reentrant interface not implemented ***" );
50 if ( fgets( name, sizeof( name ), stdin ) == NULL )
52 name[ strlen( name ) - 1 ] = '\0';
53 if ( (p = (char *) avl_find( tree, name, (AVL_CMP) strcmp ))
55 printf( "Not found.\n\n" );
57 printf( "%s\n\n", p );
59 case 'i': /* insert */
61 if ( fgets( name, sizeof( name ), stdin ) == NULL )
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" );
68 case 'd': /* delete */
70 if ( fgets( name, sizeof( name ), stdin ) == NULL )
72 name[ strlen( name ) - 1 ] = '\0';
73 if ( avl_delete( &tree, name, (AVL_CMP) strcmp ) == NULL )
74 printf( "\nNot found!\n" );
82 printf("Commands: insert, delete, print, new, quit\n");
91 static void ravl_print( Avlnode *root, int depth )
98 ravl_print( root->avl_right, depth+1 );
100 for ( i = 0; i < depth; i++ )
102 printf( "%s %d\n", (char *) root->avl_data, root->avl_bf );
104 ravl_print( root->avl_left, depth+1 );
107 static void myprint( Avlnode *root )
109 printf( "********\n" );
112 printf( "\tNULL\n" );
114 ravl_print( root, 0 );
116 printf( "********\n" );