二叉树遍历神招——morris Traversal

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

如下图为morris 的一个示例

【morris中序遍历】

void bst_morris_inorder(struct bst_node *root)
{
    struct bst_node *p = root, *tmp;

    while (p) {
        if (p->left == NULL) {
            printf("%d ", p->key);
            p = p->right;
        }
        else {
            tmp = p->left;
            while (tmp->right != NULL && tmp->right != p)
                tmp = tmp->right;
            if (tmp->right == NULL) {
                tmp->right = p;
                p = p->left;
            }
            else {
                printf("%d ", p->key);
                tmp->right = NULL;
                p = p->right;
            }
        }
    }
}
【morris前序遍历】

void bst_morris_preorder(struct bst_node *root)
{
    struct bst_node *p = root, *tmp;

    while (p) {
        if (p->left == NULL) {
            printf("%d ", p->key);
            p = p->right;
        }
        else {
            tmp = p->left;
            while (tmp->right != NULL && tmp->right != p)
                tmp = tmp->right;
            if (tmp->right == NULL) {
                printf("%d ", p->key);                
                tmp->right = p;
                p = p->left;
            }
            else {
                tmp->right = NULL;
                p = p->right;
            }
        }
    }
}

【morris后序遍历】This is a little chanllenge

后序遍历比较复杂,它的思路是利用中序遍历,所以首先产生了一个假的根结点,其左子树为原来的二叉树,从假的根结点开始中序遍历

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

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

static void bst_morris_reverse(struct bst_node *node, struct bst_node *last)
{
    struct bst_node *p = node, *x, *y;
    if (p == last) {
        printf("%d ", last->key);
        return;
    }

    /* change right to parent pointer */
    x = p->right;
    for (;;) {
        if (x == last) {
            x->right = p;
            break;
        }
        y = x->right;
        x->right = p;
        p = x;
        x = y;
    }

    /* visit each */
    x = last;
    for (;;) {
        printf("%d ", x->key);
        if (x == node)
            break;
        x = x->right;
    } 

    /* revert right pointer */
    p = last;
    x = last->right;
    for (;;) {
        if (x == node) {
            x->right = p;
            break;
        }
        y = x->right;
        x->right = p;
        p = x;
        x = y;
    }
}


void bst_morris_postorder(struct bst_node *root)
{
    struct bst_node dummy;
    struct bst_node *p, *tmp;

    dummy.left = root;
    dummy.right = NULL;
    p = &dummy;

    while (p) {
        if (p->left == NULL) {
            p = p->right;
        }
        else {
            tmp = p->left;
            while (tmp->right != NULL && tmp->right != p)
                tmp = tmp->right;
            if (tmp->right == NULL) {
                tmp->right = p;
                p = p->left;
            }
            else {
                bst_morris_reverse(p->left, tmp);
                tmp->right = NULL;
                p = p->right;
            }
        }
    }
}





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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值