我的删除功能无法正常工作。当它删除节点时,它不会重新平衡整个树。有时它会平衡左子树,但省略整个右子树。代码很长
class AVLTree
{
public:
void insert(const DataType &item);
void graph(ostream &out) const;
void remove(DataType &value);
private:
class AVLNode
{
public:
DataType data;
int balanceFactor;
AVLNode *left;
AVLNode *right;
AVLNode():balanceFactor(0),left(NULL),right(NULL){}
AVLNode(DataType item):balanceFactor(0), data(item),left(NULL), right(NULL){}
};
typedef AVLNode* AVLNodePointer;
AVLNodePointer myRoot;
int height(AVLTree<DataType>::AVLNodePointer temp);
int different(AVLTree<DataType>::AVLNodePointer temp);
AVLNodePointer ll_rotation(AVLTree<DataType>::AVLNodePointer parent);
AVLNodePointer rr_rotation(AVLTree<DataType>::AVLNodePointer parent);
AVLNodePointer lr_rotation(AVLTree<DataType>::AVLNodePointer parent);
AVLNodePointer rl_rotation(AVLTree<DataType>::AVLNodePointer parent);
AVLNodePointer balance_tree(AVLTree<DataType>::AVLNodePointer temp);
AVLNodePointer insertAux(AVLTree<DataType>::AVLNodePointer &subtreeRoot,const DataType &value);
AVLNodePointer findmin(AVLTree<DataType>::AVLNodePointer temp);
AVLNodePointer deleteAux(AVLTree<DataType>::AVLNodePointer temp,DataType &value);
void graphAux(ostream &out,int indent, AVLTree<DataType>::AVLNodePointer subtreePtr) const;
};
template<typename DataType>
int AVLTree<DataType>::height(AVLTree<DataType>::AVLNodePointer temp)
{
int h=0;
if(temp!=NULL)
{
int left_height=height(temp->left);
int right_height=height(temp->right)
int max_height =max(left_height,right_height);
h=max_height +1;
}
return h;
}
template<typename DataType>
int AVLTree<DataType>::different(AVLTree<DataType>::AVLNodePointer temp)
{
int left_height=height(temp->left);
int right_height=height(temp->right);
temp->balanceFactor=left_height-right_height;
return temp->balanceFactor;
}
template<typename DataType>
typename AVLTree<DataType>::AVLNodePointer AVLTree<DataType>::ll_rotation(AVLTree<DataType>::AVLNodePointer parent)
{
AVLTree<DataType>::AVLNodePointer temp;
temp=parent->left;
parent->left=temp->right;
temp->right=parent;
return temp;
}
template<typename DataType>
typename AVLTree<DataType>::AVLNodePointer AVLTree<DataType>::rr_rotation(AVLTree<DataType>::AVLNodePointer parent)
{
AVLTree<DataType>::AVLNodePointer temp;
temp=parent->right;
parent->right=temp->left;
temp->left=parent;
return temp;
}
template<typename DataType>
typename AVLTree<DataType>::AVLNodePointer AVLTree<DataType>::lr_rotation(AVLTree<DataType>::AVLNodePointer parent)
{
AVLTree<DataType>::AVLNodePointer temp;
temp=parent->left;
parent->left=rr_rotation(temp);
return ll_rotation(parent);
}
template<typename DataType>
typename AVLTree<DataType>::AVLNodePointer AVLTree<DataType>::rl_rotation(AVLTree<DataType>::AVLNodePointer parent)
{
AVLTree<DataType>::AVLNodePointer temp;
temp=parent->right;
parent->right=ll_rotation(temp);
return rr_rotation(parent);
}
template<typename DataType>
typename AVLTree<DataType>::AVLNodePointer AVLTree<DataType>::balance_tree(AVLTree<DataType>::AVLNodePointer temp)
{
temp->balanceFactor=different(temp);
if(temp->balanceFactor>1)
{
if(different(temp->left)>0)
{
temp=ll_rotation(temp);
}
else
{
temp=lr_rotation(temp);
}
}
else if(temp->balanceFactor<-1)
{
if(different(temp->right)>0)
{
temp=rl_rotation(temp);
}
else
{
temp=rr_rotation(temp);
}
}
return temp; //no balance needed
}
template<typename DataType>
typename AVLTree<DataType>::AVLNodePointer AVLTree<DataType>::findmin(AVLTree<DataType>::AVLNodePointer temp)
{
if(temp->left==NULL)
{
return temp;
}
else
{
findmin(temp->left);
}
}
template<typename DataType>
void AVLTree<DataType>::remove(DataType &value)
{
deleteAux(myRoot,value);
}
template<typename DataType>
typename AVLTree<DataType>::AVLNodePointer AVLTree<DataType>::deleteAux(AVLTree<DataType>::AVLNodePointer temp,DataType &value)
{
if(temp==NULL)
{
return temp;
}
if (value<temp->data)
{
temp->left=deleteAux(temp->left,value);
temp=balance_tree(temp);
}
else if(value>temp->data)
{
temp->right=deleteAux(temp->right,value);
temp= balance_tree(temp);
}
else
{
if(temp->left==NULL && temp->right==NULL)
{
AVLTree<DataType>::AVLNodePointer temp1;
temp1=temp;
delete temp1;
temp=NULL;
}
else if(temp->left==NULL)
{
AVLTree<DataType>::AVLNodePointer temp1=temp;
temp=temp->right;
delete temp1;
temp1=NULL;
}
else if(temp->right==NULL)
{
AVLTree<DataType>::AVLNodePointer temp1=temp;
temp=temp->left;
delete temp1;
temp1=NULL;
}
else
{
AVLTree<DataType>::AVLNodePointer temp1=findmin(temp->right);
temp->data=temp1->data;
temp->right=deleteAux(temp->right, temp1->data);
}
}
return temp;
}
只需编辑忘记添加删除功能
答案 0 :(得分:0)
您应该尝试使用调试器调试代码以查看正在发生的事情。
跳出来的一件事是你在第一次调用时没有使用deleteAux
的返回值,所以如果deleteAux改变了root应该是什么,你就不会拥有正确的root权限值。尝试将remove
功能更改为:
myRoot = deleteAux(myRoot,value);