zoukankan      html  css  js  c++  java
  • C++ Implementation of AVL Trees

    仅供学习使用,复制粘贴需谨慎。

    仅供学习使用,复制粘贴需谨慎。

    仅供学习使用,复制粘贴需谨慎。

      

    #include <iostream>
    #include <sstream>
    using namespace std;
    
    class AVLNode
    {
    public:
        int key;
        AVLNode *leftNode;
        AVLNode *rightNode;
        int height;
        AVLNode()
        {
            leftNode = NULL;
            rightNode = NULL;
            key = -1;
            height = 0;
        }
    };
    
    int getHeight(AVLNode *N)
    {
        return N == NULL ? 0:N->height;
    }
    
    AVLNode *rightRotate(AVLNode *y)
    {
        AVLNode *x = y->leftNode;
        AVLNode *T2 = x->rightNode;
        
        x->rightNode = y;
        y->leftNode = T2;
        
        y->height = max(getHeight(y->leftNode), getHeight(y->rightNode)) + 1;
        x->height = max(getHeight(x->leftNode), getHeight(x->rightNode)) + 1;
        
        return x;
    }
    
    AVLNode *leftRotate(AVLNode *x)
    {
        AVLNode *y = x->rightNode;
        AVLNode *swp = y->leftNode;
        
        y->leftNode = x;
        x->rightNode = swp;
        
        x->height = max(getHeight(x->leftNode), getHeight(x->rightNode)) + 1;
        y->height = max(getHeight(y->leftNode), getHeight(y->rightNode)) + 1;
        return y;
    }
    
    int getBalance(AVLNode *node)
    {
        return node == NULL ? 0:getHeight(node->leftNode) - getHeight(node->rightNode);
    }
    
    AVLNode *insert(AVLNode *node, int key)
    {
        if (node == NULL) {
            AVLNode *newNode = new AVLNode();
            newNode->key = key;
            newNode->leftNode = NULL;
            newNode->rightNode = NULL;
            newNode->height = 1; 
            return newNode;
        }
        if (key < node->key) {
            node->leftNode = insert(node->leftNode, key);
        }
        else if (key > node->key) {
            node->rightNode = insert(node->rightNode, key);
        }
        else {
            return node;
        }
        node->height = 1 + max(getHeight(node->leftNode), getHeight(node->rightNode));
        int balance = getBalance(node);
        
        if (balance > 1 && key < node->leftNode->key) {
            return rightRotate(node);
        }
        
        if (balance < -1 && key > node->rightNode->key) {
            return leftRotate(node);
        }
        
        if (balance > 1 && key > node->leftNode->key)
        {
        node->leftNode = leftRotate(node->leftNode);
            return rightRotate(node);
        }
        
        if (balance < -1 && key < node->rightNode->key)
        {
            node->rightNode = rightRotate(node->rightNode);
            return leftRotate(node);
        }
        return node;
    }
    
    AVLNode *minValueAVLNode(AVLNode *node)
    {
        AVLNode *current = node;
        while (current->leftNode != NULL)
        current = current->leftNode;
        return current;
    }
    
    AVLNode *deleteAVLNode(AVLNode *root, int key)
    {
        if (root == NULL) {
            return root;
        }
        if (key < root->key) {
            root->leftNode = deleteAVLNode(root->leftNode, key);
        }
        else if (key > root->key) {
            root->rightNode = deleteAVLNode(root->rightNode, key);
        }
        else {
            if ((root->leftNode == NULL) || (root->rightNode == NULL)) {
                AVLNode *tmp = root->leftNode ? root->leftNode : root->rightNode;
                if (tmp == NULL) {
                    tmp = root;
                    root = NULL;
                }
                else { 
                    *root = *tmp; 
                }
                free(tmp);
            } else {
                AVLNode *tmp = minValueAVLNode(root->rightNode);
    
                root->key = tmp->key;
                root->rightNode = deleteAVLNode(root->rightNode,
                tmp->key);
            }
        }
     
        if (root == NULL) {
            return root;
        }
     
        root->height = 1 + max(getHeight(root->leftNode), getHeight(root->rightNode));
        int balance = getBalance(root);
    
        if (balance > 1 && getBalance(root->leftNode) >= 0) {
            return rightRotate(root);
        }
     
        if (balance > 1 && getBalance(root->leftNode) < 0) {
            root->leftNode = leftRotate(root->leftNode);
            return rightRotate(root);
        }
     
        if (balance < -1 && getBalance(root->rightNode) <= 0) {
            return leftRotate(root);
        }
     
        if (balance < -1 && getBalance(root->rightNode) > 0) {
            root->rightNode = rightRotate(root->rightNode);
            return leftRotate(root);
        }
        return root;
    }
    
    
    void preOrder(AVLNode *root)
    {
        if(root == NULL) return;
        cout << root->key << " ";
        preOrder(root->leftNode);
        preOrder(root->rightNode);
        
    }
    
    void postOrder(AVLNode *root)
    {
        if(root == NULL) return;
        postOrder(root->leftNode);
        postOrder(root->rightNode);
        cout << root->key << " ";
        
    }
    
    void inOrder(AVLNode *root)
    {
        if(root == NULL) return;
        inOrder(root->leftNode);
        cout << root->key << " ";
        inOrder(root->rightNode);
        
    }
    
    bool findNode(AVLNode *root, int value) {
        if (root == NULL) {
            return false;
        }
        if (root->key == value) {
            return true;
        }
        else if (value < root->key) {
            return findNode(root->leftNode, value);
        }
        else if (root->key < value) {
            return findNode(root->rightNode, value);
        }
        return false;
    }
    
    int main()
    {
        AVLNode *root = NULL;
        string input;
        getline(cin, input); 
        istringstream mystring(input);
        string op;
        while (mystring >> op) {
            char choice = op[0];
            if(choice == 'A')
            {
                if (findNode(root, stoi(op.substr(1)))) {
                } else {
                    root = insert(root, stoi(op.substr(1)));
                }
                
            }
            
            if(choice == 'D') {
                root = deleteAVLNode(root, stoi(op.substr(1)));
            }
                
            if(choice == 'I') 
            {
                if (root == NULL) {
                    cout << "EMPTY" << endl;
                }
                else {
                    inOrder(root);
                }
            }
                
            if(choice == 'P') 
            {
                if (op[1] == 'R') { 
                    if (root == NULL) {
                        cout << "EMPTY" << endl;
                    }
                    else {
                        preOrder(root);
                    }
                }
                else { 
                    if (root == NULL) {
                        cout << "EMPTY" << endl;
                    }
                    else
                        postOrder(root);
                }
            }
        }
        return 0;
    }
    

      

  • 相关阅读:
    安装jupyter_contrib_nbextensions库
    1.20
    架构之美阅读笔记01
    使用 netcat 数据源测试 Flume
    使用 Avro 数据源测试 Flume
    Tensorflow01-认识张量Tensor
    Spark06-RDD分区、缓存与Checkpoint讲解
    Spark05-RDD算子细谈
    Spark04-RDD入门
    Spark03-Scala面向对象和函数式编程
  • 原文地址:https://www.cnblogs.com/adelaide/p/15292285.html
Copyright © 2011-2022 走看看