40 template<
typename Key,
42 typename Compare = less<Key> >
79 void delete_children_recursive()
83 left->delete_children_recursive();
89 right->delete_children_recursive();
131 root->delete_children_recursive();
140 return ( root ==
NULL );
146 return ( root ==
NULL ? 0 : root->sleft + root->sright + 1 );
154 assert( it !=
NULL );
163 assert( it !=
NULL );
182 nn =
new node(key,data);
184 throw std::bad_alloc();
188 nn = create_new_node(key, data, root);
201 assert( compare(new_key, item->key) );
204 rotate_backward(item);
210 assert ( root !=
NULL );
219 assert ( item !=
NULL );
220 assert ( root !=
NULL );
222 bool goto_left = ( item->left !=
NULL );
223 bool goto_right = ( item->right !=
NULL );
224 if ( goto_left && goto_right )
226 goto_right = ( compare( item->right->key, item->left->key ) );
227 goto_left = ! goto_right;
231 swap_with_father( item->right );
237 swap_with_father( item->left );
242 for (node* n = item, *f = n->father; f !=
NULL; n = f, f = n->father)
250 assert( f->right == n );
256 if ( item->father->left == item )
258 assert( item->father->sleft == 0 );
259 item->father->left =
NULL;
263 assert( item->father->right == item );
264 assert( item->father->sright == 0 );
265 item->father->right =
NULL;
270 assert( item == root );
280 node* create_new_node(
286 assert( subproblem !=
NULL );
288 if ( subproblem->sleft == 0 )
290 assert( subproblem->left ==
NULL );
292 node* nn =
new node(key,data);
293 subproblem->left = nn;
294 subproblem->sleft = 1;
295 nn->father = subproblem;
298 if ( subproblem->sright == 0 )
300 assert( subproblem->right ==
NULL );
302 node* nn =
new node(key,data);
303 subproblem->right = nn;
304 subproblem->sright = 1;
305 nn->father = subproblem;
308 assert( subproblem->left !=
NULL );
309 assert( subproblem->right !=
NULL );
311 if ( subproblem->sleft <= subproblem->sright )
313 subproblem->sleft += 1;
314 return create_new_node(key, data, subproblem->left);
317 subproblem->sright += 1;
318 return create_new_node(key, data, subproblem->right);
322 void swap_with_father(
326 int n1_sleft = n1->sleft;
327 int n1_sright = n1->sright;
328 node* n1_left = n1->left;
329 node* n1_right = n1->right;
330 node* n1_father = n1->father;
331 assert( n1_father !=
NULL );
332 assert( n1_father->left == n1 || n1_father->right == n1 );
334 if ( root == n1_father )
337 if ( n1_father->left == n1 )
339 n1->left = n1_father;
340 n1->right = n1_father->right;
344 assert( n1_father->right == n1 );
346 n1->left = n1_father->left;
347 n1->right = n1_father;
349 n1_father->left = n1_left;
350 n1_father->right = n1_right;
352 n1->sleft = n1_father->sleft;
353 n1->sright = n1_father->sright;
354 n1_father->sleft = n1_sleft;
355 n1_father->sright = n1_sright;
357 n1->father = n1_father->father;
358 n1_father->father = n1;
361 n1->left-> father = n1;
363 n1->right->father = n1;
364 if ( n1_father->left )
365 n1_father->left->father = n1_father;
366 if ( n1_father->right )
367 n1_father->right->father = n1_father;
370 if ( n1->father->left == n1_father )
371 n1->father->left = n1;
372 if ( n1->father->right == n1_father )
373 n1->father->right = n1;
377 void rotate_backward(
381 assert( item !=
NULL );
385 if ( ! compare( item->father->key, item->key ) )
387 swap_with_father( item );
388 rotate_backward( item );
void decrease_key(pqueue_item item, const Key &new_key)
const Data & get_data(pqueue_item it) const
pqueue_item insert(const Key &key, const Data &data)
const Key & get_key(pqueue_item it) const