]> git.sur5r.net Git - openldap/blob - libraries/libavl/testavl.c
de948d8bcd48193942f8e7bffeaa6cd9d3867b33
[openldap] / libraries / libavl / testavl.c
1 /* testavl.c - Test Tim Howes AVL code */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
5  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6  */
7
8 #include "portable.h"
9
10 #include <stdio.h>
11
12 #include <ac/stdlib.h>
13 #include <ac/string.h>
14
15 #define AVL_INTERNAL
16 #define AVL_NONREENTRANT 
17 #include "avl.h"
18
19 static void ravl_print LDAP_P(( Avlnode *root, int depth ));
20 static void myprint LDAP_P(( Avlnode *root ));
21
22 int
23 main( int argc, char **argv )
24 {
25         Avlnode *tree = NULL;
26         char    command[ 10 ];
27         char    name[ 80 ];
28         char    *p;
29
30         printf( "> " );
31         while ( fgets( command, sizeof( command ), stdin ) != NULL ) {
32                 switch( *command ) {
33                 case 'n':       /* new tree */
34                         ( void ) avl_free( tree, (AVL_FREE) free );
35                         tree = NULL;
36                         break;
37                 case 'p':       /* print */
38                         ( void ) myprint( tree );
39                         break;
40                 case 't':       /* traverse with first, next */
41 #ifdef AVL_NONREENTRANT
42                         printf( "***\n" );
43                         for ( p = (char * ) avl_getfirst( tree );
44                             p != NULL;
45                                 p = (char *) avl_getnext())
46                                 printf( "%s\n", p );
47                         printf( "***\n" );
48 #else
49                         printf( "*** reentrant interface not implemented ***" );
50 #endif
51                         break;
52                 case 'f':       /* find */
53                         printf( "data? " );
54                         if ( fgets( name, sizeof( name ), stdin ) == NULL )
55                                 exit( EXIT_SUCCESS );
56                         name[ strlen( name ) - 1 ] = '\0';
57                         if ( (p = (char *) avl_find( tree, name, (AVL_CMP) strcmp ))
58                             == NULL )
59                                 printf( "Not found.\n\n" );
60                         else
61                                 printf( "%s\n\n", p );
62                         break;
63                 case 'i':       /* insert */
64                         printf( "data? " );
65                         if ( fgets( name, sizeof( name ), stdin ) == NULL )
66                                 exit( EXIT_SUCCESS );
67                         name[ strlen( name ) - 1 ] = '\0';
68                         if ( avl_insert( &tree, strdup( name ), (AVL_CMP) strcmp, 
69                             avl_dup_error ) != 0 )
70                                 printf( "\nNot inserted!\n" );
71                         break;
72                 case 'd':       /* delete */
73                         printf( "data? " );
74                         if ( fgets( name, sizeof( name ), stdin ) == NULL )
75                                 exit( EXIT_SUCCESS );
76                         name[ strlen( name ) - 1 ] = '\0';
77                         if ( avl_delete( &tree, name, (AVL_CMP) strcmp ) == NULL )
78                                 printf( "\nNot found!\n" );
79                         break;
80                 case 'q':       /* quit */
81                         exit( EXIT_SUCCESS );
82                         break;
83                 case '\n':
84                         break;
85                 default:
86                         printf("Commands: insert, delete, print, new, quit\n");
87                 }
88
89                 printf( "> " );
90         }
91
92         return( 0 );
93 }
94
95 static void ravl_print( Avlnode *root, int depth )
96 {
97         int     i;
98
99         if ( root == 0 )
100                 return;
101
102         ravl_print( root->avl_right, depth+1 );
103
104         for ( i = 0; i < depth; i++ )
105                 printf( "   " );
106         printf( "%s %d\n", (char *) root->avl_data, root->avl_bf );
107
108         ravl_print( root->avl_left, depth+1 );
109 }
110
111 static void myprint( Avlnode *root )
112 {
113         printf( "********\n" );
114
115         if ( root == 0 )
116                 printf( "\tNULL\n" );
117         else
118                 ravl_print( root, 0 );
119
120         printf( "********\n" );
121 }