二进制搜索树迭代预序遍历,无需额外存储

Binary search tree iterative preorder traversal without additional storage

本文关键字:存储 遍历 搜索树 迭代 二进制      更新时间:2023-10-16

对于有序的二进制搜索树遍历,有一种不使用辅助内存(堆栈、父指针、访问标志)的迭代算法,称为Morris traversal。对于预订单和后订单遍历,有类似的算法吗?

刚刚为预购遍历找到了一个解决方案,可能可以使用

Initialize current as root 
While current is not NULL
If current does not have right child
  a) print current root
  b) Go to the left, i.e., current = current->left
Else
  a) print current root
  a) Make the whole right sub-tree of current as the left node of the rightmost child in the left sub tree(inorder predecessor of current)
  b) Go to the left child, i.e., current = current->left

如果算法有问题,请发表评论