39 template<
class K,
class Comp>
41 : super(), key(k), balance(0), father(NULL)
44 assert(!super::right);
51 template<
class K,
class Comp>
63 template<
class K,
class Comp>
67 avl_node* node_copy =
new avl_node(key);
69 node_copy->balance = balance;
70 node_copy->father = NULL;
74 node_copy->left = super::left->duplicate(count);
75 node_copy->left->father = node_copy;
78 node_copy->left = NULL;
82 node_copy->right = super::right->duplicate(count);
83 node_copy->right->father = node_copy;
86 node_copy->right = NULL;
96 template<
class K,
class Comp>
109 assert( !super::left );
110 assert( !super::right );
119 template<
class K,
class Comp>
122 unsigned int pl=0, pr=0;
124 if (super::left) pl = super::left->depth();
125 if (super::right) pr = super::right->depth();
138 template<
class K,
class Comp>
143 avl_node* node =
this;
146 if ( avl_base<K, Comp>::s_key_less(key, node->key) )
148 else if ( avl_base<K, Comp>::s_key_less(node->key, key) )
161 template<
class K,
class Comp>
166 const avl_node* node =
this;
169 if ( avl_base<K, Comp>::s_key_less(key, node->key) )
171 else if ( avl_base<K, Comp>::s_key_less(node->key, key) )
185 template<
class K,
class Comp>
190 avl_node* node =
this;
191 avl_node* prev_node = NULL;
194 if ( avl_base<K, Comp>::s_key_less(key, node->key) )
199 else if ( avl_base<K, Comp>::s_key_less(node->key, key) )
211 if ( avl_base<K, Comp>::s_key_less(key, prev_node->key) )
212 return prev_node->next();
226 template<
class K,
class Comp>
231 const avl_node* node =
this;
232 const avl_node* prev_node = NULL;
235 if ( avl_base<K, Comp>::s_key_less(key, node->key) )
240 else if ( avl_base<K, Comp>::s_key_less(node->key, key) )
252 if ( avl_base<K, Comp>::s_key_less(key, prev_node->key) )
253 return prev_node->next();
267 template<
class K,
class Comp>
272 avl_node* node =
this;
273 avl_node* prev_node = NULL;
276 if ( s_key_less(key, node->key) )
281 else if ( s_key_less(node->key, key) )
293 if ( s_key_less(key, prev_node->key) )
296 return prev_node->prev();
308 template<
class K,
class Comp>
313 const avl_node* node =
this;
314 const avl_node* prev_node = NULL;
317 if ( s_key_less(key, node->key) )
322 else if ( s_key_less(node->key, key) )
334 if ( s_key_less(key, prev_node->key) )
337 return prev_node->prev();
347 template<
class K,
class Comp>
351 avl_node* node =
this;
364 template<
class K,
class Comp>
368 const avl_node* node =
this;
381 template<
class K,
class Comp>
385 avl_node* node =
this;
398 template<
class K,
class Comp>
402 const avl_node* node =
this;
415 template<
class K,
class Comp>
419 avl_node* result =
this;
422 if (result->right != NULL)
424 result = result->right;
426 while (result->left != NULL)
427 result = result->left;
432 avl_node* previous_node =
this;
435 while (result->father && !done)
437 if (result->father->left == result)
440 result = result->father;
445 result = previous_node;
455 template<
class K,
class Comp>
459 const avl_node* result =
this;
462 if (result->right != NULL)
464 result = result->right;
466 while (result->left != NULL)
467 result = result->left;
472 const avl_node* previous_node =
this;
475 while (result->father && !done)
477 if (result->father->left == result)
480 result = result->father;
485 result = previous_node;
495 template<
class K,
class Comp>
499 avl_node* result =
this;
502 if (result->left != NULL)
504 result = result->left;
506 while (result->right != NULL)
507 result = result->right;
514 while (result->father && !done)
516 if (result->father->right == result)
519 result = result->father;
530 template<
class K,
class Comp>
534 const avl_node* result =
this;
537 if (result->left != NULL)
539 result = result->left;
541 while (result->right != NULL)
542 result = result->right;
549 while (result->father && !done)
551 if (result->father->right == result)
554 result = result->father;
575 template<
class K,
class Comp>
577 : super(that), key(that.key), balance(that.balance), father(NULL)
594 template<
class K,
class Comp>
596 : m_current(NULL), m_is_final(true)
605 template<
class K,
class Comp>
607 ( avl_node_ptr node,
bool final )
608 : m_current(node), m_is_final(
final)
618 template<
class K,
class Comp>
625 avl_node* p = m_current->next();
627 if ( m_current == p )
639 template<
class K,
class Comp>
653 template<
class K,
class Comp>
660 m_is_final = !m_is_final;
662 m_current = m_current->prev();
664 assert(m_current != NULL);
673 template<
class K,
class Comp>
686 template<
class K,
class Comp>
687 typename claw::avl_base<K,Comp>::avl_iterator::reference
690 return m_current->key;
697 template<
class K,
class Comp>
698 typename claw::avl_base<K,Comp>::avl_iterator::pointer
701 return &m_current->key;
709 template<
class K,
class Comp>
713 return (m_current == it.m_current) && (m_is_final == it.m_is_final);
721 template<
class K,
class Comp>
725 return !( *
this == it );
740 template<
class K,
class Comp>
742 : m_current(NULL), m_is_final(true)
751 template<
class K,
class Comp>
753 ( const_avl_node_ptr node,
bool final )
754 : m_current(node), m_is_final(
final)
764 template<
class K,
class Comp>
771 const_avl_node_ptr p = m_current->next();
773 if ( m_current == p )
785 template<
class K,
class Comp>
799 template<
class K,
class Comp>
806 m_is_final = !m_is_final;
808 m_current = m_current->prev();
810 assert(m_current != NULL);
819 template<
class K,
class Comp>
832 template<
class K,
class Comp>
833 typename claw::avl_base<K,Comp>::avl_const_iterator::reference
836 return m_current->key;
843 template<
class K,
class Comp>
844 typename claw::avl_base<K,Comp>::avl_const_iterator::pointer
847 return &m_current->key;
855 template<
class K,
class Comp>
859 return (m_current == it.m_current) && (m_is_final == it.m_is_final);
867 template<
class K,
class Comp>
868 bool claw::avl_base<K,Comp>::avl_const_iterator::operator!=
871 return !( *
this == it );
882 template<
class K,
class Comp>
890 template<
class K,
class Comp>
892 : m_size(0), m_tree(NULL)
902 template<
class K,
class Comp>
908 m_tree = that.m_tree->duplicate( m_size );
917 template<
class K,
class Comp>
933 template<
class K,
class Comp>
936 assert( validity_check() );
938 if ( m_tree == NULL )
940 m_tree =
new avl_node(key);
946 assert( validity_check() );
957 template<
class K,
class Comp>
958 template<
typename Iterator>
961 for ( ; first!=last; ++first )
971 template<
class K,
class Comp>
974 assert( validity_check() );
977 recursive_delete( m_tree, key );
979 assert( validity_check() );
987 template<
class K,
class Comp>
1005 template<
class K,
class Comp>
1016 template<
class K,
class Comp>
1026 template<
class K,
class Comp>
1032 return lower_bound();
1039 template<
class K,
class Comp>
1046 return lower_bound();
1053 template<
class K,
class Comp>
1059 return iterator(m_tree->upper_bound(),
true);
1066 template<
class K,
class Comp>
1081 template<
class K,
class Comp>
1085 return make_iterator( m_tree->find(key) );
1093 template<
class K,
class Comp>
1097 return make_const_iterator( m_tree->find(key) );
1106 template<
class K,
class Comp>
1110 return make_iterator( m_tree->find_nearest_greater(key) );
1119 template<
class K,
class Comp>
1123 return make_const_iterator( m_tree->find_nearest_greater(key) );
1132 template<
class K,
class Comp>
1136 return make_iterator( m_tree->find_nearest_lower(key) );
1145 template<
class K,
class Comp>
1149 return make_const_iterator( m_tree->find_nearest_lower(key) );
1156 template<
class K,
class Comp>
1159 return make_iterator( m_tree->lower_bound() );
1166 template<
class K,
class Comp>
1170 return make_const_iterator( m_tree->lower_bound() );
1177 template<
class K,
class Comp>
1180 return make_iterator( m_tree->upper_bound() );
1187 template<
class K,
class Comp>
1191 return make_const_iterator( m_tree->upper_bound() );
1199 template<
class K,
class Comp>
1214 m_tree = that.m_tree->duplicate( m_size );
1227 template<
class K,
class Comp>
1230 if (m_size != that.m_size)
1233 return std::equal( begin(), end(), that.
begin(), s_key_less );
1241 template<
class K,
class Comp>
1244 return !( *
this == that );
1252 template<
class K,
class Comp>
1255 return std::lexicographical_compare
1256 ( begin(), end(), that.
begin(), that.
end(), s_key_less );
1264 template<
class K,
class Comp>
1267 return that < *
this;
1275 template<
class K,
class Comp>
1278 return !( that < *this );
1286 template<
class K,
class Comp>
1289 return !( *
this < that );
1297 template<
class K,
class Comp>
1300 std::swap(m_size, that.m_size);
1301 std::swap(m_tree, that.m_tree);
1318 template<
class K,
class Comp>
1320 (
const avl_node_ptr node,
const K& min,
const K& max )
const
1324 else if ( !( s_key_less(node->key, min) || s_key_less(min, node->key) ) )
1325 return (node->left == NULL)
1326 && check_in_bounds( node->right, node->key, max );
1327 else if ( !( s_key_less(node->key, max) || s_key_less(max, node->key) ) )
1328 return (node->right == NULL)
1329 && check_in_bounds( node->left, min, node->key );
1331 return s_key_less(node->key, max) && s_key_less(min, node->key)
1332 && check_in_bounds( node->left, min, node->key )
1333 && check_in_bounds( node->right, node->key, max );
1345 template<
class K,
class Comp>
1354 if (node->left) pl = node->left->depth();
1355 if (node->right) pr = node->right->depth();
1357 return (pl-pr>=-1) && (pl-pr<=1) && (pl-pr == node->balance)
1358 && check_balance(node->left) && check_balance(node->right);
1369 template<
class K,
class Comp>
1376 if (node->father != NULL)
1378 valid = (node->father->left == node) ^ (node->father->right == node);
1379 valid = valid && correct_descendant( node->left )
1380 && correct_descendant( node->right );
1396 template<
class K,
class Comp>
1403 avl_node *node_min, *node_max;
1406 for (node_min = m_tree; node_min->left!=NULL; node_min = node_min->left);
1407 for (node_max = m_tree; node_max->right!=NULL;
1408 node_max = node_max->right);
1410 valid = check_in_bounds(m_tree->left, node_min->key, m_tree->key);
1412 && check_in_bounds(m_tree->right, m_tree->key, node_max->key);
1414 valid = valid && (m_tree->father == NULL)
1415 && correct_descendant( m_tree->left )
1416 && correct_descendant( m_tree->right );
1420 return valid && check_balance(m_tree);
1432 template<
class K,
class Comp>
1437 return iterator(node,
false);
1447 template<
class K,
class Comp>
1452 return const_iterator(node,
false);
1471 template<
class K,
class Comp>
1475 char old_node_balance;
1476 char old_subtree_balance;
1478 assert( node != NULL );
1479 assert( node->left != NULL );
1480 assert( (1 <= node->balance) && (node->balance <= 2) ) ;
1481 assert( (-1 <= node->left->balance) && (node->left->balance <= 2) );
1482 assert( (node->left->balance != 2) || (node->balance == 2) );
1484 old_node_balance = node->balance;
1485 old_subtree_balance = node->left->balance;
1489 p->father = node->father;
1491 node->left = p->right;
1494 p->right->father = node;
1502 switch(old_subtree_balance)
1506 node->right->balance = old_node_balance - 1;
1510 node->right->balance = old_node_balance - 1;
1513 node->balance = old_node_balance - 2;
1514 node->right->balance = old_node_balance - 2;
1519 node->right->balance = - 1;
1532 template<
class K,
class Comp>
1536 char old_node_balance;
1537 char old_subtree_balance;
1539 assert( node != NULL );
1540 assert( node->right != NULL );
1541 assert( (-2 <= node->balance) && (node->balance <= -1) ) ;
1542 assert( (-2 <= node->right->balance) && (node->right->balance <= 1) );
1543 assert( (node->right->balance != -2) || (node->balance == -2) );
1545 old_node_balance = node->balance;
1546 old_subtree_balance = node->right->balance;
1550 p->father = node->father;
1552 node->right = p->left;
1555 p->left->father = node;
1563 switch(old_subtree_balance)
1568 node->left->balance = 1;
1571 node->balance = old_node_balance + 2;
1572 node->left->balance = old_node_balance + 2;
1576 node->left->balance = old_node_balance + 1;
1580 node->left->balance = old_node_balance + 1;
1590 template<
class K,
class Comp>
1593 assert( node != NULL );
1595 rotate_left( node->left );
1596 rotate_right( node );
1604 template<
class K,
class Comp>
1607 assert( node != NULL );
1609 rotate_right( node->right );
1610 rotate_left( node );
1622 template<
class K,
class Comp>
1625 assert(node != NULL);
1629 if ( s_key_less(key, node->key) )
1634 else if ( s_key_less(node->key, key) )
1650 template<
class K,
class Comp>
1653 assert(node != NULL);
1655 if ( node->balance == 2)
1656 adjust_balance_left(node);
1657 else if ( node->balance == -2)
1658 adjust_balance_right(node);
1669 template<
class K,
class Comp>
1672 assert(node != NULL);
1673 assert(node->balance == 2);
1675 if (node->left->balance > -1)
1676 rotate_right( node );
1677 else if ( node->left->balance == -1)
1678 rotate_left_right(node);
1689 template<
class K,
class Comp>
1692 assert(node != NULL);
1693 assert(node->balance == -2);
1695 if (node->right->balance < 1)
1696 rotate_left( node );
1697 else if ( node->right->balance == 1)
1698 rotate_right_left(node);
1714 template<
class K,
class Comp>
1717 avl_node_ptr* new_node;
1718 avl_node_ptr node_father;
1719 avl_node_ptr last_imbalanced;
1720 avl_node_ptr last_imbalanced_father;
1722 assert(m_tree != NULL);
1724 new_node = find_node_reference(key, last_imbalanced, node_father );
1726 if (*new_node == NULL)
1728 *new_node =
new avl_node(key);
1729 (*new_node)->father = node_father;
1732 last_imbalanced_father = last_imbalanced->father;
1735 update_balance( last_imbalanced, key );
1737 adjust_balance( last_imbalanced );
1740 if ( last_imbalanced_father == NULL )
1742 m_tree = last_imbalanced;
1743 m_tree->father = NULL;
1745 else if (s_key_less(last_imbalanced->key, last_imbalanced_father->key))
1747 last_imbalanced_father->left = last_imbalanced;
1749 last_imbalanced_father->right = last_imbalanced;
1766 template<
class K,
class Comp>
1767 typename claw::avl_base<K,Comp>::avl_node_ptr*
1769 (
const K& key, avl_node_ptr& last_imbalanced, avl_node_ptr& node_father)
1772 bool exists =
false;
1774 last_imbalanced = m_tree;
1778 while ( ((*node) != NULL) && !exists )
1780 if ( (*node)->balance != 0 )
1781 last_imbalanced = *node;
1785 if ( s_key_less(key, (*node)->key) )
1787 node_father = *node;
1788 node = & (*node)->left;
1790 else if ( s_key_less((*node)->key, key) )
1792 node_father = *node;
1793 node = & (*node)->right;
1817 template<
class K,
class Comp>
1821 bool result =
false;
1826 if ( s_key_less(key, node->key) )
1828 if ( recursive_delete( node->left, key ) )
1829 result = new_balance( node, -1 );
1832 else if ( s_key_less(node->key, key) )
1834 if ( recursive_delete( node->right, key ) )
1835 result = new_balance( node, 1 );
1840 result = recursive_delete_node( node );
1858 template<
class K,
class Comp>
1861 assert( (imbalance==1) || (imbalance==-1) );
1862 assert( node != NULL );
1864 node->balance += imbalance;
1866 switch(node->balance)
1870 case 0:
return true;
1877 case 2: adjust_balance_left(node);
return node->balance == 0;
1879 case -2: adjust_balance_right(node);
return node->balance == 0;
1880 default :
return false;
1893 template<
class K,
class Comp>
1896 assert( node != NULL );
1898 if ( node->left == NULL)
1901 avl_node_ptr right_subtree = node->right;
1904 right_subtree->father = node->father;
1911 node = right_subtree;
1916 if ( recursive_delete_max( node->left, node ) )
1922 if ( node->balance == -2 )
1925 adjust_balance_right(node);
1926 return node->balance == 0;
1928 else if ( node->balance == 0 )
1961 template<
class K,
class Comp>
1963 ( avl_node_ptr& root, avl_node_ptr node )
1968 if ( root->right == NULL )
1972 avl_node_ptr left_subtree;
1974 node->key = root->key;
1975 left_subtree = root->left;
1978 left_subtree->father = root->father;
1984 root = left_subtree;
1990 if ( recursive_delete_max( root->right, node ) )
1996 if (root->balance == 2)
1999 adjust_balance_left( root );
2000 return root->balance == 0;
2007 return root->balance == 0;