C++ AVL树旋转问题

C++ AVL树旋转问题,c++,rotation,avl-tree,C++,Rotation,Avl Tree,我一直在为插入操作制作AVL树。我的插件工作正常,但我尝试过的每一次旋转都不起作用。我一直在尝试不同的平衡位置,也只是在每次插入后尝试旋转,只是为了检查它是否能工作,但我根本没有得到一个旋转。任何关于我的旋转不起作用的帮助都将不胜感激 标题: #ifndef LINKEDBINARYTREE_H #define LINKEDBINARYTREE_H #include <iostream> #include <string> using namespace std; cl

我一直在为插入操作制作AVL树。我的插件工作正常,但我尝试过的每一次旋转都不起作用。我一直在尝试不同的平衡位置,也只是在每次插入后尝试旋转,只是为了检查它是否能工作,但我根本没有得到一个旋转。任何关于我的旋转不起作用的帮助都将不胜感激

标题:

#ifndef LINKEDBINARYTREE_H
#define LINKEDBINARYTREE_H
#include <iostream>
#include <string>
using namespace std;

class LinkedBinaryTree {
private:
    struct Node {
        string word;
        Node* left;
        Node* right;
        Node* parent;
        int wordCount;
        int height;
        Node() : word(), left(NULL), right(NULL), parent(NULL), wordCount(1), height(1) {}
        Node(string s, Node* l, Node* r, Node* p) {
            word = s;
            left = NULL;
            right = NULL;
            parent = p;
            wordCount = 1;
            height = 1;
        }
    };
    Node* _root;

public:
    LinkedBinaryTree();
    ~LinkedBinaryTree();
    void insertNode(Node* node, string word);
    void insert(string word);
    void display(Node* ptr, int level);
    Node* root();
    void inOrder(Node* node);
    void rightRotate(Node* node);
    void leftRotate(Node* node);
    void rightLeftRotate(Node* node);
    void leftRightRotate(Node* node);
    int avlNum(Node* node);
    int height(Node* node);
    int bfactor(Node* node);
    void fixHeight(Node* node);
    void balance(Node* node);

    int n;
};
#endif 
#如果链接到BinaryTree\u H
#定义LINKEDBINARYTREE_H
#包括
#包括
使用名称空间std;
类LinkedBinaryTree{
私人:
结构节点{
字符串字;
节点*左;
节点*右;
节点*父节点;
int字数;
内部高度;
Node():word()、左(NULL)、右(NULL)、父(NULL)、字数(1)、高度(1){}
节点(字符串s、节点*l、节点*r、节点*p){
word=s;
左=空;
右=空;
父母=p;
字数=1;
高度=1;
}
};
节点*_根;
公众:
LinkedBinaryTree();
~LinkedBinaryTree();
void insertNode(节点*节点,字符串字);
空白插入(字符串字);
无效显示(节点*ptr,整数级);
Node*root();
按顺序作废(节点*节点);
void rightRotate(节点*节点);
void leftRotate(节点*节点);
void right leftrotate(节点*节点);
void leftRightRotate(节点*节点);
int avlNum(节点*节点);
整数高度(节点*节点);
int bfactor(Node*Node);
空隙固定高度(节点*节点);
空余额(节点*节点);
int n;
};
#恩迪夫
.cpp:

#包括“LinkedBinaryTree.h”
#包括
void LinkedBinaryTree::索引(节点*节点)
{
if(node==NULL)
返回;
顺序(节点->左);
cout单词left=temp->right;
temp->right=节点;
临时->父节点->父节点;
节点=温度;
如果(临时->父级=NULL){
_根=温度;
}
固定高度(节点);
固定高度(节点->右侧);
固定高度(节点->左侧);
}
void LinkedBinaryTree::leftRotate(节点*节点)
{
节点*温度;
temp=节点->右侧;
节点->右=临时->左;
temp->left=节点;
临时->父节点->父节点;
节点=温度;
如果(临时->父级=NULL){
_根=温度;
}
固定高度(节点);
固定高度(节点->右侧);
固定高度(节点->左侧);
}
void LinkedBinaryTree::rightLeftRotate(节点*节点)
{
右旋转(节点->左);
左旋转(节点);
}
void LinkedBinaryTree::leftRightRotate(节点*节点)
{
左旋转(节点->右);
右旋转(节点);
}
int LinkedBinaryTree::高度(节点*节点)
{
int h=0;
如果(节点!=NULL){
h=节点->高度;
}
返回h;
}
int LinkedBinaryTree::bfactor(节点*节点)
{
返回高度(节点->右)-高度(节点->左);
}
void LinkedBinaryTree::固定高度(节点*节点)
{
int hl=高度(节点->左侧);
int hr=高度(节点->右侧);
节点->高度=(hl>hr?hl:hr)+1;
}
int LinkedBinaryTree::avlNum(节点*节点)
{
int leftH=高度(节点->左侧);
int rightH=高度(节点->右侧);
int avlNum=rightH-leftH;
返回平均值;
}
LinkedBinaryTree::LinkedBinaryTree()
{
_root=NULL;
}
LinkedBinaryTree::~LinkedBinaryTree()
{
}
void LinkedBinaryTree::insertNode(节点*节点,字符串字)
{
if(wordword){
如果(节点->左!=NULL)
insertNode(节点->左侧,word);
否则{
节点->左=新节点(字,空,空,节点);
}   
}
else if(word>节点->word)
{
如果(节点->右侧!=NULL)
insertNode(节点->右侧,word);
否则{
node->right=新节点(word,NULL,NULL,node);
}
}
else if(word==节点->word){
node->wordCount++;
}
平衡(节点);
}
void LinkedBinaryTree::insert(字符串字){
如果(_root==NULL){
_root=新节点(word,NULL,NULL,NULL);
n++;
}
否则{
插入节点(_根,字);
n++;
}
}
void LinkedBinaryTree::display(节点*ptr,int级别)
{
int i;
如果(ptr!=NULL)
{
显示(ptr->右侧,级别+1);
printf(“\n”);
如果(ptr==\u根)
cout(右侧)<0)
右旋转(节点->右);
其他的
左旋转(节点);
}
if(bfactor(node)==2){
如果(b因素(节点->左)>0)
左旋转(节点->左);
其他的
右旋转(节点);
}
}
主要内容:

#包括“LinkedBinaryTree.h”
#包括
int main(){
连接二叉树t;
t、 插入(“是”);
t、 插入(“否”);
t、 插入(“可能”);
t、 插入(“希望”);
t、 插入(“绝对”);
t、 显示(t.root(),1);

cout您的代码中有许多错误,但是阻止旋转的是第二个
b因素
检入
平衡
不正确。它应该与
-2
比较

if (bfactor(node) == -2)

另一个问题是
if
语句在
Rotate
函数中的赋值(增加编译器警告级别会通知您这些).

您的代码中有许多错误,但是阻止旋转的是第二个
b因素
检入
平衡
不正确。它应该与
-2
比较

if (bfactor(node) == -2)

另一个问题是
if
语句在
Rotate
函数中的赋值(增加编译器警告级别会通知您这些)。

啊,谢谢,我错过了-2。我在代码中做了这些更改,但仍然没有解决问题。“是的”始终是根,无论发生什么我都不理解,因为即使没有其他东西在旋转,一旦启动旋转,它也应该更改树的根。@NeerP84是否已将
if(temp->parent=NULL)
修复为
if(temp->parent==NULL)
?修复了它,谢谢。我在第四次I之后仍在丢失节点
if (bfactor(node) == -2)