Back to index

openldap  2.4.31
testavl.c
Go to the documentation of this file.
00001 /* testavl.c - Test Tim Howes AVL code */
00002 /* $OpenLDAP$ */
00003 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
00004  *
00005  * Copyright 1998-2012 The OpenLDAP Foundation.
00006  * All rights reserved.
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted only as authorized by the OpenLDAP
00010  * Public License.
00011  *
00012  * A copy of this license is available in the file LICENSE in the
00013  * top-level directory of the distribution or, alternatively, at
00014  * <http://www.OpenLDAP.org/license.html>.
00015  */
00016 /* Portions Copyright (c) 1993 Regents of the University of Michigan.
00017  * All rights reserved.
00018  *
00019  * Redistribution and use in source and binary forms are permitted
00020  * provided that this notice is preserved and that due credit is given
00021  * to the University of Michigan at Ann Arbor. The name of the University
00022  * may not be used to endorse or promote products derived from this
00023  * software without specific prior written permission. This software
00024  * is provided ``as is'' without express or implied warranty.
00025  */
00026 /* ACKNOWLEDGEMENTS:
00027  * This work was originally developed by the University of Michigan
00028  * (as part of U-MICH LDAP).
00029  */
00030 
00031 #include "portable.h"
00032 
00033 #include <stdio.h>
00034 
00035 #include <ac/stdlib.h>
00036 #include <ac/string.h>
00037 
00038 #define AVL_INTERNAL
00039 #define AVL_NONREENTRANT 
00040 #include "avl.h"
00041 
00042 static void ravl_print LDAP_P(( Avlnode *root, int depth ));
00043 static void myprint LDAP_P(( Avlnode *root ));
00044 static int avl_strcmp LDAP_P(( const void *s, const void *t ));
00045 
00046 int
00047 main( int argc, char **argv )
00048 {
00049        Avlnode       *tree = NULL;
00050        char   command[ 10 ];
00051        char   name[ 80 ];
00052        char   *p;
00053 
00054        printf( "> " );
00055        while ( fgets( command, sizeof( command ), stdin ) != NULL ) {
00056               switch( *command ) {
00057               case 'n':     /* new tree */
00058                      ( void ) avl_free( tree, free );
00059                      tree = NULL;
00060                      break;
00061               case 'p':     /* print */
00062                      ( void ) myprint( tree );
00063                      break;
00064               case 't':     /* traverse with first, next */
00065 #ifdef AVL_NONREENTRANT
00066                      printf( "***\n" );
00067                      for ( p = (char * ) avl_getfirst( tree );
00068                          p != NULL;
00069                             p = (char *) avl_getnext())
00070                             printf( "%s\n", p );
00071                      printf( "***\n" );
00072 #else
00073                      printf( "*** reentrant interface not implemented ***" );
00074 #endif
00075                      break;
00076               case 'f':     /* find */
00077                      printf( "data? " );
00078                      if ( fgets( name, sizeof( name ), stdin ) == NULL )
00079                             exit( EXIT_SUCCESS );
00080                      name[ strlen( name ) - 1 ] = '\0';
00081                      if ( (p = (char *) avl_find( tree, name, avl_strcmp ))
00082                          == NULL )
00083                             printf( "Not found.\n\n" );
00084                      else
00085                             printf( "%s\n\n", p );
00086                      break;
00087               case 'i':     /* insert */
00088                      printf( "data? " );
00089                      if ( fgets( name, sizeof( name ), stdin ) == NULL )
00090                             exit( EXIT_SUCCESS );
00091                      name[ strlen( name ) - 1 ] = '\0';
00092                      if ( avl_insert( &tree, strdup( name ), avl_strcmp, 
00093                          avl_dup_error ) != 0 )
00094                             printf( "\nNot inserted!\n" );
00095                      break;
00096               case 'd':     /* delete */
00097                      printf( "data? " );
00098                      if ( fgets( name, sizeof( name ), stdin ) == NULL )
00099                             exit( EXIT_SUCCESS );
00100                      name[ strlen( name ) - 1 ] = '\0';
00101                      if ( avl_delete( &tree, name, avl_strcmp ) == NULL )
00102                             printf( "\nNot found!\n" );
00103                      break;
00104               case 'q':     /* quit */
00105                      exit( EXIT_SUCCESS );
00106                      break;
00107               case '\n':
00108                      break;
00109               default:
00110                      printf("Commands: insert, delete, print, new, quit\n");
00111               }
00112 
00113               printf( "> " );
00114        }
00115 
00116        return( 0 );
00117 }
00118 
00119 static void ravl_print( Avlnode *root, int depth )
00120 {
00121        int    i;
00122 
00123        if ( root == 0 )
00124               return;
00125 
00126        ravl_print( root->avl_right, depth+1 );
00127 
00128        for ( i = 0; i < depth; i++ )
00129               printf( "   " );
00130        printf( "%s %d\n", (char *) root->avl_data, root->avl_bf );
00131 
00132        ravl_print( root->avl_left, depth+1 );
00133 }
00134 
00135 static void myprint( Avlnode *root )
00136 {
00137        printf( "********\n" );
00138 
00139        if ( root == 0 )
00140               printf( "\tNULL\n" );
00141        else
00142               ravl_print( root, 0 );
00143 
00144        printf( "********\n" );
00145 }
00146 
00147 static int avl_strcmp( const void *s, const void *t )
00148 {
00149        return strcmp( s, t );
00150 }