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 ));
20 Avlnode *tree = NULLAVL;
26 while ( fgets( command, sizeof( command ), stdin ) != NULL ) {
28 case 'n': /* new tree */
29 ( void ) avl_free( tree, free );
33 ( void ) myprint( tree );
35 case 't': /* traverse with first, next */
37 for ( p = (char * ) avl_getfirst( tree );
38 p != NULL; p = (char *) avl_getnext( /* tree, p */ ) )
44 if ( fgets( name, sizeof( name ), stdin ) == NULL )
46 name[ strlen( name ) - 1 ] = '\0';
47 if ( (p = (char *) avl_find( tree, name, strcmp ))
49 printf( "Not found.\n\n" );
51 printf( "%s\n\n", p );
53 case 'i': /* insert */
55 if ( fgets( name, sizeof( name ), stdin ) == NULL )
57 name[ strlen( name ) - 1 ] = '\0';
58 if ( avl_insert( &tree, strdup( name ), strcmp,
59 avl_dup_error ) != 0 )
60 printf( "\nNot inserted!\n" );
62 case 'd': /* delete */
64 if ( fgets( name, sizeof( name ), stdin ) == NULL )
66 name[ strlen( name ) - 1 ] = '\0';
67 if ( avl_delete( &tree, name, strcmp ) == NULL )
68 printf( "\nNot found!\n" );
76 printf("Commands: insert, delete, print, new, quit\n");
84 static void ravl_print( Avlnode *root, int depth )
91 ravl_print( root->avl_right, depth+1 );
93 for ( i = 0; i < depth; i++ )
95 printf( "%s %d\n", root->avl_data, root->avl_bf );
97 ravl_print( root->avl_left, depth+1 );
100 static void myprint( Avlnode *root )
102 printf( "********\n" );
105 printf( "\tNULL\n" );
107 ravl_print( root, 0 );
109 printf( "********\n" );