Morris

/*Morris遍历树:
*一;如果一个结点有左子树会到达此节点两次(第二次到达结点的时候左子树的所有结点都遍历完成),第一次遍历左子树最后
* 节点为nullptr,第二次遍历指向他自己
*二:否则到达一次(第一次到来和第二次到来一起 )
*当前节点cur
*1.如果cur没有左子树,cur向右移动:cur=cur->right
*2.如果cur有左结点,找左子树最右的结点记为mostRight
* 1>如果mostRight的右指针为nullptr,让其指向cur,cur向左移动:cur=cur->left
* 2>如果mostRight指向cur,让其指向nullptr,cur向右移动
*/
#include <iostream>
#include <algorithm>
#include <iterator>
#include <list>
using namespace std; typedef struct Tree
{
int value;
Tree *left,*right;
Tree()
{
value=;
left=right=nullptr;
}
}Tree;
void create_tree(Tree **root)
{
int n;
cin>>n;
if(n==)
*root=nullptr;
else
{
*root=new Tree();//*root中的内容就是地址
(*root)->value=n;
(*root)->left=nullptr;
(*root)->right=nullptr;
create_tree(&((*root)->left));
create_tree(&((*root)->right));
}
}
class Morris
{
public:
void morris_in(Tree *const root);//O(1)的空间复杂度遍历方法
void morris_pre(Tree *const root);
void morris_back(Tree *const root);
private:
void print_edge(Tree *const cur);
};
//先序中序只是选择时机的差别
void Morris::morris_pre(Tree *const root)
{
if(root==nullptr)
return; Tree *cur=root;
Tree *mostRight=nullptr;
while(cur!=nullptr)//当前节点不为nullptr继续遍历
{
mostRight=cur->left;
if(mostRight!=nullptr)//第二种情况:有左结点
{
//找当前节点左子树的最右结点
while(mostRight->right!=nullptr&&mostRight->right!=cur)
mostRight=mostRight->right; //<1>如果不指向当前节点(也就是指向nullptr,即第一次到来),就让它指向当前节点
if(mostRight->right!=cur)
{
mostRight->right=cur;
cout<<cur->value<<" ";
cur=cur->left;
continue;
}
else//<2>如果指向当前节点(也即第二次到来)把值置为nullptr
mostRight->right=nullptr;
}
else//一个结点没有左子树,第一次到来和第二次到来一起
cout<<cur->value<<" ";
//第一种情况:没有左结点向右走
cur=cur->right;
}
cout<<endl;
} //中序打印:如果一个节点有左子树,把左子树都处理完再打印自己
void Morris::morris_in(Tree *const root)
{
if(root==nullptr)
return; Tree *cur=root;
Tree *mostRight=nullptr;
while(cur!=nullptr)
{
mostRight=cur->left;
if(mostRight!=nullptr)
{
while(mostRight->right!=nullptr&&mostRight->right!=cur)
mostRight=mostRight->right; if(mostRight->right!=cur)
{
mostRight->right=cur;
cur=cur->left;
continue;
}
else
mostRight->right=nullptr;
}
//把打印时机放在最后一次来到此节点(如果结点没左子树第一次和第二次同时到来)
cout<<cur->value<<" "; //此节点要向右走,也就是左子树都处理完
cur=cur->right;
}
cout<<endl;
} //把打印时机放在第二次(该结点必须两次来到)来到该结点的时候,只关注能两次到来该节点的结点
//第二次到来时逆序打印左子树的右边界,打印完成之后在整个函数退出之前单独打印整个数的右边界
void Morris::morris_back(Tree *const root)
{
if(root==nullptr)
return; Tree *cur=root;
Tree *mostRight=nullptr;
while(cur!=nullptr)
{
mostRight=cur->left;
if(mostRight!=nullptr)//有左子树
{
while(mostRight->right!=nullptr&&mostRight->right!=cur)
mostRight=mostRight->right; if(mostRight->right!=cur)
{
mostRight->right=cur;
cur=cur->left;
continue;
}
else//第二次来到此节点
{
mostRight->right=nullptr;
print_edge(cur->left);
}
}
cur=cur->right;
}
print_edge(root);
cout<<endl;
}
void Morris::print_edge(Tree *const cur)
{
Tree *t=cur;
list<Tree *> list;
while(t!=nullptr)
{
list.push_back(t);
t=t->right;
}
list.reverse();
for(auto it=list.begin();it!=list.end();++it)
cout<<(*it)->value<<" ";
}
int main()
{
Tree *root=nullptr;
create_tree(&root); Morris ms;
ms.morris_pre(root); ms.morris_in(root); ms.morris_back(root);
return ;
}

Morris的更多相关文章

  1. Morris post order traversal algorithm

    Sept. 5, 2015 花时间把代码读明白, 比光看书强. 动手写代码, 改代码,  兴趣是最好的老师. 多记几个例子, 增加情趣. 举个例子关于中序遍历,            4       ...

  2. Morris.js和flot绘制折线图的比较

    [文章摘要] 最近用开源的AdminLTE做框架感觉效果特别好,其针对图表库Morris.js和flot都提供了不错的支持,也都提供了这两者的例子.不过Morris.js是基于Raphael.js来的 ...

  3. 一款免费的js图表工具--morris

    前段时间需要使用免费的图表工具做报表,同事提及了一款图表工具morris.官方网站: http://www.oesmith.co.uk/morris.js/ 该插件遵循BSD协议,可以用于商业软件,也 ...

  4. [Leetcode][JAVA] Recover Binary Search Tree (Morris Inorder Traversal)

    Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...

  5. 二叉树的遍历(递归,迭代,Morris遍历)

    二叉树的三种遍历方法: 先序,中序,后序,这三种遍历方式每一个都可以用递归,迭代,Morris三种形式实现,其中Morris效率最高,空间复杂度为O(1). 主要参考博客: 二叉树的遍历(递归,迭代, ...

  6. morris的用法

    參數選項說明: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 ...

  7. 二叉树的遍历(递归,迭代,Morris遍历)

    二叉树的遍历: 先序,中序,后序: 二叉树的遍历有三种常见的方法, 最简单的实现就是递归调用, 另外就是飞递归的迭代调用, 最后还有O(1)空间的morris遍历: 二叉树的结构定义: struct ...

  8. 数据结构《13》----二叉树 Morris 前序遍历

    三种二叉树的后序遍历的方法: 1. 递归                      O(n) 时间复杂度, O(n) 空间复杂度 2. 迭代(用栈)       O(n) 时间复杂度, O(n) 空间 ...

  9. 数据结构《10》----二叉树 Morris 中序遍历

    无论是二叉树的中序遍历还是用 stack 模拟递归, 都需要 O(n)的空间复杂度. Morris 遍历是一种 常数空间 的遍历方法,其本质是 线索二叉树(Threaded Binary Tree), ...

  10. 时间作为横轴的图表(morris.js)超越昨天的自己系列(8)

    超越昨天的自己系列(8) morris.js的官网有详细的例子:http://www.oesmith.co.uk/morris.js/ 特别注意它的依赖: <link rel="sty ...

随机推荐

  1. 1.6socket服务器传送文件--gui窗口

    socket服务器代码 import sys,os,time,_thread from socket import * class Server(object): def __init__(self) ...

  2. 在MATLAB中安装MinGW-w64 C/C++ 编译器的方法

    reference:http://blog.sina.com.cn/s/blog_167bbdec10102x113.html 在MATLAB中编译C/C++ 文件时出现以下情况: 说明缺少MinGW ...

  3. 1085 PAT单位排行

    每次 PAT 考试结束后,考试中心都会发布一个考生单位排行榜.本题就请你实现这个功能. 输入格式: 输入第一行给出一个正整数 N(≤10​^5​​),即考生人数.随后 N 行,每行按下列格式给出一个考 ...

  4. sptring boot 修改默认Banner

    一.自定义banner 启动Spring Boot项目时,在控制台或日志中会默认显示一个Banner,如图所示: 在我们的项目中更希望使用自己的Banner,这样看起来更帅写,但是这对于程序员来说并不 ...

  5. 软件工程项目程序:WC

    1:代码来源:http://yuncode.net/code/c_5087c8e4cd77190 2:Platform:Eclipse Language:Java 3:Bug:暂时没有 4. Func ...

  6. <Web Crawler><Java><thread-safe queue>

    Basic Solution The simplest way is to build a web crawler that runs on a single machine with single ...

  7. java学习笔记21(迭代器)

    java中有很多集合,内部有各种的存储的方法,取出的方法也各不相同,那么有没有一种通用的方法来取出来呢? java提供的遍历集合元素的方法有两种: 1.for-each结构(增强型for循环) 格式: ...

  8. HDU 6106 17多校6 Classes(容斥简单题)

    Problem Description The school set up three elective courses, assuming that these courses are A, B, ...

  9. Ubuntu 录制视频并制作成gif图

    最近在github中添加项目实现效果时需要用到gif图,在网上搜了一些工具和教程,会发现windows系统比ubuntu多很多,经过实际验证以下这种方法可以实现gif图的录制与制作,具体方法如下:   ...

  10. Container(容器)

    容器可以管理对象的生命周期.对象与对象之间的依赖关系,您可以使用一个配置文件(通常是XML),在上面定义好对象的名称.如何产生(Prototype 方式或Singleton 方式). 哪个对象产生之后 ...