testavl.c revision 1.2
1/* $NetBSD: testavl.c,v 1.2 2021/08/14 16:14:56 christos Exp $ */ 2 3/* testavl.c - Test Tim Howes AVL code */ 4/* $OpenLDAP$ */ 5/* This work is part of OpenLDAP Software <http://www.openldap.org/>. 6 * 7 * Copyright 1998-2021 The OpenLDAP Foundation. 8 * All rights reserved. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted only as authorized by the OpenLDAP 12 * Public License. 13 * 14 * A copy of this license is available in the file LICENSE in the 15 * top-level directory of the distribution or, alternatively, at 16 * <http://www.OpenLDAP.org/license.html>. 17 */ 18/* Portions Copyright (c) 1993 Regents of the University of Michigan. 19 * All rights reserved. 20 * 21 * Redistribution and use in source and binary forms are permitted 22 * provided that this notice is preserved and that due credit is given 23 * to the University of Michigan at Ann Arbor. The name of the University 24 * may not be used to endorse or promote products derived from this 25 * software without specific prior written permission. This software 26 * is provided ``as is'' without express or implied warranty. 27 */ 28/* ACKNOWLEDGEMENTS: 29 * This work was originally developed by the University of Michigan 30 * (as part of U-MICH LDAP). 31 */ 32 33#include <sys/cdefs.h> 34__RCSID("$NetBSD: testavl.c,v 1.2 2021/08/14 16:14:56 christos Exp $"); 35 36#include "portable.h" 37 38#include <stdio.h> 39 40#include <ac/stdlib.h> 41#include <ac/string.h> 42 43#define AVL_INTERNAL 44#define AVL_NONREENTRANT 45#include "ldap_avl.h" 46 47static void ravl_print LDAP_P(( Avlnode *root, int depth )); 48static void myprint LDAP_P(( Avlnode *root )); 49static int avl_strcmp LDAP_P(( const void *s, const void *t )); 50 51int 52main( int argc, char **argv ) 53{ 54 Avlnode *tree = NULL; 55 char command[ 10 ]; 56 char name[ 80 ]; 57 char *p; 58 59 printf( "> " ); 60 while ( fgets( command, sizeof( command ), stdin ) != NULL ) { 61 switch( *command ) { 62 case 'n': /* new tree */ 63 ( void ) ldap_avl_free( tree, free ); 64 tree = NULL; 65 break; 66 case 'p': /* print */ 67 ( void ) myprint( tree ); 68 break; 69 case 't': /* traverse with first, next */ 70#ifdef AVL_NONREENTRANT 71 printf( "***\n" ); 72 for ( p = (char * ) ldap_avl_getfirst( tree ); 73 p != NULL; 74 p = (char *) ldap_avl_getnext()) 75 printf( "%s\n", p ); 76 printf( "***\n" ); 77#else 78 printf( "*** reentrant interface not implemented ***" ); 79#endif 80 break; 81 case 'f': /* find */ 82 printf( "data? " ); 83 if ( fgets( name, sizeof( name ), stdin ) == NULL ) 84 exit( EXIT_SUCCESS ); 85 name[ strlen( name ) - 1 ] = '\0'; 86 if ( (p = (char *) ldap_avl_find( tree, name, avl_strcmp )) 87 == NULL ) 88 printf( "Not found.\n\n" ); 89 else 90 printf( "%s\n\n", p ); 91 break; 92 case 'i': /* insert */ 93 printf( "data? " ); 94 if ( fgets( name, sizeof( name ), stdin ) == NULL ) 95 exit( EXIT_SUCCESS ); 96 name[ strlen( name ) - 1 ] = '\0'; 97 if ( ldap_avl_insert( &tree, strdup( name ), avl_strcmp, 98 ldap_avl_dup_error ) != 0 ) 99 printf( "\nNot inserted!\n" ); 100 break; 101 case 'd': /* delete */ 102 printf( "data? " ); 103 if ( fgets( name, sizeof( name ), stdin ) == NULL ) 104 exit( EXIT_SUCCESS ); 105 name[ strlen( name ) - 1 ] = '\0'; 106 if ( ldap_avl_delete( &tree, name, avl_strcmp ) == NULL ) 107 printf( "\nNot found!\n" ); 108 break; 109 case 'q': /* quit */ 110 exit( EXIT_SUCCESS ); 111 break; 112 case '\n': 113 break; 114 default: 115 printf("Commands: insert, delete, print, new, quit\n"); 116 } 117 118 printf( "> " ); 119 } 120 121 return( 0 ); 122} 123 124static void ravl_print( Avlnode *root, int depth ) 125{ 126 int i; 127 128 if ( root == 0 ) 129 return; 130 131 ravl_print( root->avl_right, depth+1 ); 132 133 for ( i = 0; i < depth; i++ ) 134 printf( " " ); 135 printf( "%s %d\n", (char *) root->avl_data, root->avl_bf ); 136 137 ravl_print( root->avl_left, depth+1 ); 138} 139 140static void myprint( Avlnode *root ) 141{ 142 printf( "********\n" ); 143 144 if ( root == 0 ) 145 printf( "\tNULL\n" ); 146 else 147 ravl_print( root, 0 ); 148 149 printf( "********\n" ); 150} 151 152static int avl_strcmp( const void *s, const void *t ) 153{ 154 return strcmp( s, t ); 155} 156