What factors could cause bad_alloc?

114 Views Asked by At

I was writing an AVL tree implementation but got an error due to bad_alloc on another platform. However, the code works perfectly for me in CS50 IDE without any warning. In my code, I used new keyword to declare objects and I did deleted them after using. My question is that what other factors could result in this bad_alloc error? And how can I solve it?

#include <vector>
using namespace std;

class node {
    public:
    int value;
    int height;
    node* left;
    node* right;
    node();
};

// freeing the dynamically allocated memory
void freeTree(node* root){

    if((root->left) != nullptr){
        freeTree(root->left);
    }

    if((root->right) != nullptr){
        freeTree(root->right);
    }

    delete root;
}

node::node(){
    height = 1;
}

int get_height(node* n){
    if(n == NULL){
        return 0;
    }
    return n->height;
}

int get_balance_factor(node* n){
    if(n == NULL){
        return 0;
    }
    if(n->value == 0){
        return 0;
    }
    return (get_height(n->left))-(get_height(n->right));
}

// prints out the AVL tree by inorder traversal
void inorder(node* root){

    if((root->left) != nullptr){
        inorder(root->left);
    }

    cout << root->value << ' ';

    if((root->right) != nullptr){
        inorder(root->right);
    }

}

// prints out the AVL tree by preorder traversal
void preorder(node* root){

    cout << root->value << ' ';

    if((root->left) != nullptr){
        preorder(root->left);
    }

    if((root->right) != nullptr){
        preorder(root->right);
    }

}

// prints out the AVL tree by postorder traversal
void postorder(node* root){

    if((root->left) != nullptr){
        postorder(root->left);
    }

    if((root->right) != nullptr){
        postorder(root->right);
    }

    cout << root->value << ' ';

}

node* left_rotation(node* root){
    node* temp = root->right;
    root->right = root->right->left;
    temp->left = root;

    root->height = max(get_height(root->left),get_height(root->right))+1;
    temp->height = max(get_height(temp->left),get_height(temp->right))+1;
    return temp;
}

node* right_rotation(node* root){
    node* temp = root->left;
    root->left = root->left->right;
    temp->right = root;

    root->height = max(get_height(root->left),get_height(root->right))+1;
    temp->height = max(get_height(temp->left),get_height(temp->right))+1;
    return temp;
}

node* add_node(int k, node* root){
    // cout << "inserting " << k << endl;
    // BST insertion
    if(root == NULL){
        node* temp = new node();
        temp->value = k;
        return temp;
    }
    if(root->value == 0){
        node* temp = new node();
        temp->value = k;
        return temp;
    }
    else if(k > (root->value)){
        root->right = add_node(k, root->right);
    }
    else if(k < (root->value)){
        root->left =  add_node(k, root->left);
    }
    else{
        return root;
    }

    root->height = max(get_height(root->left),get_height(root->right))+1;

    // rebalancing

    int bf = get_balance_factor(root);
    // right-heavy tree
    if(bf < -1){
        return left_rotation(root);
    }
    // left-heave tree
    if(bf > 1){
        return right_rotation(root);
    }

    return root;
}

int main(){
    // initializing an avl tree
    node* root = new node();

    // handling input
    vector<string> input;

    while(true){
        string temp;
        cin >> temp;
        input.push_back(temp);

        if(cin.peek() == 32){
            cin.ignore();
        }
        else if(cin.peek() == 10){
            break;
        }
    }

    // processing input
    for(int i = 0; i < (int)input.size()-1; i++){
        string temp = input[i];
        char operation = temp[0];
        int number = stoi(temp.substr(1, temp.length()-1));

        if(operation == 'A'){
        root = add_node(number, root);
        }
        else if(operation == 'D'){
            // call delete node function
        }
    }

    // traversal
    string traversal = input[input.size()-1];
    if(traversal == "PRE"){
        preorder(root);
    }
    else if(traversal == "IN"){
        inorder(root);
    }
    else if(traversal == "POST"){
        postorder(root);
    }

    freeTree(root);

    return 0;
}```
0

There are 0 best solutions below