Hi guys! First of all i have to say that i am new to this forum. I've been learning c++ for few months now and still feel kinda unexperienced in many ways.
I hope to find good support from you guys :) Thank you!
Here is my problem: I had to program a binary tree which includes a deleteNode method. It's almost working fine. There is just a problem buth my root. Lets say i add 10 nodes to the tree and 2 are left after i deleted 8 parts. Whenever i try to delete the last 2 parts i am getting an error message.
I really hope you guys can help me out here, i've spent too many hours on this.
Here is the code:
//Treenode.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#pragma once
class treenode
{
public:
int data;
treenode *left;
treenode *right;
treenode(int value);
treenode();
~treenode(void);
};
#include "treenode.h"
treenode::treenode(int value)
{
left = 0;
right = 0;
data = value;
}
treenode::treenode()
{
left = 0;
right = 0;
data = 0;
}
treenode::~treenode(void)
{
}
//binarytree.h (includes all methods to modify/edit a tree)
Your deleteItem function is over 50 lines long, it seems unnecessarily complex. I think you should be able to simplify it dramatically, and most likely if that does not eliminate the problem it will make it easier to find.
I also think there is little reasearch made on understanding why it doesn't work.
You should try and give a number to the nodes and see which ones are left... try to see if there is some kind of "pattern" that makes your 2 nodes impossible to delete.
I believe you can print out some output while you're deleting the nodes so you know which one are currently being deleted. And you can have a clear idea on how your tree is being run through.
My personal idea is that you lose the pointer on where the 2 nodes are left. :)