C++ Implementation of AVL Trees

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

 

You should start your program by initializing an empty AVL tree. Your program takes one line as input. The input line contains n “modification moves” separated by spaces (1 ≤ n ≤ 100). The available modification moves are • Aint (Character A followed by an int value between 1 and 100): A3 means insert value 3 into the AVL tree. If 3 is already in the tree, do nothing. • Dint (Character D followed by an int value between 1 and 100): D3 means delete value 3 into the AVL tree. If 3 is not in the tree, do nothing. Your input is then followed by exactly one finishing move (PRE or POST or IN): If the finishing move is PRE, then you should print out the tree (in its current situation) in pre-order. If the tree is empty, print out EMPTY. Otherwise, print out the values separated by spaces. POST and IN are handled similarly. You don’t need to worry about invalid inputs. Sample input 1: A1 A2 A3 IN Sample output 1: 1 2 3 Sample input 2: A1 A2 A3 PRE Sample output 2: 2 1 3 Sample input 3: A1 D1 POST Sample output 3: EMPTY

 

#include<iostream>
#include <sstream>
using namespace std;

class AVLNode {
    int data;
    struct AVLNode* leftNode;
    struct AVLNode* rightNode;
    int height;
    int getHeight(AVLNode * node);
    AVLNode* rightRotate(AVLNode * tmp);
    AVLNode* leftRotate(AVLNode * tmp);
    int getBalance(AVLNode * ND);

    public:
        AVLNode(int value);
        AVLNode* insertNode(AVLNode *node, int value);
        AVLNode* deleteNode(AVLNode *root, int value);
        void preOrder(AVLNode *root);
        void postOrder(AVLNode *root);
        void inOrder(AVLNode *root);
        void setHeight(int value);
        AVLNode* minValueNode();
}; 

AVLNode::AVLNode(int value){
    data = value;
    leftNode = NULL;
    rightNode = NULL;
    height = 1;
}

int AVLNode::getHeight(AVLNode * node) {
    return node == NULL ? 0 : node -> height;
} 

void AVLNode::setHeight(int value) {
    height = value + 1;
} 

AVLNode * AVLNode::rightRotate(AVLNode * tmp) {
    AVLNode * newRoot = tmp -> leftNode;
    AVLNode * T = newRoot -> rightNode;
    newRoot -> rightNode = tmp;
    tmp -> leftNode = T;
    tmp -> setHeight(max(getHeight(tmp -> leftNode), getHeight(tmp -> rightNode)));
    newRoot -> setHeight(max(getHeight(newRoot -> leftNode), getHeight(newRoot -> rightNode)));
    return newRoot;
} 


AVLNode * AVLNode::leftRotate(AVLNode * tmp) {
    AVLNode * newRoot = tmp -> rightNode;
    AVLNode * T = newRoot -> leftNode;
    newRoot -> leftNode = tmp;
    tmp -> rightNode = T;
    tmp -> setHeight(max(getHeight(tmp -> leftNode), getHeight(tmp -> rightNode)));
    newRoot -> setHeight(max(getHeight(newRoot -> leftNode), getHeight(newRoot -> rightNode)));
    
    return newRoot;
} 


int AVLNode::getBalance(AVLNode * NodeBalance) {
    return NodeBalance == NULL ? 0:getHeight(NodeBalance -> leftNode) - getHeight(NodeBalance -> rightNode);
} 


AVLNode * AVLNode::insertNode(AVLNode * node, int value) {
    if (node == NULL){
        AVLNode * newNode = new AVLNode(value);
        return newNode;
    }
    
    if (value < node -> data) {
        node -> leftNode = insertNode(node -> leftNode, value);
    }
    
    else if (value > node -> data) {
        node -> rightNode = insertNode(node -> rightNode, value);
    }
    else {
        return node;
    }
    
    node -> setHeight(max(getHeight(node -> leftNode), getHeight(node -> rightNode)));
    int balance = getBalance(node);
    if (balance > 1 && value < node -> leftNode -> data)
        return rightRotate(node);
    
    if (balance < -1 && value > node -> rightNode -> data)
        return leftRotate(node);
    
    if (balance > 1 && value > node -> leftNode -> data) {
        node -> leftNode = leftRotate(node -> leftNode);
        return rightRotate(node);
    } 
    
    if (balance < -1 && value < node -> rightNode -> data) {
        node -> rightNode = rightRotate(node -> rightNode);
        return leftRotate(node);
    } 
    
    return node;
} 

AVLNode * AVLNode::minValueNode() {
    AVLNode * current = this;
    while (current -> leftNode != NULL) {
        current = current -> leftNode;
    }
    
    return current;
} 



AVLNode * AVLNode::deleteNode(AVLNode * root, int value) {
    if (root == NULL)
        return root;
    
    if (value < root -> data){
        root -> leftNode = deleteNode(root -> leftNode, value);
    }
    else if (value > root -> data){
        root -> rightNode = deleteNode(root -> rightNode, value);
    }
    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;
            delete(tmp);
        } 
        else {
            AVLNode * tmp = root -> rightNode -> minValueNode();
            root -> data = tmp -> data;
            root -> rightNode = deleteNode(root -> rightNode, tmp -> data);
        } 
    } 
    return root;
} 

void AVLNode::preOrder(AVLNode * root) {
    if (root != NULL) {
        cout << root -> data << " ";
        preOrder(root -> leftNode);
        preOrder(root -> rightNode);
    } 
} 


void AVLNode::postOrder(AVLNode * root) {
    if (root != NULL) {
        postOrder(root -> leftNode);
        postOrder(root -> rightNode);
        cout << root -> data << " ";
    } 
} 


void AVLNode::inOrder(AVLNode * root) {
    if (root != NULL) {
        inOrder(root -> leftNode);
        cout << root -> data << " ";
        inOrder(root -> rightNode);
    } 
} 


int main() {
    AVLNode * root = NULL;
    int number;
    string command;
    getline(cin, command);
    
    for (int x = 0; x < command.length(); x++) {
        if (command.at(x) == 'A' || command.at(x) == 'a') {
            number = command.at(++x) - '0';
            root = root -> insertNode(root, number);
        } 
        
        else if (command.at(x) == 'D' || command.at(x) == 'd') {
            number = command.at(++x) - '0';
            root = root -> deleteNode(root, number);
        } 
        
        else if (command.at(x) == 'I' || command.at(x) == 'i') {
            if (root == NULL)
                cout << "\n EMPTY";
            root -> inOrder(root);
        } 
        
        else if (command.at(x) == 'P' || command.at(x) == 'p') {
            if (command.at(x + 1) == 'R' || command.at(x) == 'r') {
                if (root == NULL)
                    cout << "\n EMPTY";
                root -> preOrder(root);
            } 
            
            else if (command.at(x + 1) == 'O' || command.at(x) == 'o') {
                if (root == NULL)
                    cout << "EMPTY";
                root -> postOrder(root);
            } 
        } 
    } 
    return 0;
} 

  

上一篇:openlayers i查询功能(矢量图层、postgresql空间数据库)


下一篇:System.ComponentModel.Win32Exception解决方案