i believe that it checks every nodes height and makes sure that the left -right is 1 or 0 or -1. It is supposed to return tree if tree is balanced which means that left-right is 0 or 1 or -1 at every node.
template <class KT, class DT>
void my_bst<KT,DT>::balanced()
{
if(balanced(root)==1)
{
cout<<"true\n";
}
else
{
cout<<"false\n";
}
}
template <class KT, class DT>
bool my_bst<KT,DT>::balanced(my_bst_node<KT,DT>*& j)
{
if((height(j->left)-height(j->right)==-1)||(height(j->left)-height(j->right)==1)||(height(j->left)-height(j->right)==0))
{
return true;
}
else
{
return false;
}
if(balanced(j->left)&&balanced(j->right))
{
if(j->left==NULL)
{
balanced(j->right);
}
else if(j->right==NULL)
{
balanced(j->left);
}
}
else
{
return false;
}
}