有人能告诉我下面几行是怎么写的吗
else if ( obj < retrieve() )
{
return ( left() == 0 ) ? 0 : left()->erase( obj, left_tree );
}
else
{
return ( right() == 0 ) ? 0 : right()->erase( obj, right_tree );
}
在下面的代码块中:
template <typename Comp>
int Binary_search_node<Comp>::erase( Comp const &obj, Binary_search_node<Comp> *&ptr_to_this)
{
if ( obj == retrieve() ) {
if ( leaf() ) { // leaf node
ptr_to_this= 0;
delete this;
}
else if ( left() != 0 && right() != 0 ) { // full node
element= right()->front();
right()->erase( retrieve(), right_tree );
}
else { // only one child
ptr_to_this= ( left() != 0 ) ? left() : right();
delete this;
}
return 1;
}
else if ( obj < retrieve() ) {
return ( left() == 0 ) ? 0 : left()->erase( obj, left_tree );}
else {
return ( right() == 0 ) ? 0 : right()->erase( obj, right_tree );}
}
额外信息:
(一)
front() -- finds the minimum objects
实施:
template <typename Comp>
Comp Binary_search_node<Comp>::front() const
{
return( left() == 0 ) ?retrieve() :left()->front();
}
2个)
left() -- returns pointer to left subtree
三)
right() -- returns pointer to right subtree
(四)
*ptr_to_this points to current object (same location as what *this points to)
我知道台词的作用,但我不能百分之百确定,所以我想确认一下请注意,此
erase()
函数用于二进制搜索树谢谢! 最佳答案
erase
似乎是递归实现的在每个阶段,我们都会测试要擦除的对象是否等于当前对象,或者我们是否需要进入左或右子对象。
如果我们要进入的儿童不存在(left() == 0
或right() == 0
),那么我们不能擦除对象(因为它不在树中),所以我们返回0
。否则,我们递归到子函数中,并返回它返回的任何内容。
关于c++ - 二进制搜索树中的擦除功能,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6498076/