1 /* testavl.c - Test Tim Howes AVL code */
12 static void ravl_print LDAP_P(( Avlnode *root, int depth ));
13 static void myprint LDAP_P(( Avlnode *root ));
16 main( int argc, char **argv )
18 Avlnode *tree = NULLAVL;
24 while ( fgets( command, sizeof( command ), stdin ) != NULL ) {
26 case 'n': /* new tree */
27 ( void ) avl_free( tree, (AVL_FREE) free );
31 ( void ) myprint( tree );
33 case 't': /* traverse with first, next */
35 for ( p = (char * ) avl_getfirst( tree );
36 p != NULL; p = (char *) avl_getnext( /* tree, p */ ) )
42 if ( fgets( name, sizeof( name ), stdin ) == NULL )
44 name[ strlen( name ) - 1 ] = '\0';
45 if ( (p = (char *) avl_find( tree, name, (AVL_CMP) strcmp ))
47 printf( "Not found.\n\n" );
49 printf( "%s\n\n", p );
51 case 'i': /* insert */
53 if ( fgets( name, sizeof( name ), stdin ) == NULL )
55 name[ strlen( name ) - 1 ] = '\0';
56 if ( avl_insert( &tree, strdup( name ), (AVL_CMP) strcmp,
57 avl_dup_error ) != 0 )
58 printf( "\nNot inserted!\n" );
60 case 'd': /* delete */
62 if ( fgets( name, sizeof( name ), stdin ) == NULL )
64 name[ strlen( name ) - 1 ] = '\0';
65 if ( avl_delete( &tree, name, (AVL_CMP) strcmp ) == NULL )
66 printf( "\nNot found!\n" );
74 printf("Commands: insert, delete, print, new, quit\n");
83 static void ravl_print( Avlnode *root, int depth )
90 ravl_print( root->avl_right, depth+1 );
92 for ( i = 0; i < depth; i++ )
94 printf( "%s %d\n", (char *) root->avl_data, root->avl_bf );
96 ravl_print( root->avl_left, depth+1 );
99 static void myprint( Avlnode *root )
101 printf( "********\n" );
104 printf( "\tNULL\n" );
106 ravl_print( root, 0 );
108 printf( "********\n" );