将BST(无递归)转换为AVL时的旋转问题 这个周末我正在研究一个大学项目(数据结构),我必须用C++编写一个AVL树。我认为首先编写BST代码,然后将其转换为AVL并不困难。也许我错了。。。我有两个类,class node和class AVLTree,它们是class node的朋友。我设法根据BST的规则进行插入和删除(我检查了),并且设法找到了树节点的平衡因子(这也起作用)。然而,当我尝试简单的左旋转时,一切都出了问题!以下是我的代码(首先是.h文件):

将BST(无递归)转换为AVL时的旋转问题 这个周末我正在研究一个大学项目(数据结构),我必须用C++编写一个AVL树。我认为首先编写BST代码,然后将其转换为AVL并不困难。也许我错了。。。我有两个类,class node和class AVLTree,它们是class node的朋友。我设法根据BST的规则进行插入和删除(我检查了),并且设法找到了树节点的平衡因子(这也起作用)。然而,当我尝试简单的左旋转时,一切都出了问题!以下是我的代码(首先是.h文件):,c++,binary-search-tree,avl-tree,C++,Binary Search Tree,Avl Tree,}) 现在是AVLTree类的.cpp文件(仅限某些方法) 以及平衡方法: void AVLTree::balanceTree(node *current,node *previous,node *next) { if(current->balance>1) //if the tree is right heavy if(next->balance>0) //if the tree's right subtree is right heavy sl

})

现在是AVLTree类的.cpp文件(仅限某些方法)

以及平衡方法:

void AVLTree::balanceTree(node *current,node *previous,node *next)
{
if(current->balance>1) //if the tree is right heavy
    if(next->balance>0) //if the tree's right subtree is right heavy
        slRotation(current,previous,next); //perform Simple Left Rotation
    else //if the tree's right subtree is left heavy
        dlRotation(current,previous,next); //perform Double Left Rotation
else //if the tree is left heavy
    if(next->balance<0) //if the tree's left subtree is left heavy
        srRotation(current,previous,next); //perform Simple Right Roation
    else //if the tree's left subtree is right heavy
        drRotation(current,previous,next); //perform Double Right Rotation
updateTreeHeights(root);
}

抱歉发了这么长的帖子!这是我有生以来第一次使用AVL树,更不用说编程了!希望你能帮忙

问题解决了!我搞砸了左右指针的双向旋转

bool AVLTree::insertNode(int aData)
{
node *current,*next,*ptr;
bool isLeftChild;
int nodeCounter=0;
current=next=root;
ptr=newNode(aData);
if(ptr==NULL) //Couldn't allocate memory
{
    return false;
}
if(current==NULL) //Inserting the first node in our tree (root==NULL)
{
    root=ptr;
    return true; //Successful insertion of root
}
do
{
    if(aData<current->data) //If the node we want to insert has data smaller than the current node's data, then repeat the procedure for the left child of the current node
    {
        next=current->leftChild;
        isLeftChild=true;
        nodeCounter++;
    }
    else if(aData>current->data) //If the node we want to insert has data bigger than the current node's data, then repeat the procedure for the right child of the current node
    {
        next=current->rightChild;
        isLeftChild=false;
        nodeCounter++;
    }
    if(next==NULL)
    {
        if(isLeftChild)
        {
            current->leftChild=ptr;
        }
        else
        {
            current->rightChild=ptr;
        }
        updateHeightsInserting(nodeCounter,aData);
        return true;
    }
    current=next; //Repeat the procedure for the next node
}while(next!=NULL); //Repeat the procedure until there's no next node, meaning we enter the if(next==NULL) statement
}
void AVLTree::updateHeightsInserting(int nodeCounter,int aData)
{
node *current,*next,*previous;
current=next=previous=root;
do
{
    if(aData<current->data)
    {
        if(current->heightL<nodeCounter)
        {
            current->heightL=nodeCounter;
        }
        next=current->leftChild;
        nodeCounter--;
    }
    else if(aData>current->data)
    {
        if(current->heightR<nodeCounter)
        {
            current->heightR=nodeCounter;
        }
        next=current->rightChild;
        nodeCounter--;
    }
    current->balance=current->heightR-current->heightL;
    if(abs(current->balance)>1)
    {
        if(abs(next->heightR-next->heightL)<1) //We use <1, because the hight of the next node hasn't been increased yet-If the next node isn't problematic it means the current node is
            balanceTree(current,previous,next);
    }
    previous=current;
    current=next;
}while(next->data!=aData);
}
void AVLTree::slRotation(node *current,node *previous,node *next)
{
if(current==root) //previous=current
{
   node *temp;
   root=next; //next=current->rightChild
   temp=next->leftChild;
   next->leftChild=current;
   current->rightChild=temp;
}
else
    previous->rightChild=next;
    current->rightChild=NULL;
    next->leftChild=current;
} 
void AVLTree::balanceTree(node *current,node *previous,node *next)
{
if(current->balance>1) //if the tree is right heavy
    if(next->balance>0) //if the tree's right subtree is right heavy
        slRotation(current,previous,next); //perform Simple Left Rotation
    else //if the tree's right subtree is left heavy
        dlRotation(current,previous,next); //perform Double Left Rotation
else //if the tree is left heavy
    if(next->balance<0) //if the tree's left subtree is left heavy
        srRotation(current,previous,next); //perform Simple Right Roation
    else //if the tree's left subtree is right heavy
        drRotation(current,previous,next); //perform Double Right Rotation
updateTreeHeights(root);
}
void AVLTree::updateTreeHeights(node *ptr)  //Visits the nodes by level recursively      (post-order traversal), so that it can calculate the balance of each node
{
if(ptr==NULL)
    return;
updateTreeHeights(ptr->leftChild);
updateTreeHeights(ptr->rightChild);
if(ptr->leftChild==NULL && ptr->rightChild==NULL)
{
    ptr->heightL=ptr->heightR=0;
}
else if(ptr->leftChild==NULL)
{
    ptr->heightR=max(ptr->rightChild->heightL,ptr->rightChild->heightR)+1;
    ptr->heightL=0;
}
else if(ptr->rightChild==NULL)
{
    ptr->heightL=max(ptr->leftChild->heightL,ptr->leftChild->heightR)+1;
    ptr->heightR=0;
}
else
{
    ptr->heightL=max(ptr->leftChild->heightL,ptr->leftChild->heightR)+1;
    ptr->heightR=max(ptr->rightChild->heightL,ptr->rightChild->heightR)+1;
}
ptr->balance=ptr->heightR-ptr->heightL;
}