二叉搜索树求最小结点的问题

tobeistodo1 2004-10-09 04:07:30
我想利用中序遍历直接走到第一个要输出的结点,可以走到,但返回指针时,由于是递归,它层层返回,最中返回的是根结点指针,这不是我需要的,如何才能返回最内层的结点?代码如下:

template <class Type> BstNode<Type> * BST<Type>::Min(BstNode<Type> *ptr) const
{
while (ptr->leftChild != NULL)
{
Min(ptr->leftChild);
}
return ptr;
}

...全文
162 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
nlstone 2004-10-10
  • 打赏
  • 举报
回复
踏雪而歌和短歌的做法应该是正确的...你仔细调调看...
24607660 2004-10-10
  • 打赏
  • 举报
回复
这样应该可以了吧?

template <class Type> BstNode<Type> * BST<Type>::Min(BstNode<Type> *ptr) const
{
if (ptr->leftChild != NULL)
return Min(ptr->leftChild);
else
{
BstNode<Type> *pTemp = ptr->leftChild;
return pTemp;
}
}
tobeistodo1 2004-10-09
  • 打赏
  • 举报
回复
还是不行!
24607660 2004-10-09
  • 打赏
  • 举报
回复
这个函数似乎不能直接返回BstNode<Type> * 指针,建议用输出参数
24607660 2004-10-09
  • 打赏
  • 举报
回复
template <class Type> BstNode<Type> * BST<Type>::Min(BstNode<Type> *ptr) const
{
if (ptr->leftChild != NULL)
return Min(ptr->leftChild);
else
ptr = ptr->leftChild;
return;


}
ywchen2000 2004-10-09
  • 打赏
  • 举报
回复
mark
tobeistodo1 2004-10-09
  • 打赏
  • 举报
回复
还是不行,用debug走还是一样的。
短歌如风 2004-10-09
  • 打赏
  • 举报
回复
template <class Type> BstNode<Type> * BST<Type>::Min(BstNode<Type> *ptr) const
{
if (ptr->leftChild != NULL)
return Min(ptr->leftChild);
else
return ptr;
}
yegaofei 2004-10-09
  • 打赏
  • 举报
回复
template <class Type> BstNode<Type> * BST<Type>::Min(BstNode<Type> *ptr) const
{
if (ptr->leftChild != NULL)
{
return Min(ptr->leftChild);
}
else return ptr;
}


这样如何?

64,646

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧