1 /* testavl.c - Test Tim Howes AVL code */
11 #define AVL_NONREENTRANT
14 static void ravl_print LDAP_P(( Avlnode *root, int depth ));
15 static void myprint LDAP_P(( Avlnode *root ));
18 main( int argc, char **argv )
26 while ( fgets( command, sizeof( command ), stdin ) != NULL ) {
28 case 'n': /* new tree */
29 ( void ) avl_free( tree, (AVL_FREE) free );
33 ( void ) myprint( tree );
35 case 't': /* traverse with first, next */
36 #ifdef AVL_NONREENTRANT
38 for ( p = (char * ) avl_getfirst( tree );
40 p = (char *) avl_getnext())
44 printf( "*** reentrant interface not implemented ***" );
49 if ( fgets( name, sizeof( name ), stdin ) == NULL )
51 name[ strlen( name ) - 1 ] = '\0';
52 if ( (p = (char *) avl_find( tree, name, (AVL_CMP) strcmp ))
54 printf( "Not found.\n\n" );
56 printf( "%s\n\n", p );
58 case 'i': /* insert */
60 if ( fgets( name, sizeof( name ), stdin ) == NULL )
62 name[ strlen( name ) - 1 ] = '\0';
63 if ( avl_insert( &tree, strdup( name ), (AVL_CMP) strcmp,
64 avl_dup_error ) != 0 )
65 printf( "\nNot inserted!\n" );
67 case 'd': /* delete */
69 if ( fgets( name, sizeof( name ), stdin ) == NULL )
71 name[ strlen( name ) - 1 ] = '\0';
72 if ( avl_delete( &tree, name, (AVL_CMP) strcmp ) == NULL )
73 printf( "\nNot found!\n" );
81 printf("Commands: insert, delete, print, new, quit\n");
90 static void ravl_print( Avlnode *root, int depth )
97 ravl_print( root->avl_right, depth+1 );
99 for ( i = 0; i < depth; i++ )
101 printf( "%s %d\n", (char *) root->avl_data, root->avl_bf );
103 ravl_print( root->avl_left, depth+1 );
106 static void myprint( Avlnode *root )
108 printf( "********\n" );
111 printf( "\tNULL\n" );
113 ravl_print( root, 0 );
115 printf( "********\n" );