代码之家  ›  专栏  ›  技术社区  ›  NeerP84

AVL树旋转问题

  •  1
  • NeerP84  · 技术社区  · 8 年前

    标题:

    #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 
    

    .cpp:

    #include "LinkedBinaryTree.h"
    #include <algorithm>
    
    void LinkedBinaryTree::inOrder(Node * node)
    {
        if (node == NULL)
            return;
        inOrder(node->left);
        cout << node->word << " " << avlNum(node) << " : " ;
        inOrder(node->right);
    }
    
    void LinkedBinaryTree::rightRotate(Node* node)
    {
        Node* temp;
        temp = node->left;
        node->left = temp->right;
        temp->right = node;
        temp->parent = node->parent;
        node = temp;
        if (temp->parent = NULL) {
            _root = temp;
        }
        fixHeight(node);
        fixHeight(node->right);
        fixHeight(node->left);
    }
    
    void LinkedBinaryTree::leftRotate(Node * node)
    {
        Node* temp;
        temp = node->right;
        node->right = temp->left;
        temp->left = node;
        temp->parent = node->parent;
        node = temp;
        if (temp->parent = NULL) {
            _root = temp;
        }
        fixHeight(node);
        fixHeight(node->right);
        fixHeight(node->left);
    }
    
    void LinkedBinaryTree::rightLeftRotate(Node * node)
    {
        rightRotate(node->left);
        leftRotate(node);
    }
    
    void LinkedBinaryTree::leftRightRotate(Node * node)
    {
        leftRotate(node->right);
        rightRotate(node);
    }
    
    int LinkedBinaryTree::height(Node * node)
    {
        int h = 0;
    
        if (node != NULL) {
            h = node->height;
        }
        return h;
    }
    
    int LinkedBinaryTree::bfactor(Node * node)
    {
        return height(node->right) - height(node->left);
    }
    
    void LinkedBinaryTree::fixHeight(Node * node)
    {
        int hl = height(node->left);
        int hr = height(node->right);
        node->height = (hl > hr ? hl : hr) + 1;
    }
    
    int LinkedBinaryTree::avlNum(Node * node)
    {
        int leftH = height(node->left);
        int rightH = height(node->right);
        int avlNum = rightH - leftH;
        return avlNum;
    }
    
    LinkedBinaryTree::LinkedBinaryTree()
    {
        _root = NULL;
    }
    
    LinkedBinaryTree::~LinkedBinaryTree()
    {
    }
    
    void LinkedBinaryTree::insertNode(Node* node, string word)
    {
        if (word < node->word) {
            if (node->left != NULL)
                insertNode(node->left, word);
            else {
                node->left = new Node(word, NULL, NULL, node);
            }   
        }
        else if (word > node->word)
        {
            if (node->right != NULL)
                insertNode(node->right, word);
            else {
                node->right = new Node(word, NULL, NULL, node);
            }
        }
        else if (word == node->word) {
            node->wordCount++;
        }
        balance(node);
    }
    
    void LinkedBinaryTree::insert(string word) {
    
        if (_root == NULL) {
            _root = new Node(word, NULL, NULL, NULL);
            n++;
        }
        else {
            insertNode(_root, word);
            n++;
        }
    }
    void LinkedBinaryTree::display(Node* ptr, int level)
    {
        int i;
        if (ptr != NULL)
        {
            display(ptr->right, level + 1);
            printf("\n");
            if (ptr == _root)
                cout << "Root -> ";
            for (i = 0; i < level && ptr != _root; i++)
                cout << "        ";
            cout << ptr->word;
            display(ptr->left, level + 1);
        }
    }
    
    LinkedBinaryTree::Node * LinkedBinaryTree::root()
    {
        return _root;
    }
    
    void LinkedBinaryTree::balance(Node* node) 
    {
        fixHeight(node);
        if (bfactor(node) == 2) {
            if (bfactor(node->right) < 0)
                rightRotate(node->right);
            else
                leftRotate(node);
        }
        if (bfactor(node) == 2) {
            if (bfactor(node->left) > 0)
                leftRotate(node->left);
            else
                rightRotate(node);
        }
    }
    

    主要:

    #include "LinkedBinaryTree.h"
    #include <string>
    
    int main() {
    
        LinkedBinaryTree t;
    
        t.insert("Yes");
        t.insert("No");
        t.insert("Maybe");
        t.insert("Hopefully");
        t.insert("Absolutely");
    
        t.display(t.root(), 1);
    
        cout << endl;
        cout << endl;
    
        t.inOrder(t.root());
    
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    
    1 回复  |  直到 8 年前
        1
  •  0
  •   1201ProgramAlarm    8 年前

    您的代码中有许多错误,但阻止旋转的是第二个错误 bfactor 登记入住 balance 不正确。它应该与 -2 .

    if (bfactor(node) == -2)
    

    你面临的另一个问题是 if 您的 Rotate 函数(提高编译器警告级别会通知您这些)。