testavl.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  3. * Copyright (C) 2005 Red Hat, Inc.
  4. * All rights reserved.
  5. *
  6. * License: GPL (version 3 or any later version).
  7. * See LICENSE for details.
  8. * END COPYRIGHT BLOCK **/
  9. #ifdef HAVE_CONFIG_H
  10. # include <config.h>
  11. #endif
  12. /* testavl.c - Test Tim Howes AVL code */
  13. #include <sys/types.h>
  14. #include <stdio.h>
  15. #include "avl.h"
  16. char *strdup( s )
  17. char *s;
  18. {
  19. char *new;
  20. if ( (new = (char *) malloc( strlen( s ) + 1 )) == NULL )
  21. return( NULL );
  22. strcpy( new, s );
  23. return( new );
  24. }
  25. main( argc, argv )
  26. int argc;
  27. char **argv;
  28. {
  29. Avlnode *tree = NULLAVL;
  30. char command[ 10 ];
  31. char name[ 80 ];
  32. char *p;
  33. int free(), strcmp();
  34. printf( "> " );
  35. while ( fgets( command, sizeof( command ), stdin ) != NULL ) {
  36. switch( *command ) {
  37. case 'n': /* new tree */
  38. ( void ) avl_free( tree, free );
  39. tree = NULLAVL;
  40. break;
  41. case 'p': /* print */
  42. ( void ) myprint( tree );
  43. break;
  44. case 't': /* traverse with first, next */
  45. printf( "***\n" );
  46. for ( p = (char * ) avl_getfirst( tree );
  47. p != NULL; p = (char *) avl_getnext( tree, p ) )
  48. printf( "%s\n", p );
  49. printf( "***\n" );
  50. break;
  51. case 'f': /* find */
  52. printf( "data? " );
  53. if ( fgets( name, sizeof( name ), stdin ) == NULL )
  54. exit( 0 );
  55. name[ strlen( name ) - 1 ] = '\0';
  56. if ( (p = (char *) avl_find( tree, name, strcmp ))
  57. == NULL )
  58. printf( "Not found.\n\n" );
  59. else
  60. printf( "%s\n\n", p );
  61. break;
  62. case 'i': /* insert */
  63. printf( "data? " );
  64. if ( fgets( name, sizeof( name ), stdin ) == NULL )
  65. exit( 0 );
  66. name[ strlen( name ) - 1 ] = '\0';
  67. if ( avl_insert( &tree, strdup( name ), strcmp,
  68. avl_dup_error ) != OK )
  69. printf( "\nNot inserted!\n" );
  70. break;
  71. case 'd': /* delete */
  72. printf( "data? " );
  73. if ( fgets( name, sizeof( name ), stdin ) == NULL )
  74. exit( 0 );
  75. name[ strlen( name ) - 1 ] = '\0';
  76. if ( avl_delete( &tree, name, strcmp ) == NULL )
  77. printf( "\nNot found!\n" );
  78. break;
  79. case 'q': /* quit */
  80. exit( 0 );
  81. break;
  82. case '\n':
  83. break;
  84. default:
  85. printf("Commands: insert, delete, print, new, quit\n");
  86. }
  87. printf( "> " );
  88. }
  89. /* NOTREACHED */
  90. }
  91. static ravl_print( root, depth )
  92. Avlnode *root;
  93. int depth;
  94. {
  95. int i;
  96. if ( root == 0 )
  97. return;
  98. ravl_print( root->avl_right, depth+1 );
  99. for ( i = 0; i < depth; i++ )
  100. printf( " " );
  101. printf( "%s %d\n", root->avl_data, root->avl_bf );
  102. ravl_print( root->avl_left, depth+1 );
  103. }
  104. myprint( root )
  105. Avlnode *root;
  106. {
  107. printf( "********\n" );
  108. if ( root == 0 )
  109. printf( "\tNULL\n" );
  110. else
  111. ( void ) ravl_print( root, 0 );
  112. printf( "********\n" );
  113. }