zoukankan      html  css  js  c++  java
  • 追寻伯利恒二叉树 -- 不用栈不用递归

    http://www.acmerblog.com/inorder-tree-traversal-without-recursion-and-without-stack-5988.html

    用叶子节点的空指针来记录当前节点的位置,然后一旦遍历到了叶子节点,发现叶子节点的右指针指向的是当前节点,那么就认为以当前节点的左子树已经遍历完成。

    以inorder为例,初始化当前节点为root,它的遍历规则如下:

    • 如果当前节点为空,程序退出。
    • 如果当前节点非空,
      • 如果当前节点的左儿子为空,那么输出当前节点,当前节点重置为当前节点的右儿子。
      • 如果当前节点的左儿子非空,找到当前节点左子树的最右叶子节点(此时最右节点的右儿子有两种情况,一种是指向当前节点,一种是为空,你也许感到奇怪,右节点的右儿子怎么可能非空,注意,这里的最右叶子节点只带的是原树中的最右叶子节点。),若其最右叶子节点为空,令其指向当前节点,将当前节点重置为其左儿子,若其最右节点指向当前节点,输出当前节点,将当前节点重置为当前节点的右儿子,并恢复树结构,即将最右节点的右节点再次设置为NULL。
    #include<stdio.h>
    #include<stdlib.h>
    
    struct tNode
    {
       int data;
       struct tNode* left;
       struct tNode* right;
    };
    
    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
        {
          /* 找到current的前驱节点 */
          pre = current->left;
          while(pre->right != NULL && pre->right != current)
            pre = pre->right;
    
          /* 将current节点作为其前驱节点的右孩子 */
          if(pre->right == NULL)
          {
            pre->right = current;
            current = current->left;
          }
    
          /* 恢复树的原有结构,更改right 指针 */   
          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 */
    }
    
    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);
    }
    
    /* 测试*/
    int main()
    {
    
      /* 构建树结构如下:
                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);
       return 0;
    }
  • 相关阅读:
    python 查询文件存放地址
    类数组对象:arguments
    前端开发工程师从入门到精通学习过程及资源总结(持续更新中)
    CSS3的圆角border-radius属性
    内联元素inline-block空隙问题
    js中typeof用法详细介绍
    正则表达式
    sublime text 3 快捷键大全以及配置编译环境
    c# 泛型类
    c# 中config.exe 引发的一些问题
  • 原文地址:https://www.cnblogs.com/argenbarbie/p/5408438.html
Copyright © 2011-2022 走看看