这是我的代码。当我运行我的nearestValue()方法时,我总是得到0.有谁知道如何获得最接近整数的值?这意味着我想要与我选择最接近的值的最接近的整数,但必须存在于要显示的树中
template<typename T>
T bst<T>::closestValue(T value) const {
}
template<typename T>
T bst<T>::closestValue(T value, T & closest, bst_node<T>* node) const
{
bst<T>* pClosest = NULL;
int minDistance = 5000;
bst<T>* pNode = root;
while(pNode != NULL)
{
int distance = abs(pNode->value - value);
if(distance < minDistance)
{
minDistance = distance;
pClosest = pNode;
}
if(distance == 0)
break;
if(pNode->m_nValue > value)
pNode = pNode->m_pLeft;
else if(pNode->m_nValue < value)
pNode = pNode->m_pRight;
}
return pClosest;
}