0

I do not know why this code breaks ,probably when adding new node, on Windows .Returns "Process returned -1073741819 (0xC0000005)"),it was compiled with GNU GCC.It works perfectly fine on Linux. Also tested on geeksforgeeks ide , this is the link https://ide.geeksforgeeks.org/feo8SYMsFP. When debugged, SIGSEGV is returned when adding node but I am not sure why.. For example, input : 1 10 1 11 then it breaks..

#include <iostream>
#include <stdlib.h>
struct Node
{
    int key;
    Node * left;
    Node * right;
};
class binarySearchTree
{
private:
    Node *root;
    Node *newNode(int key)
    {
        Node *temp = new Node;
        temp->left = temp->right = NULL;
        temp->key = key;
        return temp;
    }
    void traverse_inorder(Node *temp)
    {
        if (temp==NULL)
            return;

        traverse_inorder(temp->left);
        std::cout <<"Current key: "<< temp->key << "\n";
        traverse_inorder(temp->right);

    }
    void find(Node* temp,int key)
    {

        if (temp==NULL)
            return;
        find(temp->left,key);
        if (temp->key == key)
            std::cout <<"Key " << key << " found\n";
        find(temp->right,key);
    }
    Node* minValueNode(Node* n)
    {
        Node* x = n;
        while (x->left != NULL)
            x = x->left;
        return x;
    }
    Node* deleteNode(Node* temp, int key)
    {
        if (temp == NULL)
            return temp;
        if (key < temp->key)
            temp->left = deleteNode(temp->left, key);
        else if (key > temp->key)
            temp->right = deleteNode(temp->right, key);
        else
        {
            if (temp->left == NULL)
            {
                Node *x = temp->right;
                delete temp;
                return x;
            }
            else if (root->right == NULL)
            {
                Node *x = temp->left;
                delete temp;
                return x;
            }
            Node* x = minValueNode(temp->right);
            temp->key = x->key;
            temp->right = deleteNode(temp->right, x->key);
        }
        return temp;
    }
    void delete_tree(Node *temp)
    {
        if (temp == NULL)
            return;
        delete_tree(temp->left);
        delete_tree(temp->right);
        delete temp;

    }
    void traverse_postorder(Node* temp)
    {
        if (temp == NULL)
            return;
        traverse_postorder(temp->left);
        traverse_postorder(temp->right);
        std::cout <<"Current key: "<< temp->key << "\n";
    }
    void traverse_preorder(Node* temp)
    {
        if (temp == NULL)
            return;
        std::cout <<"Current key: "<< temp->key << "\n";
        traverse_preorder(temp->left);
        traverse_preorder(temp->right);
    }
    void add(int key)
    {
        if (root == NULL)
            root = newNode(key);
        else
        {
            Node *temp = root;
            while (true)
            {
                if (temp->key > key)
                {
                    if (temp->left == NULL)
                    {
                        temp->left = newNode(key);
                        break;
                    }
                    else
                        temp = temp->left;
                }
                else if (temp->key < key)
                {
                    if (temp->right == NULL)
                    {
                        temp->right =newNode(key);
                        break;
                    }
                    else
                        temp = temp->right;
                }
                else
                {
                    std::cout << "Key already added!\n";
                    break;
                }
            }
        }
    }
public:

    binarySearchTree()
    {
        root = NULL;
    }
    ~binarySearchTree()
    {
        delete_tree(root);
    }
    void _find(int key)
    {
        find(root,key);
    }
    void _del(int key)
    {
        root = deleteNode(root,key);
    }
    void _traverse_postorder()
    {
        traverse_postorder(root);
    }
    void _traverse_preorder()
    {
        traverse_preorder(root);
    }
    void _traverse_inorder()
    {
        traverse_inorder(root);
    }
    void _add(int key)
    {
        add(key);
    }

};
int main()
{
    binarySearchTree bst;
    std::cout << "Binary Search Tree Menu (1-add key, 2-search key, 3-remove key, 4-traverse inorder, 5-traverse postorder, 6-traverse preorder, q-exit)\n";
    char input;
    do
    {
        std::cout << "Awaiting input ";
        std::cin >> input;
        int key;

        switch(input)
        {
        case '1':
            std::cout << "Enter key.. ";
            std::cin >> key;
            bst._add(key);
            break;
        case '2':
            std::cout << "Enter key.. ";
            std::cin >> key;
            bst._find(key);
            break;
        case '3':
            std::cout << "Enter key.. ";
            std::cin>>key;
            bst._del(key);
            break;
        case '4':
            bst._traverse_inorder();
            break;
        case '5':
            bst._traverse_postorder();
            break;
        case '6':
            bst._traverse_preorder();
            break;


        }
    }
    while (input != 'q');
    std::cout << "Deleting Binary Search Tree...\n";
    return 0;
}
1
  • @M.M thanks a lot ! problem solved ;) Commented May 5, 2018 at 12:22

1 Answer 1

1

This code:

struct Node
{
    int key;
    Node * left;
    Node * right;
};

Node *newNode(int key)
{
    Node *temp = new Node;
    temp->key = key;
    return temp;
}

means that newNode returns pointer to node with indeterminate values for left and right. Instead these should be null pointers, (e.g. use new Node(); instead of new Node;).

On the original system it probably happened to get zeroed memory from the allocator so the problem didn't show up.

Sign up to request clarification or add additional context in comments.

2 Comments

Being a pretty odd behaviour, you should expand on the difference between new Node(); and new Node;. This answer would be a nice reference, since it actually describes it.
@LoPiTaL my answer already says that this will make them null pointers instead of uninitialized ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.