last day (14 days later) » 

3:08 PM
2
A: Various issues with binary tree minheap implementation in C

AracthorFirst, when you have compiler warnings messages like this, do not try to execute. It means you shall obviously memory leaks somewhere. And your problem is effectively that you are making a comparaison between an integer and a pointer here : (root->key == NULL) According to your structure, root->

 
Thanks! Would it be possible to do this instead of the check for whether root->key is NULL: if (root == NULL) root->key = key; ? Or is there another way to check for this without adding another variable to the struct? (the struct was predetermined for this exercise) Regarding the print part - that's actually what I was initially aiming to do but then got confused halfway through and made it a loop...
 
I edited the part on the check on root->key : your insert function was not necessary here. Except if your tree have to handle node without data, but if your structure is predetermined, that should not be the case. And for the print part, recursive functions are common in tree management, so you shall have to be used to it, sorry.
 
Thanks! I adjusted the code according to your suggestions and will update the OP accordingly in a bit. It still doesn't work quite correctly, though. Recursion for print is no big problem. I just couldn't figure it out at the time and decided to try and do what seemed the easier solution first. I also adjusted the print function you suggested slightly to get an in-order output instead of your pre-order one. I also changed the formatting but that's nothing deal-breaking.
I have updated the OP and found another error in my code while doing so. Now the only part left is to exclude the root from having a connection while printing I think. I'll look into how to do that. It also looks like it's missing the 12 in the first tree.
 
You forgot to initialise your root node key. That is why your trees start by a random value. When you create a tree, you usually initialise the root key as your first value to insert rather than inserting it after. Else, your tree seems to be correctly sorted... Try the new version of printNode, it should go to any node. Yours don't always go to right children.
 
I'm confused. Wasn't initializing the root key what I was doing in my previous insert() function which I then replaced with heapify as per your instruction. I'm probably misunderstanding something. How would I make sure that if the root->key is empty that the first value I insert() would be made the root->key? Regarding the printfunctions, I actually just found the problem myself as well and will insert the code into OP in a second.
 
3:08 PM
Without adding a new attribute in your structure, there is no stable way to check a root node via insert. However, you could make an createTree(int rootKey) function that take as parameter only the first key of the tree, and return you a root node with NULL children and a key equals to your parameter. (I added it in my answer)
 
2 things. My exercise actually calls for first creating an empty tree and then adding all values into it. As createTree() calls for an initial key value I don't think that would count, sadly. Maybe I could just initialize the tree with root->key = -1 or something like that... Furthermore, am I understanding this correctly that the way to call createTree() would be node *root1 = createTree(value);?
 
You understood correctly the way to call createTree(), and I think it is still a good idea even if you cannot initialise the first key with it. But if you don't have right to give to it an argument, you can still initialise it with a default value - like 0 - to not make it random. Do never let random values somewhere, it's a big leak source.
 
Understood. I updated my code accordingly and inserted it into the OP. Thank you for your help!
 
Any more trouble ?
 
I just noticed that there may be an additional problem with the code in the deleteTree() function. I added a printTree() command after the deleteTree() to check whether everything got deleted properly
graph g {
4 -- 2
4 -- 8
2 -- 1
2 -- 3
8 -- 6
8 -- 9
6 -- 7
9 -- 12
}
graph g {
3 -- 1
3 -- 8
8 -- 7
8 -- 10
}
graph g {
5837208 -- 5836872
5837208 -- 8
5836872 -- 5836840

Process returned -1073741819 (0xC0000005) execution time : 0.343 s
Press any key to continue.
 
3:11 PM
If your deleteTree worked, you shouldn't be able to print anything after deleted it...
 
I'm not sure whether this is expected due to trying to print an empty tree or whether there is still a problem
 
When you free() something, you cannot even try to use it after.
 
that's what I thought too. But there is an "8" in there
which doesn't seem to be a random number
 
In fact, free() doesn't really "erase" what you did. It just make your data free to use by another allocation.
 
since it stays during multiple executions of the code.
hmm
 
3:13 PM
So if you try to use a data freed, anything can happen.
It may be the same value than before...
Another one...
 
ok
 
Or with bad luck, a core dump.
 
thanks!
 
Your welcome :)
 
haha, I'd like to avoid that
 

last day (14 days later) »