doc_avltree_algorithms.cpp 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2007-2013
  4. //
  5. // Distributed under the Boost Software License, Version 1.0.
  6. // (See accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. //
  9. // See http://www.boost.org/libs/intrusive for documentation.
  10. //
  11. /////////////////////////////////////////////////////////////////////////////
  12. //[doc_avltree_algorithms_code
  13. #include <boost/intrusive/avltree_algorithms.hpp>
  14. #include <cassert>
  15. struct my_node
  16. {
  17. my_node(int i = 0)
  18. : int_(i)
  19. {}
  20. my_node *parent_, *left_, *right_;
  21. int balance_;
  22. //other members
  23. int int_;
  24. };
  25. //Define our own avltree_node_traits
  26. struct my_avltree_node_traits
  27. {
  28. typedef my_node node;
  29. typedef my_node * node_ptr;
  30. typedef const my_node * const_node_ptr;
  31. typedef int balance;
  32. static node_ptr get_parent(const_node_ptr n) { return n->parent_; }
  33. static void set_parent(node_ptr n, node_ptr parent){ n->parent_ = parent; }
  34. static node_ptr get_left(const_node_ptr n) { return n->left_; }
  35. static void set_left(node_ptr n, node_ptr left) { n->left_ = left; }
  36. static node_ptr get_right(const_node_ptr n) { return n->right_; }
  37. static void set_right(node_ptr n, node_ptr right) { n->right_ = right; }
  38. static balance get_balance(const_node_ptr n) { return n->balance_; }
  39. static void set_balance(node_ptr n, balance b) { n->balance_ = b; }
  40. static balance negative() { return -1; }
  41. static balance zero() { return 0; }
  42. static balance positive() { return 1; }
  43. };
  44. struct node_ptr_compare
  45. {
  46. bool operator()(const my_node *a, const my_node *b)
  47. { return a->int_ < b->int_; }
  48. };
  49. int main()
  50. {
  51. typedef boost::intrusive::avltree_algorithms<my_avltree_node_traits> algo;
  52. my_node header, two(2), three(3);
  53. //Create an empty avltree container:
  54. //"header" will be the header node of the tree
  55. algo::init_header(&header);
  56. //Now insert node "two" in the tree using the sorting functor
  57. algo::insert_equal_upper_bound(&header, &two, node_ptr_compare());
  58. //Now insert node "three" in the tree using the sorting functor
  59. algo::insert_equal_lower_bound(&header, &three, node_ptr_compare());
  60. //Now take the first node (the left node of the header)
  61. my_node *n = header.left_;
  62. assert(n == &two);
  63. //Now go to the next node
  64. n = algo::next_node(n);
  65. assert(n == &three);
  66. //Erase a node just using a pointer to it
  67. algo::unlink(&two);
  68. //Erase a node using also the header (faster)
  69. algo::erase(&header, &three);
  70. return 0;
  71. }
  72. //]