【数据结构】(十)二叉树的(前中后)序遍历, 算法+代码

【数据结构】(十)二叉树的遍历

(一)说明:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
中序遍历左中右,有左子树就一直访问下去,直到没有返回根,然后右子树,之后一层层向上回溯
在这里插入图片描述
(二)前序遍历详解:
相关递归算法详解:

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
然后在返回函数时,依次将系统栈的结点依次弹出即可。
最后得到输出结果。
前序遍历代码:


                           //****************————《二叉树前序遍历》————****************//
#include <iostream>
using namespace std;
#include<string>
#define _CRT_SECURE_NO_WARNINGS


struct Binary
{
	char ch;
	struct Binary* lchild;
	struct Binary* rchild;
};

void DiGui(struct Binary* N)   //N可以代表任意结点
{
	if (N == 0)
	{
		return;
	}
	else
	{
		cout << N->ch << " ";
		DiGui(N->lchild);
		DiGui(N->rchild);
	}
}

void output()
{
	Binary AN = { 'A',NULL,NULL };
	Binary BN = { 'B',NULL,NULL };
	Binary CN = { 'C',NULL,NULL };
	Binary DN = { 'D',NULL,NULL };
	Binary EN = { 'E',NULL,NULL };
	Binary FN = { 'F',NULL,NULL };
	Binary GN = { 'G',NULL,NULL };
	Binary HN = { 'H',NULL,NULL };
	AN.lchild = &BN;
	AN.rchild = &CN;
	BN.lchild = &DN;
	DN.rchild = &HN;
	CN.lchild = &EN;
	CN.rchild = &FN;
	FN.lchild = &GN;

	DiGui(&AN);   //递归遍历
}

int main()
{
	output();
	system("pause");
	return 0;
}

在这里插入图片描述
中序遍历代码:

           //****************————《二叉树中序遍历》————****************//
#include <iostream>
using namespace std;
#include<string>
#define _CRT_SECURE_NO_WARNINGS


struct Binary
{
	char ch;
	struct Binary* lchild;
	struct Binary* rchild;
};

void DiGui(struct Binary* N)   //N可以代表任意结点
{
	if (N == 0)
	{
		return;
	}
	else
	{	
		DiGui(N->lchild);
		cout << N->ch << " ";
		DiGui(N->rchild);
	}
}

void output()
{
	Binary AN = { 'A',NULL,NULL };
	Binary BN = { 'B',NULL,NULL };
	Binary CN = { 'C',NULL,NULL };
	Binary DN = { 'D',NULL,NULL };
	Binary EN = { 'E',NULL,NULL };
	Binary FN = { 'F',NULL,NULL };
	Binary GN = { 'G',NULL,NULL };
	Binary HN = { 'H',NULL,NULL };
	AN.lchild = &BN;
	AN.rchild = &CN;
	BN.lchild = &DN;
	DN.rchild = &HN;
	CN.lchild = &EN;
	CN.rchild = &FN;
	FN.lchild = &GN;

	DiGui(&AN);   //递归遍历
}

int main()
{
	output();
	system("pause");
	return 0;
}

在这里插入图片描述
后序遍历:

	   //****************————《二叉树后序遍历》————****************//
#include <iostream>
using namespace std;
#include<string>
#define _CRT_SECURE_NO_WARNINGS


struct Binary
{
	char ch;
	struct Binary* lchild;
	struct Binary* rchild;
};

void DiGui(struct Binary* N)   //N可以代表任意结点
{
	if (N == 0)
	{
		return;
	}
	else
	{
		DiGui(N->lchild);
		DiGui(N->rchild);
		cout << N->ch << " ";
	}
}

void output()
{
	Binary AN = { 'A',NULL,NULL };
	Binary BN = { 'B',NULL,NULL };
	Binary CN = { 'C',NULL,NULL };
	Binary DN = { 'D',NULL,NULL };
	Binary EN = { 'E',NULL,NULL };
	Binary FN = { 'F',NULL,NULL };
	Binary GN = { 'G',NULL,NULL };
	Binary HN = { 'H',NULL,NULL };
	AN.lchild = &BN;
	AN.rchild = &CN;
	BN.lchild = &DN;
	DN.rchild = &HN;
	CN.lchild = &EN;
	CN.rchild = &FN;
	FN.lchild = &GN;

	DiGui(&AN);   //递归遍历
}

int main()
{
	output();
	system("pause");
	return 0;
}

在这里插入图片描述

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值