1 /* testavl.c - Test Tim Howes AVL code */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2016 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). Additional contributors include
36 #include <ac/stdlib.h>
37 #include <ac/string.h>
42 static void ravl_print LDAP_P(( Avlnode *root, int depth, int thread ));
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 )
49 Avlnode *tree = NULL, *n;
55 while ( fgets( command, sizeof( command ), stdin ) != NULL ) {
57 case 'n': /* new tree */
58 ( void ) tavl_free( tree, free );
62 ( void ) myprint( tree );
64 case 't': /* traverse with first, next */
66 for ( n = tavl_end( tree, TAVL_DIR_LEFT );
68 n = tavl_next( n, TAVL_DIR_RIGHT ))
69 printf( "%s\n", n->avl_data );
74 if ( fgets( name, sizeof( name ), stdin ) == NULL )
76 name[ strlen( name ) - 1 ] = '\0';
77 if ( (p = (char *) tavl_find( tree, name, avl_strcmp ))
79 printf( "Not found.\n\n" );
81 printf( "%s\n\n", p );
83 case 'i': /* insert */
85 if ( fgets( name, sizeof( name ), stdin ) == NULL )
87 name[ strlen( name ) - 1 ] = '\0';
88 if ( tavl_insert( &tree, strdup( name ), avl_strcmp,
89 avl_dup_error ) != 0 )
90 printf( "\nNot inserted!\n" );
92 case 'd': /* delete */
94 if ( fgets( name, sizeof( name ), stdin ) == NULL )
96 name[ strlen( name ) - 1 ] = '\0';
97 if ( tavl_delete( &tree, name, avl_strcmp ) == NULL )
98 printf( "\nNot found!\n" );
101 exit( EXIT_SUCCESS );
106 printf("Commands: insert, delete, print, new, quit\n");
115 static const char bfc_array[] = "\\-/";
116 static const char *bfcs = bfc_array+1;
118 static void ravl_print( Avlnode *root, int depth, int thread )
122 if ( root && !thread )
123 ravl_print( root->avl_link[1], depth+1, root->avl_bits[1] == AVL_THREAD );
125 for ( i = 0; i < depth; i++ )
130 printf( "%c", bfcs[root->avl_bf] );
137 printf( "%s\n", (char *) root->avl_data );
140 ravl_print( root->avl_link[0], depth+1, root->avl_bits[0] == AVL_THREAD );
143 static void myprint( Avlnode *root )
145 printf( "********\n" );
148 printf( "\tNULL\n" );
150 ravl_print( root, 0, 0 );
152 printf( "********\n" );
155 static int avl_strcmp( const void *s, const void *t )
157 return strcmp( s, t );