AVL_tree的删除

template <class Record>
Error_code AVL_tree<Record>::avl_remove(Binary_node<Record> *&sub_root, Record &new_data, bool &shorter)
{
Error_code result=success;
Record sub_record;
if(sub_root==NULL)
{
shorter=false;
return not_present;
}
else if(new_data==sub_root->data)
{
Binary_node<Record>*to_delete=sub_root;
if(sub_root->right==NULL)
{
sub_root=sub_root->left;
shorter=true;
delete to_delete;
return success;
}
else if(sub_root->left==NULL)
{
sub_root=sub_root->right;
shorter=true;
delete to_delete;
return success;
}
else
{
to_delete=sub_root->left;
Binary_node<Record>*parent=sub_root;
while(to_delete->right!=NULL)
{
parent=to_delete;
to_delete=to_delete->right;
}
new_data=to_delete->data;
sub_record=new_data;
}
}


if(new_data<sub_root->data)
{
result=avl_remove(sub_root->left,new_data,shorter);
if(shorter=true)
switch(sub_root->get_balance())
{
case left_higher:
sub_root->set_balance(equal_height);
break;
case equal_height:
sub_root->set_balance(right_higher);
break;
case right_higher:
if(sub_record.the_key()!=0)
sub_root->data=sub_record;
shorter=right_balance2(sub_root);
break;
}
}
if (new_data > sub_root->data) 

result = avl_remove(sub_root->right, new_data, shorter);
if (shorter == true)
switch (sub_root->get_balance( ))

case left_higher:
if(sub_record.the_key()!=0)sub_root->data = sub_record; 
shorter = left_balance2(sub_root);
break;
case equal_height:
sub_root->set_balance(left_higher);
shorter = false;
break;
case right_higher:
sub_root->set_balance(equal_height);
break;
}  

return result;






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值