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