struct elementEx
{
int key;
elementEx* left;
elementEx* right;
elementEx* parent;
~elementEx()
{
delete left;
delete right;
}
elementEx(int key)
{
this->key = key;
parent = NULL;
left = NULL;
right = NULL;
}
};
elementEx* T = root;
while(T)
{
out << T->key << endl;
if(T->left)
{
T = T->left;
out << "to left" << endl;
continue;
}
else if(T->right)
{
out << "to right" << endl;
T = T->right;
continue;
}
else
{
bool found = false;
while(!found)
{
if (T->parent)
{
if(T->parent->left == T)//left child node
{
if(T->parent->right)
{
T = T->parent->right;
out <<"go to parent' right" << endl;
found = true;
break;
}
else//right node empty
{
out <<"go to parent" << endl;
T = T->parent;
continue;
}
}
if(T->parent->right == T)//right child node
{
out <<"go to parent" << endl;
T = T->parent;
continue;
}
}
else//root node
return ;
}//while(found)
if(T && found)
continue;
}
}