Delete whole binary tree C++

0

Hi I having problem when I try to implement my delete tree function in my Bst class. This is what i have implemented. Currently only have the insert and deletetree functions.

struct nodeType
{
    int data;
    nodeType * LeftLink;
    nodeType * RightLink;
};

class Bst
{
public:
        /**
        * Default constructor
        */
    Bst();
    ~Bst();
    bool isEmpty() const;
    void Insert(int insertItem);
    void Destroy(nodeType* &p);
    void DeleteTree();
    bool Search(int searchItem);
    void inOrderTraversal() const;
    void preOrderTraversal() const;
    void postOrderTraversal() const;
    void inOrderTraversal(void(*visit) (int& item)) const;

private:
    nodeType * root;
    void inOrder(nodeType *p, void(*visit) (int &item)) const;
};

Bst::Bst()
{
    root = nullptr;
}
Bst::~Bst()
{
    Destroy(root);
}
bool Bst::isEmpty() const
{
    return (root == nullptr);
}
void Bst::Insert(int insertItem)
{
    nodeType* NewNode = new nodeType;
    nodeType* trailCurrent = nullptr;
    nodeType* current;

    NewNode->data = insertItem;
    NewNode->LeftLink = NULL;
    NewNode->RightLink = NULL;
    if(root == NULL)
    {
        root = NewNode;
    }
    else
    {
        current = root;

        while(current != nullptr)
        {
            trailCurrent = current;
            if(insertItem == current->data)
            {
                cout << "item already inserted, No duplicates allowed." << endl;
                return;
            }
            else if(insertItem > current->data)
            {
                current = current->RightLink;
            }
            else
            {
                current = current->LeftLink;
            }
        }//endwhile

        if(insertItem > trailCurrent->data)
        {
            trailCurrent->RightLink = NewNode;
            cout << NewNode->data << " Inserted" << endl;
        }
        else
        {
            trailCurrent->LeftLink = NewNode;
            cout << NewNode->data << " Inserted" << endl;
        }
    }
}
//Delete tree function
void Bst::Destroy(nodeType * &p)
{
    if(p != NULL)
    {
        Destroy(p->LeftLink);
        Destroy(p->RightLink);
        delete p;
        p = NULL;
    }
}
void Bst::DeleteTree()
{
    Destroy(root);
}

main

int main()
{
    Bst intTree;
    intTree.Insert(5);
    intTree.Insert(3);
    intTree.Insert(7);
    intTree.Insert(10);
    intTree.DeleteTree();
    cout << "is tree empty: " << intTree.isEmpty() << endl;
}

When i try to call the DeleteTree function my programs ends without printing out the "is tree empty:" line and ends with Process returned -1073741571 (0xC00000FD).

Can anyone figure out what is happening?

EDIT::

Thanks for all the help! I have changed the insert function as it looks like I was doing the inserting wrongly. I have updated my code to the corrected version.

Yet again thanks for the help.

c++
asked on Stack Overflow Mar 13, 2021 by Vincent • edited Mar 13, 2021 by Vincent

2 Answers

0

The problem is not with your DeleteTree method. Instead the Insert method is inserting same node at multiple places which is causing issue of dangling pointers because when two or more pointers point to single node and one is deleted then the other is pointing to a deleted node and that causes the crash when we try to access it.

Here is a simplified version of insert. This fixes the issue, although I am not sure if that is your intended implementation requirement.

class Bst
{
public:
    Bst();
    bool isEmpty() const;
    nodeType* Insert(int insertItem, nodeType *tmpRoot=NULL);
    void Destroy(nodeType*& p);
    void DeleteTree();
    bool Search(int searchItem);
    void inOrderTraversal() const;
    void preOrderTraversal() const;
    void postOrderTraversal() const;
private:
    nodeType* root;
};

nodeType* Bst::Insert(int insertItem, nodeType* tmpRoot)
{
    nodeType* NewNode = new nodeType;
    NewNode->data = insertItem;
    NewNode->LeftLink = NewNode->RightLink = nullptr;

    if (tmpRoot == nullptr)
    {
        tmpRoot = NewNode;
        if (root == nullptr)
            root = tmpRoot;
    }
    else
    {
        if (tmpRoot->data == insertItem)
        {
            cout << "item already inserted, No duplicates allowed." << endl;
            return tmpRoot;
        }
        else if (tmpRoot->data > insertItem)
        {
            tmpRoot->LeftLink = Insert(insertItem, tmpRoot->LeftLink);
        }
        else
        {
            tmpRoot->RightLink = Insert(insertItem, tmpRoot->RightLink);
        }
    }
    return tmpRoot;
}

After updating this code it should work just fine.

0

Please edit as follows:

while(current != nullptr)
{
    trailCurrent = current;
    if(current->data == insertItem)
    {
        cout << "item already inserted, No duplicates allowed." << endl;
        return;
    }
    else if(current->data > insertItem)
    {
        current = current->RightLink;
    }
    else
    {
        current = current->LeftLink;
    }
}

if(trailCurrent->data > insertItem)
{
   trailCurrent->LeftLink = NewNode;
}
else
   trailCurrent->RightLink = NewNode;
answered on Stack Overflow Mar 13, 2021 by HSh

User contributions licensed under CC BY-SA 3.0