1 /* testavl.c - Test Tim Howes AVL code */
11 if ( (new = (char *) malloc( strlen( s ) + 1 )) == NULL )
23 Avlnode *tree = NULLAVL;
30 while ( fgets( command, sizeof( command ), stdin ) != NULL ) {
32 case 'n': /* new tree */
33 ( void ) avl_free( tree, free );
37 ( void ) myprint( tree );
39 case 't': /* traverse with first, next */
41 for ( p = (char * ) avl_getfirst( tree );
42 p != NULL; p = (char *) avl_getnext( tree, p ) )
48 if ( fgets( name, sizeof( name ), stdin ) == NULL )
50 name[ strlen( name ) - 1 ] = '\0';
51 if ( (p = (char *) avl_find( tree, name, 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 ), strcmp,
63 avl_dup_error ) != OK )
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, strcmp ) == NULL )
72 printf( "\nNot found!\n" );
80 printf("Commands: insert, delete, print, new, quit\n");
88 static ravl_print( root, depth )
97 ravl_print( root->avl_right, depth+1 );
99 for ( i = 0; i < depth; i++ )
101 printf( "%s %d\n", root->avl_data, root->avl_bf );
103 ravl_print( root->avl_left, depth+1 );
109 printf( "********\n" );
112 printf( "\tNULL\n" );
114 ( void ) ravl_print( root, 0 );
116 printf( "********\n" );