C++ 使用非递归插入方法实现AVL树

C++ 使用非递归插入方法实现AVL树,c++,algorithm,data-structures,C++,Algorithm,Data Structures,我正在学习实现AVL树,我被卡住了。。。尝试以非递归方式实现插入函数。已经三天了,我迷路了。这是我的密码。。。希望有人能帮我:) #包括 使用名称空间std; 类节点{//WORKS 公众: 节点*左、*右、*父节点; 内部元素,高度; 节点(Node*l,Node*r,Node*p,intx,inth):左(l),右(r),父(p),元素(x),高度(h){} ~Node(){}; }; ostream&operator(我有点害怕想到非递归AVLdelete())是否有任何关于您正在遵循的算

我正在学习实现AVL树,我被卡住了。。。尝试以非递归方式实现插入函数。已经三天了,我迷路了。这是我的密码。。。希望有人能帮我:)

#包括
使用名称空间std;
类节点{//WORKS
公众:
节点*左、*右、*父节点;
内部元素,高度;
节点(Node*l,Node*r,Node*p,intx,inth):左(l),右(r),父(p),元素(x),高度(h){}
~Node(){};
};

ostream&operator(我有点害怕想到非递归AVL
delete()
)是否有任何关于您正在遵循的算法的描述,您可以引用或提供超链接?我可以。我正试图实现我的教授提出的AVL,其思想是在二叉搜索树中插入节点,然后寻找平衡点(使用我的教授提出的伪代码的旋转,我认为我实现了这些旋转),并更新高度,从而得到AVL树结构。我尽了最大的努力。。。我认为我的问题是用这些右/左旋转来确定我应该旋转的位置和内容rotations@greybeard你能帮我吗?你既没有引用你试图实现的插入和平衡*/*旋转算法的描述,也没有提供超链接:请这样做。所示的
struct
class
,包含所有
public
)不具有值为-、0、+的余额。它的特点是一个
父级
和一个
高度
——过去,这被认为是一种不可容忍的空间浪费。您有一个
节点*to_rotate()
,其中至少有一条路径(显式)不返回值-您的语言系统是否会抱怨?为什么在
righrotate()
leftRotate()
中有单独的开放编码旋转?
#include <iostream>
using namespace std;

class Node{ // WORKS
    public:
        Node *left, *right, *parent;
        int elem, height;

        Node(Node* l, Node* r, Node* p, int x, int h): left(l), right(r), parent(p), elem(x), height(h){}
        ~Node(){};
};

ostream&operator<<(ostream& os, const Node &n){  // WORKS
    os << "Node: " << n.left << ", " << n.right << ", " << n.parent << ", " << n.elem << ", " << n.height << ", my adress: " << &n << endl;
    return os;
}
class AVL{
    public:
        Node* root{};
        AVL(){};
        ~AVL(){Destructor(root);}
        void Destructor(Node* node){
            if (node){
                Destructor(node->left);
                Destructor(node->right);
                delete node;
            }
        }
        void inorder(Node* node){ // WORKS
            if(node!=nullptr){
                inorder(node->left);
                cout << node->elem << " ";
                inorder(node->right);
            }
        }
        int minimum(Node* node){ // WORKS
            if(node->left!= nullptr) return minimum(node->left);
            return node->elem;
        }
        int maximum(Node* node){ // WORKS
            if(node->right!= nullptr) return maximum(node->right);
            return node->elem;
        }
        Node* predacessor(Node* node);
        Node* successor(Node* node);
        int height(Node* node){ 
            if(node == nullptr) return -1;
            return node->height;
        }
        int balance(Node* node){
            // cout << *node;
            int l_height = height(node->left);
            int r_height = height(node->right);
            int b_factor = l_height - r_height;
            // cout << ", Balance: " << b_factor << endl;
            return abs(b_factor);
        }
        Node* Insert(int x){ // inserting like its BST then checking for AVL proporties
            cout << "-------------------------------------------------------" << endl;
            cout << "Insert: " << x << endl;
            Node* current;
            if(root == nullptr){
                cout << "Importing root" << endl;
                Node* n = new Node( nullptr, nullptr, nullptr, x, 0);
                cout << *n;
                root = n;
            }else{
                Node* iter = root;
                while(iter != nullptr){
                    current = iter;
                    if(x > current->elem){
                        iter = iter->right;
                    }else{
                        iter = iter->left;
                    }
                }
                Node* n = new Node(nullptr, nullptr, current, x, current->height + 1);
                cout << *n ;
                cout << "Parent " << current->elem;
                if(x > current->elem){
                    current->right = n;
                    cout << " child right: " << n->elem << endl; 
                }else{
                    current->left = n;
                    cout << " child left: " << n->elem << endl;
                }
                // find one that makes AVL unbalanced then rotate
                cout << "For node: " << current->elem << " Balance: " << balance(current) << endl;
                Node* node = to_rotate(current);
                cout << *node;
                if(node != nullptr){ 
                    cout << "in rotation if! " << endl;
                    if(node->right->height > node->left->height){
                        leftRotate(node);
                    }else if(node->right->height < node->left->height){
                        rightRotate(node);
                    }
                }

            }
        };

        Node* to_rotate(Node* node){ // find node that makes AVL unbalanced
            if(balance(node) > 1 || node == nullptr){
                return node;
            }
            to_rotate(node->left);
            to_rotate(node->right);
        }

        void Delete(Node* node);

        void rightRotate(Node* &x){
            cout << "rightRotate: " << x->elem << endl;
            Node* y = x->left;
            Node* T = y->right;
            x->left = T;
            if(y->right != nullptr) y->right->parent = x;

            y->parent = x->parent;

            if(x->parent == nullptr){
                root = y;
            }else if(x = x->parent->right){
                x->parent->right = y;
            }else{
                x->parent->left = y;
            }
            y->right = x;
            x->parent = y;
        }
        void leftRotate(Node* &x){
            cout << "leftRotate: " << x->elem << endl;
            Node* y = x->right;
            Node* T = y->left;
            x->right = T;
            if(y->left != nullptr) y->left->parent = x;

            y->parent = x->parent;

            if(x->parent == nullptr){
                root = y;
            }else if(x == x->parent->left){
                x->parent->left = y;
            }else{
                x->parent->right = y;
            }   
            y->left = x;
            x->parent = y; 
        }

};

int main(){
    cout << "In main: " << endl;
    AVL avl;
    avl.Insert(1);
    avl.Insert(4);
    avl.Insert(9);
    avl.Insert(2);

    avl.inorder(avl.root);
    // cout << "Minimum: " << avl.minimum(avl.root) << endl;
    // cout << "Maximum: "<< avl.maximum(avl.root) << endl;


    return 0;
}