中序遍历二叉树不用递归与栈实现

13 篇文章 0 订阅

转自https://www.geeksforgeeks.org/inorder-tree-traversal-without-recursion-and-without-stack/

Inorder Tree Traversal without recursion and without stack!

Using Morris Traversal, we can traverse the tree without using stack and recursion. The idea of Morris Traversal is based on Threaded Binary Tree. In this traversal, we first create links to Inorder successor and print the data using these links, and finally revert the changes to restore original tree.

1. Initialize current as root  初始化房前节点作为根
2. While current is not NULL   当前节点不为空时
   If current does not have left child  如果当前节点没有左子树
      a) Print current’s data   打印当前节点的值
      b) Go to the right, i.e., current = current->right  跳到右分支
   Else   
      a) Make current as right child of the rightmost  使得当前节点作为自己的左子树的最右节点的右孩子
         node in current's left subtree
      b) Go to this left child, i.e., current = current->left 跳到左分支

Although the tree is modified through the traversal, it is reverted back to its original shape after the completion. Unlike Stack based traversal, no extra space is required for this traversal.

  •  

#include<stdio.h>

#include<stdlib.h>

 

/* A binary tree tNode has data, pointer to left child

   and a pointer to right child */

struct tNode

{

   int data;

   struct tNode* left;

   struct tNode* right;

};

 

/* Function to traverse binary tree without recursion and

   without stack */

void MorrisTraversal(struct tNode *root)

{

  struct tNode *current,*pre;

 

  if(root == NULL)

     return;

 

  current = root;

  while (current != NULL)

  {                

    if (current->left == NULL)

    {

      printf("%d ", current->data);

      current = current->right;     

    }   

    else

    {

      /* Find the inorder predecessor of current */

      pre = current->left;

      while (pre->right != NULL && pre->right != current)

        pre = pre->right;

 

      /* Make current as right child of its inorder predecessor */

      if (pre->right == NULL)

      {

        pre->right = current;

        current = current->left;

      }

             

      /* Revert the changes made in if part to restore the original

        tree i.e., fix the right child of predecssor */   

      else 

      {

        pre->right = NULL;

        printf("%d ",current->data);

        current = current->right;     

      } /* End of if condition pre->right == NULL */

    } /* End of if condition current->left == NULL*/

  } /* End of while */

}

 

/* UTILITY FUNCTIONS */

/* Helper function that allocates a new tNode with the

   given data and NULL left and right pointers. */

struct tNode* newtNode(int data)

{

  struct tNode* tNode = (struct tNode*)

                       malloc(sizeof(struct tNode));

  tNode->data = data;

  tNode->left = NULL;

  tNode->right = NULL;

 

  return(tNode);

}

 

/* Driver program to test above functions*/

int main()

{

 

  /* Constructed binary tree is

            1

          /   \

        2      3

      /  \

    4     5

  */

  struct tNode *root = newtNode(1);

  root->left        = newtNode(2);

  root->right       = newtNode(3);

  root->left->left  = newtNode(4);

  root->left->right = newtNode(5);

 

  MorrisTraversal(root);

 

  getchar();

  return 0;

}

Run on IDE
Output:

 

4 2 5 1 3

Time Complexity : O(n) If we take a closer look, we can notice that every edge of the tree is traversed at-most two times. And in worst case same number of extra edges (as input tree) are created and removed.

References:
www.liacs.nl/~deutz/DS/september28.pdf
www.scss.tcd.ie/disciplines/software_systems/…/HughGibbonsSlides.pdf

Please write comments if you find any bug in above code/algorithm, or want to share more information about stack Morris Inorder Tree Traversal.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值