morris traversal(线索二叉树)

刚开始接触到这个是因为算法导论习题10.4-5,其中有一句话说不能改变二叉树结构,即使临时改变也不行,个人就感觉改变二叉树结构也可以进行遍历。


搜索的过程中发现了morris 遍历,刚开始根本不相信如此短的代码可以使用无栈、O(1) 空间进行二叉树遍历,但深入之后才发现这个想法是如此的巧妙,感谢J Morris,不知这个Morris 和KMP中的Morris 是否是同一个人?


morris 中序遍历

[cpp]  view plain copy
  1. void bst_morris_inorder(struct bst_node *root)  
  2. {  
  3.     struct bst_node *p = root, *tmp;  
  4.   
  5.     while (p) {  
  6.         if (p->left == NULL) {  
  7.             printf("%d ", p->key);  
  8.             p = p->right;  
  9.         }  
  10.         else {  
  11.             tmp = p->left;  
  12.             while (tmp->right != NULL && tmp->right != p)  
  13.                 tmp = tmp->right;  
  14.             if (tmp->right == NULL) {  
  15.                 tmp->right = p;  
  16.                 p = p->left;  
  17.             }  
  18.             else {  
  19.                 printf("%d ", p->key);  
  20.                 tmp->right = NULL;  
  21.                 p = p->right;  
  22.             }  
  23.         }  
  24.     }  
  25. }  
代码相当简单,局部变量也只需要两个。以下简单讲述其实现方式。


morris traversal 原理很简单,利用所有叶子结点的right 指针,指向其后继结点,组成一个环,在第二次遍历到这个结点时,由于其左子树已经遍历完了,则访问该结点

如下图为morris 的一个示例



morris 前序遍历

[cpp]  view plain copy
  1. void bst_morris_preorder(struct bst_node *root)  
  2. {  
  3.     struct bst_node *p = root, *tmp;  
  4.   
  5.     while (p) {  
  6.         if (p->left == NULL) {  
  7.             printf("%d ", p->key);  
  8.             p = p->right;  
  9.         }  
  10.         else {  
  11.             tmp = p->left;  
  12.             while (tmp->right != NULL && tmp->right != p)  
  13.                 tmp = tmp->right;  
  14.             if (tmp->right == NULL) {  
  15.                 printf("%d ", p->key);                  
  16.                 tmp->right = p;  
  17.                 p = p->left;  
  18.             }  
  19.             else {  
  20.                 tmp->right = NULL;  
  21.                 p = p->right;  
  22.             }  
  23.         }  
  24.     }  
  25. }  
比较简单,只要注意访问结点的顺序即可

morris 后序遍历

[cpp]  view plain copy
  1. static void bst_morris_reverse(struct bst_node *node, struct bst_node *last)  
  2. {  
  3.     struct bst_node *p = node, *x, *y;  
  4.     if (p == last) {  
  5.         printf("%d ", last->key);  
  6.         return;  
  7.     }  
  8.   
  9.     /* change right to parent pointer */  
  10.     x = p->right;  
  11.     for (;;) {  
  12.         if (x == last) {  
  13.             x->right = p;  
  14.             break;  
  15.         }  
  16.         y = x->right;  
  17.         x->right = p;  
  18.         p = x;  
  19.         x = y;  
  20.     }  
  21.   
  22.     /* visit each */  
  23.     x = last;  
  24.     for (;;) {  
  25.         printf("%d ", x->key);  
  26.         if (x == node)  
  27.             break;  
  28.         x = x->right;  
  29.     }   
  30.   
  31.     /* revert right pointer */  
  32.     p = last;  
  33.     x = last->right;  
  34.     for (;;) {  
  35.         if (x == node) {  
  36.             x->right = p;  
  37.             break;  
  38.         }  
  39.         y = x->right;  
  40.         x->right = p;  
  41.         p = x;  
  42.         x = y;  
  43.     }  
  44. }  
  45.   
  46.   
  47. void bst_morris_postorder(struct bst_node *root)  
  48. {  
  49.     struct bst_node dummy;  
  50.     struct bst_node *p, *tmp;  
  51.   
  52.     dummy.left = root;  
  53.     dummy.right = NULL;  
  54.     p = &dummy;  
  55.   
  56.     while (p) {  
  57.         if (p->left == NULL) {  
  58.             p = p->right;  
  59.         }  
  60.         else {  
  61.             tmp = p->left;  
  62.             while (tmp->right != NULL && tmp->right != p)  
  63.                 tmp = tmp->right;  
  64.             if (tmp->right == NULL) {  
  65.                 tmp->right = p;  
  66.                 p = p->left;  
  67.             }  
  68.             else {  
  69.                 bst_morris_reverse(p->left, tmp);  
  70.                 tmp->right = NULL;  
  71.                 p = p->right;  
  72.             }  
  73.         }  
  74.     }  
  75. }  
后序遍历比较复杂,它的思路是利用中序遍历,所以首先产生了一个假的根结点,其左子树为原来的二叉树,从假的根结点开始中序遍历

它和中序遍历有所不同,在发现当前结点左子树为空时,不访问此结点(后序遍历需要保证访问完右子树后才能访问根结点),直接访问右子树。

第二次遍历到某个结点时,将该结点左子树的最右路径反序输出即可,对应函数为bst_morris_reverse

转载:http://blog.csdn.net/wdq347/article/details/8853371


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值