Question

if(newNode->getData()->name<currNode->getData()->name)
{
    if(currNode->getLeftChild()==NULL)
    {
        return currNode;
    }
        compare(newNode,currNode->getLeftChild());
}
else if(newNode->getData()->name>=currNode->getData()->name)
{
    if(currNode->getRightChild()==NULL)
    {
        return currNode;
    }
        compare(newNode,currNode->getRightChild());
}
else
{
    currNode==NULL;
    return currNode;
}

Does the last else not cover any other paths that could be taken? Why am i still getting an error saying not all control paths return a value? What am I missing? and any hints on a better solution would be nice! Thank you for your time.

Was it helpful?

Solution

Use below code

if(newNode->getData()->name<currNode->getData()->name)
{
    if(currNode->getLeftChild()==NULL)
    {
    return currNode;
  }
    compare(newNode,currNode->getLeftChild());
    return currNode;
}
else if(newNode->getData()->name>=currNode->getData()->name)
{
    if(currNode->getRightChild()==NULL)
{
    return currNode;
}
    compare(newNode,currNode->getRightChild());
    return currNode;
}
else
{
    currNode==NULL;
    return currNode;
}


You are not returning any value in first and second  else part
if u dont want to return anything just use return "";
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top