1 /* testavl.c - Test Tim Howes AVL code */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2018 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
16 /* Portions Copyright (c) 1993 Regents of the University of Michigan.
17 * All rights reserved.
19 * Redistribution and use in source and binary forms are permitted
20 * provided that this notice is preserved and that due credit is given
21 * to the University of Michigan at Ann Arbor. The name of the University
22 * may not be used to endorse or promote products derived from this
23 * software without specific prior written permission. This software
24 * is provided ``as is'' without express or implied warranty.
27 * This work was originally developed by the University of Michigan
28 * (as part of U-MICH LDAP).
35 #include <ac/stdlib.h>
36 #include <ac/string.h>
39 #define AVL_NONREENTRANT
42 static void ravl_print LDAP_P(( Avlnode *root, int depth ));
43 static void myprint LDAP_P(( Avlnode *root ));
44 static int avl_strcmp LDAP_P(( const void *s, const void *t ));
47 main( int argc, char **argv )
55 while ( fgets( command, sizeof( command ), stdin ) != NULL ) {
57 case 'n': /* new tree */
58 ( void ) avl_free( tree, free );
62 ( void ) myprint( tree );
64 case 't': /* traverse with first, next */
65 #ifdef AVL_NONREENTRANT
67 for ( p = (char * ) avl_getfirst( tree );
69 p = (char *) avl_getnext())
73 printf( "*** reentrant interface not implemented ***" );
78 if ( fgets( name, sizeof( name ), stdin ) == NULL )
80 name[ strlen( name ) - 1 ] = '\0';
81 if ( (p = (char *) avl_find( tree, name, avl_strcmp ))
83 printf( "Not found.\n\n" );
85 printf( "%s\n\n", p );
87 case 'i': /* insert */
89 if ( fgets( name, sizeof( name ), stdin ) == NULL )
91 name[ strlen( name ) - 1 ] = '\0';
92 if ( avl_insert( &tree, strdup( name ), avl_strcmp,
93 avl_dup_error ) != 0 )
94 printf( "\nNot inserted!\n" );
96 case 'd': /* delete */
98 if ( fgets( name, sizeof( name ), stdin ) == NULL )
100 name[ strlen( name ) - 1 ] = '\0';
101 if ( avl_delete( &tree, name, avl_strcmp ) == NULL )
102 printf( "\nNot found!\n" );
105 exit( EXIT_SUCCESS );
110 printf("Commands: insert, delete, print, new, quit\n");
119 static void ravl_print( Avlnode *root, int depth )
126 ravl_print( root->avl_right, depth+1 );
128 for ( i = 0; i < depth; i++ )
130 printf( "%s %d\n", (char *) root->avl_data, root->avl_bf );
132 ravl_print( root->avl_left, depth+1 );
135 static void myprint( Avlnode *root )
137 printf( "********\n" );
140 printf( "\tNULL\n" );
142 ravl_print( root, 0 );
144 printf( "********\n" );
147 static int avl_strcmp( const void *s, const void *t )
149 return strcmp( s, t );