2.level = D( left of B) E ( right of B) F( left of C) G ( right of C)
3.level = H( left of D) I ( right of D) J( left of E) K ( right of E)
L( left of F)
I think there is a problem in AddNode(string info, Tree_Node* leaf). And ı do not know how to fix it. I would appreciate if someone can help, please help!
void Tree :: AddNode(string info, Tree_Node* leaf)
{
if ( leaf != NULL )
{
if(leaf->left == NULL)
{
cout << "left e eklendi" << endl;
Tree_Node* n = new Tree_Node();
n->data = info;
leaf->left = n;
return;
}
if(leaf->right == NULL)
{
cout << "right e eklendi" << endl;
Tree_Node* n = new Tree_Node();
n->data = info;
leaf->right = n;
return;
}
//Assuming both left and right were full...
AddNode(info, leaf->left);//Using recursion with checking/inserting left nodes data.
leaf = leaf->left;//Assign the current leaf its left value?
AddNode(info, leaf->right);//Do it again!! huh? Check left->right
}
}
So if Tree A had data on both its left and right, it would go the the left node and add data to its left or right. Then return and add data to the left nodes right node!
That may have been your inconsistent solution.
Ideally you should compare the data with the data within your object before pushing it left or right.
Design a algorithm to handle which function it should go to; not both.