Morris Traversal - 不需递归和栈中序遍历二叉树

Morris Traversal的目的是在不适用递归和栈的情况下,在相同时间内得到二叉树中序遍历结果,空间复杂度为O(1)

基本思想是线索二叉树:首先建立中序遍历的后向链接指针,然后通过指针进行中序遍历,遍历后再将二叉树还原。

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

注意:1、关键一步是倒数第二行,一个节点在中序遍历的前趋节点是其左子树的最右边的节点(一直向右儿子遍历直到其不存在)

2、遍历完成后,若在倒数第二行这一步找到的节点为current自己,那么将前一个节点的右儿子置为空,即还原二叉树


#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;
}

参考:http://www.geeksforgeeks.org/inorder-tree-traversal-without-recursion-and-without-stack/

相关题目:https://oj.leetcode.com/problems/recover-binary-search-tree/

在计算机科学中,创建二叉树通常涉及定义一个节点结构,包含两个子节点和一个值。以下是简单的步骤: 1. **创建二叉树**: 创建二叉树的类或结构体,例如在 C++ 中: ```cpp struct TreeNode { int val; TreeNode* left; TreeNode* right; TreeNode(int x) : val(x), left(NULL), right(NULL) {} }; ``` 通过递归或迭代的方式构造二叉树,如插入节点、从数组或序列构建等。 2. **中序遍历**: - **递归实现**: 对于每个节点,先访问左子树(`left->visit()`),然后访问自身(`visit()`),最后访问右子树(`right->visit()`)。 - **非递归实现**(也称为 Morris 遍历): 使用两个指针 `cur` 和 `pre`,在当前节点的左子树空时,将 `pre` 移动到当前节点并更新 `cur`,然后访问当前节点。完成后回溯到 `pre`。 3. **中序线索化二叉树**: 在中序遍历时,在每个节点处添加线索,指向其前驱节点(对于左线索,如果当前节点为左孩子,前驱为父节点;否则为空),以便后续无需递归跟踪即可完成遍历。 4. **利用中序线索化二叉树实现中序遍历**: - 从根节点开始,如果当前节点不为空,找到其左线索(可能为NULL)。 - 递归或迭代地遍历左子树,直到左子树为空或遇到非空节点的前驱。 - 访问当前节点,并移动到右子树,如果右子树存在,则继续遍历,否则返回上一个节点的右线索(表示回溯到父节点)。 5. **菜单提示操作**: - 插入节点:用户输入节点值,系统在相应位置插入新节点。 - 中序遍历:提示用户输入“中序遍历”,系统按照线索顺序输出节点值。 - 删除节点:用户选择节点进行删除。 - 查找节点:用户输入值,系统返回是否存在。 - 修改节点值:用户选择节点并输入新值。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值