31 template<
typename Key,
33 typename Compare = less<Key> >
64 void delete_children_recursive()
68 left->delete_children_recursive();
74 right->delete_children_recursive();
116 root->delete_children_recursive();
125 return ( root == NULL );
131 return ( root == NULL ? 0 : root->sleft + root->sright + 1 );
139 assert( it != NULL );
148 assert( it != NULL );
167 nn =
new node(key,data);
169 throw std::bad_alloc();
173 nn = create_new_node(key, data, root);
186 assert( compare(new_key, item->key) );
189 rotate_backward(item);
195 assert ( root != NULL );
204 assert ( item != NULL );
205 assert ( root != NULL );
207 bool goto_left = ( item->left != NULL );
208 bool goto_right = ( item->right != NULL );
209 if ( goto_left && goto_right )
211 goto_right = ( compare( item->right->key, item->left->key ) );
212 goto_left = ! goto_right;
216 swap_with_father( item->right );
222 swap_with_father( item->left );
227 for (node* n = item, *f = n->father; f != NULL; n = f, f = n->father)
235 assert( f->right == n );
241 if ( item->father->left == item )
243 assert( item->father->sleft == 0 );
244 item->father->left = NULL;
248 assert( item->father->right == item );
249 assert( item->father->sright == 0 );
250 item->father->right = NULL;
255 assert( item == root );
265 node* create_new_node(
271 assert( subproblem != NULL );
273 if ( subproblem->sleft == 0 )
275 assert( subproblem->left == NULL );
277 node* nn =
new node(key,data);
278 subproblem->left = nn;
279 subproblem->sleft = 1;
280 nn->father = subproblem;
283 if ( subproblem->sright == 0 )
285 assert( subproblem->right == NULL );
287 node* nn =
new node(key,data);
288 subproblem->right = nn;
289 subproblem->sright = 1;
290 nn->father = subproblem;
293 assert( subproblem->left != NULL );
294 assert( subproblem->right != NULL );
296 if ( subproblem->sleft <= subproblem->sright )
298 subproblem->sleft += 1;
299 return create_new_node(key, data, subproblem->left);
302 subproblem->sright += 1;
303 return create_new_node(key, data, subproblem->right);
307 void swap_with_father(
311 int n1_sleft = n1->sleft;
312 int n1_sright = n1->sright;
313 node* n1_left = n1->left;
314 node* n1_right = n1->right;
315 node* n1_father = n1->father;
316 assert( n1_father != NULL );
317 assert( n1_father->left == n1 || n1_father->right == n1 );
319 if ( root == n1_father )
322 if ( n1_father->left == n1 )
324 n1->left = n1_father;
325 n1->right = n1_father->right;
329 assert( n1_father->right == n1 );
331 n1->left = n1_father->left;
332 n1->right = n1_father;
334 n1_father->left = n1_left;
335 n1_father->right = n1_right;
337 n1->sleft = n1_father->sleft;
338 n1->sright = n1_father->sright;
339 n1_father->sleft = n1_sleft;
340 n1_father->sright = n1_sright;
342 n1->father = n1_father->father;
343 n1_father->father = n1;
346 n1->left-> father = n1;
348 n1->right->father = n1;
349 if ( n1_father->left )
350 n1_father->left->father = n1_father;
351 if ( n1_father->right )
352 n1_father->right->father = n1_father;
355 if ( n1->father->left == n1_father )
356 n1->father->left = n1;
357 if ( n1->father->right == n1_father )
358 n1->father->right = n1;
362 void rotate_backward(
366 assert( item != NULL );
370 if ( ! compare( item->father->key, item->key ) )
372 swap_with_father( item );
373 rotate_backward( item );