Morris
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的更多相关文章
- Morris post order traversal algorithm
Sept. 5, 2015 花时间把代码读明白, 比光看书强. 动手写代码, 改代码, 兴趣是最好的老师. 多记几个例子, 增加情趣. 举个例子关于中序遍历, 4 ...
- Morris.js和flot绘制折线图的比较
[文章摘要] 最近用开源的AdminLTE做框架感觉效果特别好,其针对图表库Morris.js和flot都提供了不错的支持,也都提供了这两者的例子.不过Morris.js是基于Raphael.js来的 ...
- 一款免费的js图表工具--morris
前段时间需要使用免费的图表工具做报表,同事提及了一款图表工具morris.官方网站: http://www.oesmith.co.uk/morris.js/ 该插件遵循BSD协议,可以用于商业软件,也 ...
- [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 ...
- 二叉树的遍历(递归,迭代,Morris遍历)
二叉树的三种遍历方法: 先序,中序,后序,这三种遍历方式每一个都可以用递归,迭代,Morris三种形式实现,其中Morris效率最高,空间复杂度为O(1). 主要参考博客: 二叉树的遍历(递归,迭代, ...
- 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 ...
- 二叉树的遍历(递归,迭代,Morris遍历)
二叉树的遍历: 先序,中序,后序: 二叉树的遍历有三种常见的方法, 最简单的实现就是递归调用, 另外就是飞递归的迭代调用, 最后还有O(1)空间的morris遍历: 二叉树的结构定义: struct ...
- 数据结构《13》----二叉树 Morris 前序遍历
三种二叉树的后序遍历的方法: 1. 递归 O(n) 时间复杂度, O(n) 空间复杂度 2. 迭代(用栈) O(n) 时间复杂度, O(n) 空间 ...
- 数据结构《10》----二叉树 Morris 中序遍历
无论是二叉树的中序遍历还是用 stack 模拟递归, 都需要 O(n)的空间复杂度. Morris 遍历是一种 常数空间 的遍历方法,其本质是 线索二叉树(Threaded Binary Tree), ...
- 时间作为横轴的图表(morris.js)超越昨天的自己系列(8)
超越昨天的自己系列(8) morris.js的官网有详细的例子:http://www.oesmith.co.uk/morris.js/ 特别注意它的依赖: <link rel="sty ...
随机推荐
- Java实验2
1.给定一组字符,编程输出里面数值最大者. package experiment; import java.util.Arrays; public class ShenYue { public sta ...
- Android开发 ---基本UI组件2:图像按钮、单选按钮监听、多选按钮监听、开关
Android开发 ---基本UI组件2 1.activity_main.xml 描述: 定义一个按钮 <?xml version="1.0" encoding=" ...
- mySql单列索引与联合索引的区别
引自https://my.oschina.net/857359351/blog/658668 第一张表gift和索引为联合索引,如图: 第二张表gift2为单列索引,如图: 下面开始进行测试: 相同的 ...
- Appium Desktop 介绍及使用
一.AppiumDesktop介绍 1.Appium-server的图形界面.可以设置选项.启动/停止服务器.查看日志等功能:且无须提前安装Node / NPM,因为Node运行时直接与Appium ...
- 在使用Idea配置jQuery的问题
今天使用idea中引入jQuery代码时,发生的几个错误,时刻提醒 1.jQuery的驱动包要放置在web目录下 2.引入jQuery的驱动包时,语句格式为<script></scr ...
- 漫步Java------接口
接口 一.定义 具有相同行为(方法),但是不相关的类 二.特点 只是提供方法,不定义方法的具体实现. 一个类只能继承一个父类,但是接口却可以继承多个接口. 接口是一个引用类型的变量 接口没有构造方法, ...
- golang切片类型
切片slice 其本身并不是数组,它指向底层的数组 作为变长数组的替代方案,可以关联底层数组的局部或全部 为引用类型 可以直接创建或从底层数组获取生成 使用len()获取元素个数,cap()获取容量 ...
- Python 字符串操作方法大全
Python 字符串操作方法大全 1.去空格及特殊符号复制代码代码如下:s.strip().lstrip().rstrip(',') 2.复制字符串复制代码代码如下:#strcpy(sStr1,sSt ...
- day12作业
- 【转载】 Pytorch中的学习率调整lr_scheduler,ReduceLROnPlateau
原文地址: https://blog.csdn.net/happyday_d/article/details/85267561 ------------------------------------ ...