| | 90 | static 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 */ |
| | 106 | static 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 | |
| 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 ) |
| 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 | |