Hey guys, I'm having some trouble with my binary tree for school. It is a data structures class so we are working on learning how to make our own binary trees and encrypt messages. Everything so far is working, except for my delete node function. I'm trying to do it recursively but if you know a better way to do it then please let me know. I will show you parts of my code.
tempnode = n;
//check if left or right child is null
if(n->left=='\0' || n->right=='\0')
{
//if n is left child of pn
if(pn->left == n)
{
//if n left child is null but right child is not null
if(n->left=='\0' && n->right!='\0')
pn->left=n->right;
//If n left child is not null but right child is null
else if(n->left!='\0')
pn->left=n->left;
//If n left child is null and right child is null
else
pn->left = '\0';
delete tempnode;
}
//if n is right child of pn
else if(pn->right == n)
{
if(n->left=='\0' && n->right!='\0')
pn->right=n->right;
else if(n->left!='\0')
pn->right=n->left;
else
pn->right = '\0';
delete tempnode;
}
}
else
{
//go to left child and search the rightmost of that left chlid
node* tmpleft = n->left;
node* ptmpleft = n;
//if left child of n does not have right child.
if(tmpleft->right == '\0')
{
if(pn->left == n)
pn->left = tmpleft;
else
pn->right = tmpleft;
tmpleft->right = n->right;
}
else
{
//search the rightmost child of left child of n
while (tmpleft->right != '\0')
{
ptmpleft = tmpleft;
tmpleft = tmpleft->right;
}
Please learn to show code, this is really hard to read.
You aren't really explaining what I did wrong and how you are fixing it. Just seems like you're changing my code how you see fit because that is how you do things. I'd like to keep my code as close to what I have as possible, just please explain what is wrong with it, and what to do to fix it. You changed stuff that didn't really need changing, what you did would make me have to go back and change my whole program (because I only showed you a little bit of it.)