当有parent指针时,可以不用栈实现非递归的中序遍历,书上提到了有这种方法,但没给出例程。
public:
BTNode*next()
{
if(!current)returnNULL;
if(current->right){current=current->right;while(current->left)current=current->left;}
else
{
BTNode*y=current->parent;
while(y&¤t==y->right){current=y;y=y->parent;}
current=y;
}
returncurrent;
}
private:
BTNode*current;
上面的函数能使current指针向前移动一个位置,如果要遍历整棵二叉树,需要使current指向中序序列的第一个节点,例如下面的成员函数:
public:
voidfirst(){current=root;while(current->left)current=current->left;}
专题:http://www.qqread.com/cpp/p520596030.html
相关图文阅读
频道图文推荐
健 康 咨 询
时 尚 咨 询
相关专题
- Java语言和C++语言的差异 (29次浏览)
- 在C++中实现四种进程或线程同步互斥的控制 (0次浏览)
- Ubuntu下面的C语言代码检查工具 Splint (0次浏览)



