Changeset 66

Show
Ignore:
Timestamp:
09/07/06 16:04:39 (4 years ago)
Author:
aqua
Message:

clean up print functions & move test stubs to libbst_test.c

Location:
libbst
Files:
1 added
2 modified

Legend:

Unmodified
Added
Removed
  • libbst/Makefile

    r64 r66  
    77all: libbst.o test 
    88 
    9 test:  libbst.o test.o  
    10         gcc ${CFLAGS} ${DEBUG} ${LDFLAGS} -o $@ libbst.o test.o 
     9test:  libbst.o libbst_test.o test.o  
     10        gcc ${CFLAGS} ${DEBUG} ${LDFLAGS} -o $@ libbst.o libbst_test.o test.o 
    1111         
    1212clean:  
    13         rm -rf libbst.o test.o test test.out 
     13        rm -rf libbst.o libbst_test.o test.o test test.out 
  • libbst/libbst.c

    r64 r66  
    1 #include <stdio.h> 
    2 #include <math.h> 
     1#include <stdlib.h> 
    32 
    43#include "libbst.h" 
    5  
    6 #define ERROR -1 
    74 
    85/* get the min value of tree.. */ 
     
    294291 
    295292} 
    296  
    297 int bst_print_level( bst_ptr root, int recur_num, int level, int max_level , int lr, FILE* out ){ 
    298  
    299         int i = 0; 
    300         int space; 
    301          
    302         if( root == NULL ){ 
    303                          
    304                 return ( recur_num * ( -1 ) ); 
    305  
    306         } 
    307         else { 
    308  
    309                 if( recur_num > 0 ){ 
    310  
    311                         if( root->left != NULL ) 
    312                                 bst_print_level( root->left , (recur_num - 1) , level,  max_level , 0,out  ); 
    313                          
    314                         else{ 
    315  
    316                                 space = pow( 2, (max_level - recur_num) )-1; 
    317                                 for( i = 0 ; i < ( space*2 - 1 ); i++ ) 
    318                                         fprintf( out," " ); 
    319  
    320                         } 
    321                          
    322                         space = pow( 2, (max_level - recur_num) ) - 1; 
    323                          
    324                         for( i = 0 ; i <= (space+1)  ; i++ ) 
    325                                 fprintf(out, " " ); 
    326                          
    327                         if( root->right != NULL ) 
    328                                 bst_print_level( root->right , (recur_num - 1), level,  max_level , 0, out ); 
    329                          
    330                         else{ 
    331  
    332                                 space = pow( 2, (max_level - recur_num))-1; 
    333                                 for( i = 0 ; i < (space*2 + 1 ); i++ ) 
    334                                         fprintf( out, " " ); 
    335  
    336                         } 
    337                          
    338                 } 
    339                 else{ 
    340                                  
    341                         fprintf( out, "%2d", root->var ); 
    342                 } 
    343                  
    344         } 
    345         return 0; 
    346          
    347          
    348 } 
    349  
    350 /* print */ 
    351 void bst_print( bst_ptr root, FILE* out ){ 
    352  
    353         int level; 
    354         int i, j; 
    355  
    356         int space; 
    357  
    358         level = bst_get_level( root ); 
    359         for( i = 0 ; i < level ; i++ ){ 
    360  
    361                 space = pow( 2, ( level - i ) ) - 2; 
    362                  
    363                 for( j = 0 ; j < space ; j++ ) 
    364                         fprintf( out, " "); 
    365  
    366                 bst_print_level( root, i, i, level, 0, out ); 
    367                 fprintf( out, "\n"); 
    368                  
    369         } 
    370  
    371 } 
    372