简体   繁体   English

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

[英]Binary search tree iterative preorder traversal without additional storage

For inorder binary search tree traversal there is an iterative algorithm that uses no auxiliary memory (stack, parent pointers, visited flags) known as Morris Traversal . 对于有序二叉搜索树遍历,有一种不使用辅助存储器(堆栈,父指针,已访问标志)的迭代算法,称为Morris Traversal Is there a similar algorithm for preorder and postorder traversals? 是否有类似的用于预遍历和后遍历的算法?

just worked out a solution for preorder traversal, might work 刚刚想出了遍历遍历的解决方案,可能会奏效

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

do comment if it there is something wrong with the algorithm 如果算法有问题,请发表评论

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM