1 /* testavl.c - Test Tim Howes AVL code */
10 #define AVL_NONREENTRANT
13 static void ravl_print LDAP_P(( Avlnode *root, int depth ));
14 static void myprint LDAP_P(( Avlnode *root ));
17 main( int argc, char **argv )
19 Avlnode *tree = NULLAVL;
25 while ( fgets( command, sizeof( command ), stdin ) != NULL ) {
27 case 'n': /* new tree */
28 ( void ) avl_free( tree, (AVL_FREE) free );
32 ( void ) myprint( tree );
34 case 't': /* traverse with first, next */
35 #ifdef AVL_NONREENTRANT
37 for ( p = (char * ) avl_getfirst( tree );
39 p = (char *) avl_getnext())
43 printf( "*** reentrant interface not implemented ***" );
48 if ( fgets( name, sizeof( name ), stdin ) == NULL )
50 name[ strlen( name ) - 1 ] = '\0';
51 if ( (p = (char *) avl_find( tree, name, (AVL_CMP) strcmp ))
53 printf( "Not found.\n\n" );
55 printf( "%s\n\n", p );
57 case 'i': /* insert */
59 if ( fgets( name, sizeof( name ), stdin ) == NULL )
61 name[ strlen( name ) - 1 ] = '\0';
62 if ( avl_insert( &tree, strdup( name ), (AVL_CMP) strcmp,
63 avl_dup_error ) != 0 )
64 printf( "\nNot inserted!\n" );
66 case 'd': /* delete */
68 if ( fgets( name, sizeof( name ), stdin ) == NULL )
70 name[ strlen( name ) - 1 ] = '\0';
71 if ( avl_delete( &tree, name, (AVL_CMP) strcmp ) == NULL )
72 printf( "\nNot found!\n" );
80 printf("Commands: insert, delete, print, new, quit\n");
89 static void ravl_print( Avlnode *root, int depth )
96 ravl_print( root->avl_right, depth+1 );
98 for ( i = 0; i < depth; i++ )
100 printf( "%s %d\n", (char *) root->avl_data, root->avl_bf );
102 ravl_print( root->avl_left, depth+1 );
105 static void myprint( Avlnode *root )
107 printf( "********\n" );
110 printf( "\tNULL\n" );
112 ravl_print( root, 0 );
114 printf( "********\n" );