SGI STL中关于rbtree的stl_tree.h中的_M_decrement函数实现

时间:2015-12-20 09:32:20

标签: c++ stl tree

在_M_increment函数结束时,

if (_M_node->_M_right != __y)
      _M_node = __y;

处理上一个节点是最大值的情况,并将头节点(__y)分配给_M_node。

void _M_increment()
{
  if (_M_node->_M_right != 0)
  {
    _M_node = _M_node->_M_right;
    while (_M_node->_M_left != 0)
      _M_node = _M_node->_M_left;
  }
  else
  {
    _Base_ptr __y = _M_node->_M_parent;
    while (_M_node == __y->_M_right)
    {
      _M_node = __y;
      __y = __y->_M_parent;
    }
    if (_M_node->_M_right != __y)
      _M_node = __y;
  }
}

但是在_M_decrement函数中,没有类似的代码来处理前一个是最小值的类似情况。

void _M_decrement()
{
  if (_M_node->_M_color == _S_rb_tree_red &&
    _M_node->_M_parent->_M_parent == _M_node)
  _M_node = _M_node->_M_right;
  else if (_M_node->_M_left != 0)
  {
    _Base_ptr __y = _M_node->_M_left;
    while (__y->_M_right != 0)
      __y = __y->_M_right;
    _M_node = __y;
  }
  else {
    _Base_ptr __y = _M_node->_M_parent;
    while (_M_node == __y->_M_left)
    {
      _M_node = __y;
      __y = __y->_M_parent;
    }
    _M_node = __y;
  }
}

减量函数没有类似的代码判断边界条件是否合法? 任何建议都表示赞赏。

0 个答案:

没有答案