1 /* testavl.c - Test Tim Howes AVL code */
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 */
36 for ( p = (char * ) avl_getfirst( tree );
37 p != NULL; p = (char *) avl_getnext( /* tree, p */ ) )
43 if ( fgets( name, sizeof( name ), stdin ) == NULL )
45 name[ strlen( name ) - 1 ] = '\0';
46 if ( (p = (char *) avl_find( tree, name, (AVL_CMP) strcmp ))
48 printf( "Not found.\n\n" );
50 printf( "%s\n\n", p );
52 case 'i': /* insert */
54 if ( fgets( name, sizeof( name ), stdin ) == NULL )
56 name[ strlen( name ) - 1 ] = '\0';
57 if ( avl_insert( &tree, strdup( name ), (AVL_CMP) strcmp,
58 avl_dup_error ) != 0 )
59 printf( "\nNot inserted!\n" );
61 case 'd': /* delete */
63 if ( fgets( name, sizeof( name ), stdin ) == NULL )
65 name[ strlen( name ) - 1 ] = '\0';
66 if ( avl_delete( &tree, name, (AVL_CMP) strcmp ) == NULL )
67 printf( "\nNot found!\n" );
75 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", 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" );