Changeset 69 for libbst/libbst.c

Show
Ignore:
Timestamp:
09/08/06 17:39:34 (4 years ago)
Author:
aqua
Message:

code clean up finished~

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • libbst/libbst.c

    r66 r69  
    44 
    55/* get the min value of tree.. */ 
    6 bst_ptr bst_get_min( bst_ptr root ){ 
    7  
    8         bst_ptr tmp; 
    9  
    10         tmp = root; 
    11          
    12         if( tmp != NULL ){ 
    13  
    14                 /* go leftest!! */ 
    15                 while( tmp->left != NULL ) 
    16                         tmp = tmp->left; 
    17  
    18         } 
    19         return tmp; 
     6static int bst_get_min( bst_ptr node ){ 
     7 
     8        /* go leftest!! */ 
     9        if( node != NULL ) 
     10                while( node->left != NULL ) 
     11                        node = node->left; 
     12 
     13        return node->var; 
    2014 
    2115} 
    2216 
    2317/* get the max value of tree.. */ 
    24 bst_ptr bst_get_max( bst_ptr root ){ 
    25  
    26         bst_ptr tmp; 
    27  
    28         tmp = root; 
    29          
    30         if( tmp != NULL ){ 
    31  
    32                 /* go rightest !! */ 
    33                 while( tmp->right != NULL ) 
    34                         tmp = tmp->right; 
    35  
    36         } 
    37         return tmp; 
     18static int bst_get_max( bst_ptr node ){ 
     19 
     20        /* go rightest !! */ 
     21        if( node != NULL ) 
     22                while( node->right != NULL ) 
     23                        node = node->right; 
     24 
     25        return node->var; 
    3826 
    3927} 
     
    10088} 
    10189 
     90static bst_ptr bst_create_node( int var ){ 
     91 
     92        bst_ptr tmp; 
     93 
     94        tmp = (bst_ptr) malloc( sizeof(bst) );  
     95         
     96        tmp->var = var; 
     97        tmp->left = NULL; 
     98        tmp->right = NULL; 
     99 
     100        return tmp; 
     101 
     102} 
     103 
     104#if 0 
     105/* this interface will be better in avl tree */ 
     106static bst_ptr bst_real_add( bst_ptr cur, int var ){ 
     107 
     108        bst_ptr tmp; 
     109         
     110        if( cur == NULL ){ 
     111 
     112                tmp = bst_create_node( var ); 
     113                return tmp; 
     114 
     115        } 
     116 
     117        /* if the key is already in this tree... then error! */ 
     118        else if( cur->var == var ) 
     119                return NULL; 
     120 
     121        /* if the given key is smaller than current key... traversal right child */ 
     122        else if( cur->var < var ){ 
     123 
     124                if( (tmp = bst_real_add( cur->right, var )) == NULL ) 
     125                        return NULL; 
     126                 
     127                cur->right = tmp; 
     128                return cur; 
     129 
     130        } 
     131 
     132        /* if the given key is bigger than current key... traversal left child */ 
     133        else { 
     134                if( (tmp = bst_real_add( cur->left, var )) == NULL ) 
     135                        return NULL; 
     136 
     137                cur->left = tmp; 
     138                return cur; 
     139        } 
     140 
     141} 
     142#endif 
     143 
    102144/* add */ 
    103145int bst_add( bst_ptr* root, int var ){ 
     
    105147        bst_ptr tmp; 
    106148 
     149        /*  
     150         * simple way (this will be use in AVL) 
     151         * 
     152         * (*root) = bst_real_add( *root, var ); 
     153         * return 1; 
     154         * 
     155         * simple way finished  
     156         */ 
     157 
     158        /* if tree is empty, then create node & make it root node of tree */ 
    107159        if( (*root) == NULL ){ 
    108  
    109                 if( (*root) = (bst_ptr) malloc( sizeof( bst ) ) ){ 
    110                  
    111                         (*root)->var = var; 
    112                         (*root)->left = NULL; 
    113                         (*root)->right = NULL; 
    114                         (*root)->mother = NULL; 
    115  
    116                         return 1; 
    117  
    118                 } 
    119                 else 
     160                (*root) = bst_create_node( var ); 
     161                return 1; 
     162        } 
     163 
     164        tmp = (*root); 
     165        do { 
     166 
     167                /* if given key is bigger than current key */ 
     168                if( tmp->var > var ){ 
     169 
     170                        /* follow left child, if exists */ 
     171                        if( tmp->left != NULL ) 
     172                                tmp = tmp->left; 
     173                         
     174                        /* if not, create node & attach it as left child */ 
     175                        else{ 
     176                                tmp->left = bst_create_node( var ); 
     177                                break; 
     178                        } 
     179                         
     180                } 
     181 
     182                /* if given key is same as current key then error! */ 
     183                else if( tmp->var == var ) 
    120184                        return ERROR; 
    121  
    122         } 
    123         else{ 
    124  
    125                 tmp = (*root); 
    126  
    127                 while( tmp != NULL ){ 
    128  
    129                         if( tmp->var > var ){ 
    130  
    131                                 if( tmp->left != NULL ) 
    132                                         tmp = tmp->left; 
    133                                  
    134                                 else{ 
    135                                                  
    136                                         tmp->left = (bst_ptr) malloc( sizeof( bst ) ); 
    137                                          
    138                                         tmp->left->mother = tmp; 
    139                                         tmp = tmp->left; 
    140                                         break; 
    141  
    142                                 } 
    143                                  
     185                 
     186                /* if given key is bigger than current key */ 
     187                else { 
     188 
     189                        /* if this node has right child, then follow it */ 
     190                        if( tmp->right != NULL ) 
     191                                tmp = tmp->right; 
     192                         
     193                        /* else create node and attach it as right child */ 
     194                        else{ 
     195                                tmp->right = bst_create_node( var ); 
     196                                break; 
    144197                        } 
    145                         else if( tmp->var == var ) 
    146                                 return ERROR; 
    147                          
    148                         else{ 
    149  
    150                                 if( tmp->right != NULL ) 
    151                                         tmp = tmp->right; 
    152                                  
    153                                 else{ 
    154                                                  
    155                                         tmp->right = (bst_ptr) malloc( sizeof( bst ) ); 
    156                                          
    157                                         tmp->right->mother = tmp; 
    158                                         tmp = tmp->right; 
    159                                         break; 
    160  
    161                                 } 
    162  
    163                         } 
    164  
    165                 } 
    166                 tmp->var = var; 
    167                 tmp->left = NULL; 
    168                 tmp->right = NULL; 
    169  
    170                 return 1; 
    171                  
    172         } 
     198 
     199                } 
     200 
     201        } while( tmp != NULL ); 
     202 
     203        return 1; 
    173204         
    174205} 
     
    177208int bst_del( bst_ptr* root, int var ){ 
    178209 
    179         bst_ptr tmp, tmp_var; 
    180  
    181         /* empty tree.. */ 
    182         if( ( tmp_var = (bst_ptr) bst_search( *root, var ) ) == NULL ) 
     210        bst_ptr mother, cur; 
     211        int tmp; 
     212 
     213        mother = NULL; 
     214        cur = (*root); 
     215 
     216        /* find the node */ 
     217        while( cur != NULL ){ 
     218                /* searching complete.. */ 
     219                if( cur->var == var ) 
     220                        break; 
     221                /* go left */ 
     222                else if( cur->var > var ){ 
     223                        mother = cur; 
     224                        cur = cur->left; 
     225                } 
     226                /* go right */ 
     227                else if( cur->var < var ){ 
     228                        mother = cur; 
     229                        cur = cur->right; 
     230                } 
     231        } 
     232 
     233        /* if can't find given key, then error */ 
     234        if( cur == NULL ) 
    183235                return ERROR; 
    184236 
    185         /* nonempty tree */ 
    186         else{ 
    187  
    188                 /* has no child.. */ 
    189                 if( tmp_var->left == NULL && tmp_var->right == NULL ){ 
    190                                  
    191                         tmp = tmp_var; 
    192                          
    193                         /* if it's root */ 
    194                         if( tmp_var->mother == NULL ){ 
    195  
    196                                 (*root)=NULL; 
    197  
    198                         } 
    199  
    200                         /* if its mother's left */ 
    201                         else if( tmp_var->mother->left != tmp_var ) 
    202                                 tmp_var->mother->right = NULL; 
    203  
    204                         /* if its mother's right */ 
    205                         else 
    206                                 tmp_var->mother->left = NULL; 
    207                          
    208                         free( tmp ); 
    209  
    210                                  
    211                 } 
    212  
    213                 /* has one child */ 
    214                 else if( tmp_var->left == NULL ){ 
    215  
    216                         tmp = tmp_var; 
    217          
    218                         /* if it's root */ 
    219                         if( tmp_var->mother == NULL ){ 
    220  
    221                                 (*root) = tmp->right; 
    222                                 tmp->right->mother = NULL; 
    223                                  
    224                         } 
    225  
    226                         /* it's mother's left child.. */ 
    227                         else if( tmp_var->mother->left == tmp_var ){ 
    228  
    229                                 tmp_var->mother->left = tmp_var->right; 
    230                                 tmp_var->mother = tmp->mother; 
    231                                  
    232                         } 
    233  
    234                         /* it's mother's right child */ 
    235                         else{ 
    236                                          
    237                                 tmp_var->mother->right = tmp_var->right; 
    238                                 tmp_var->mother = tmp->mother; 
    239  
    240                         } 
    241                          
    242                          
    243                 } 
    244  
    245                 /* has only right child.. */ 
    246                 else if( tmp_var->right == NULL ){ 
    247                                  
    248                         tmp = tmp_var; 
    249                          
    250                         if( tmp_var->mother == NULL ){ 
    251  
    252                                 (*root) = tmp->left; 
    253                                 (*root)->mother = NULL; 
    254                                  
    255                         } 
    256                         else if( tmp_var->mother->left == tmp_var ){ 
    257  
    258                                 tmp_var->mother->left = tmp_var->left; 
    259                                 tmp_var->mother->left->mother = tmp_var->mother; 
    260                                  
    261                         } 
    262                         else{ 
    263                                          
    264                                 tmp_var->mother->right = tmp_var->left; 
    265                                 tmp_var->mother->right->mother = tmp_var->mother; 
    266  
    267                         } 
    268  
    269                          
    270                 } 
    271                 /* has two child.. */ 
    272                 else{ 
    273                                  
    274                         /* get left's max value.. & swap.. */ 
    275                         tmp = (bst_ptr)bst_get_max( tmp_var->left ); 
    276                         tmp_var->var = tmp->var; 
    277  
    278                         /* & delete the left's max var */ 
    279                         if( tmp_var->left != tmp ) 
    280                                 tmp->mother->right = NULL; 
    281  
    282                         else 
    283                                 tmp_var->left = NULL; 
    284                          
    285                         free( tmp ); 
    286  
    287                 } 
    288                 return 1; 
    289                  
    290         } 
    291  
    292 } 
     237        /* if has left child */ 
     238        else if( cur->left != NULL ){ 
     239                tmp = bst_get_max(cur->left); 
     240                bst_del( &(cur->left), tmp ); 
     241 
     242                cur->var = tmp; 
     243                return 1; 
     244        } 
     245        /* if has right child */ 
     246        else if( cur->right != NULL ){ 
     247                tmp = bst_get_min(cur->right); 
     248                bst_del( &(cur->right), tmp ); 
     249 
     250                cur->var = tmp; 
     251                return 1; 
     252        } 
     253        /* if has no child */ 
     254        else { 
     255                if( mother == NULL ) 
     256                        (*root) = NULL; 
     257 
     258                else if( cur == mother->left ) 
     259                        mother->left = NULL; 
     260 
     261                else  
     262                        mother->right = NULL; 
     263 
     264                free(cur); 
     265                return 1; 
     266        } 
     267 
     268} 
     269