左神算法学习日记——morrish


#include "stdafx.h"

#include<algorithm>
#include<numeric>
#include<iostream>
#include<set>
#include<vector>
#include<map>
#include<stack>
#include<queue>
using namespace std;

class Node
{
public:
	int num;
	Node* left;
	Node* right;
	Node() = default;
	Node(int n)
	{
		num = n;
	}
	~Node()
	{
		queue<Node*> del;
		Node* temp;
		del.push(this);
		while (!del.empty()&&del.front())
		{
			temp = del.front();
			del.pop();
			if (temp->left)
				del.push(temp->left);
			if (temp->right)
				del.push(temp->right);
			delete temp;
		}
	}
};

void morrish(Node* node)
{
	if (!node)
		return;
	Node* cur = node;
	Node* mostright = NULL;
	while (cur)
	{
		mostright = cur->left;
		if (mostright)//如果当前结点的左孩子就不存在则向右移
		{
			while (mostright->right&&mostright->right != cur)
				mostright = mostright->right;
			if (mostright->right == NULL)//得到当前结点的最右孩子后,将最右孩子的右孩子指针指向当前结点,
                        //并将当前结点向左移,这样可以有效利用叶子结点的指针实现两次访问当前结点,进而实现遍历
			{
				mostright->right = cur;
				cur = cur->left;
			}
			else//如果第二次回到当前结点则向右移,并将指向当前结点的指针指向空
			{
				mostright->right = NULL;
				cur = cur->right;
			}
		}
		else
			cur = cur->right;
	}
}
//利用morrish先序遍历
void morrishpre(Node* node)
{
	if (!node)
		return;
	Node* cur = node;
	Node* mostright = NULL;
	while (cur)
	{
		mostright = cur->left;
		if (mostright)
		{
			while (mostright->right&&mostright->right != cur)
				mostright = mostright->right;
			if (mostright->right == NULL)
			{
				cout << cur->num<<"  ";
				mostright->right = cur;
				cur = cur->left;
			}
			else
			{
				mostright->right = NULL;
				cur = cur->right;
			}
		}
		else
		{
			cout << cur->num<<"  ";
			cur = cur->right;
		}
	}
	cout << endl;
}
//利用morrish中序遍历
void morrishmid(Node* node)
{
	if (!node)
		return;
	Node* cur = node;
	Node* mostright = NULL;
	while (cur)
	{
		mostright = cur->left;
		if (mostright)
		{
			while (mostright->right&&mostright->right != cur)
				mostright = mostright->right;
			if (mostright->right == NULL)
			{
				mostright->right = cur;
				cur = cur->left;
				continue;
			}
			else
			{
				cout << cur->num<<"  ";
				mostright->right = NULL;
				cur = cur->right;
			}
		}
		else
		{
			cout << cur->num<<"  ";
			cur = cur->right;
		}
	}
	cout << endl;
}

void behrev(Node* cur)
{
	Node* next = cur->right;
	cur->right = NULL;
	Node* temp;
	if (next)
	{
		temp = next->right;
		next->right = cur;
		cur = next;
		next = temp;
	}
	next = cur->right;
	cur->right = NULL;
	while (next)
	{
		cout << cur->num<<"  ";
		temp = next->right;
		next->right = cur;
		cur = next;
		next = temp;
	}
	cout << cur->num<<"  ";
}
//利用morrish后序遍历
void morrishbeh(Node* node)
{
	if (!node)
		return;
	Node* cur = node;
	Node* mostright = NULL;
	while (cur)
	{
		mostright = cur->left;
		if (mostright)
		{
			while (mostright->right&&mostright->right != cur)
				mostright = mostright->right;
			if (mostright->right == NULL)
			{
				mostright->right = cur;
				cur = cur->left;
				continue;
			}
			else
			{
				mostright->right = NULL;
				behrev(cur->left);
			}
		}
		cur = cur->right;
	}
	behrev(node);
	cout << endl;
}

void creat(Node* node)
{
	if (!node)
		return;
	int l = 0, r = 0;
	cin >> l >> r;
	if (l)
	{
		node->left = new Node(l);
		creat(node->left);
	}
	if (r)
	{
		node->right = new Node(r);
		creat(node->right);
	}
}

int main()
{
	Node* node = new Node(5);
	creat(node);
	morrishpre(node);
	morrishmid(node);
	morrishbeh(node);
};

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值