1

I made a binary tree class which holds: int value, BinaryTree* left, BinaryTree* right.

class BinaryTree {
private:
     int value;
     BinaryTree* left;
     BinaryTree* right;
     bool isVisited;
public:
     BinaryTree();
     BinaryTree createComplete(int n);
     ~BinaryTree();
}

My destructor is :

BinaryTree::~BinaryTree() {
delete left;
delete right;   
}

When running in clion it works perfectly, but in my terminal I get a segfault (core dumped). Everywhere I looked people claimed that this should be the destructor. Any elaboration would help!

I am not a stackoverflow expert , I updated my ~BinaryTree function to still gets a segfault :

BinaryTree::~BinaryTree() {
if (right != NULL) {
   delete right;
}
if (left != NULL) {
   delete left;
}

}

2
  • 1
    Can you also show the constructor definition? Commented Nov 24, 2016 at 18:02
  • How do you build your BST? Can you post enough code that we can reproduce this issue on our end? Commented Nov 24, 2016 at 18:30

3 Answers 3

2

First of all your current implementation is not that of a complete tree. It is a node, thus I suggest renaming it to BinaryTreeNode and using it to construct a new class BinaryTree, that keeps track of the root and allows you to recursively deallocate the tree.

Having said that your destructor most likely segfaults because you are blindly attempting to delete a pointer.

First make sure you initialize left and right to nullptr. Then you do if(left != nullptr) { delete left }

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

3 Comments

'BinaryTree::~BinaryTree() { if (right != NULL) { delete right; } if (left != NULL) { delete left; } }' updated my file , still gets a segfault
Did you make sure you set them to NULL? If you don't they might contain garbage values and your program might be attempting to delete memory it is not allowed to touch
First of all i am sorry , It is my first or second time using stackoverflow , I did initialize them to NULL. Again , in CLion it works fine. in terminal segfault.
0

Without seeing your constructor, I assume you don't initialize your node's children to NULL. That might mean that the uninitialized nodes left and right at the bottom leaves have a random value in them. When the destructor runs, it will try to free the memory that the random garbage in the nodes point to.

Try initializing your child nodes to NULL when ctoring nodes, then making a check for it like monoceres suggested. It will also be good to set the pointer to NULL after delete to avoid situation of erronous double delete

1 Comment

I do initialize the node's children to null.
0

So after debugging I noticed that the every right child is loosing it's nodes , which while going in a pre order traversal is fine , but when deleting it casuing the problem , thanks for the help every one !

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.