【数据结构-树】二叉树凹入输出,递归思路+非递归思路

目录

前言

实例

代码(有大量注释)


前言

本文只讨论前序

首先,凹入输出是要注意层次的,depth越大,空格越多。

那么就有两种思路:

  1. 每次打印节点前查询一下该节点层次(复杂度太高)
  2. 给struct加一个depth成员(不推荐,会改变树结构,现用也不是不可以)
  3. 给打印函数加depth参数(递归思路)
  4. 使用map来在不改变树结构的前提下实现2(非递归思路)

实例

input: 

ab#d##ce###

output:

a
    b
        d
    c
        e
 

学习3思路,只需要会递归遍历即可,4思路则需要非递归遍历的基础

二叉树前序、中序、后序遍历非递归写法的透彻解析_CCPP Blog-CSDN博客

代码(有大量注释)

#include<cstdio>
#include<cstdlib>
#include<stack>
#include<map>
#define WIDTH 4

typedef struct TriNode {
	char data;
	struct TriNode* leftchild;
	struct TriNode* rightchild;
	struct Trinode* parent;
}Node, * Tree;


void preCreatTree(Tree& tree)
{
	char ch = getchar();

	if (ch == '#')
	{
		tree = NULL;
		return;
	}
	if (!(tree = (Tree)malloc(sizeof(Node)))) exit(0);
	tree->data = ch;
	preCreatTree(tree->leftchild);
	preCreatTree(tree->rightchild);
}

void preConcavePrint(Tree tree, int depth) //思路3
{
	if (!tree)
		return;

	for (int i = 0; i < depth; i++)
		putchar(' ');
	putchar(tree->data);
	putchar('\n');

	preConcavePrint(tree->leftchild, depth + 4);
	preConcavePrint(tree->rightchild, depth + 4);
}

void preConcavePrintNoRecursion(Tree tree)//思路4
{
	//给每一个节点附加一个width量
	std::map<Node*, int> width;
	std::stack<Node*> S;
	Node* p = tree;
	Node* parent = tree;
	width[parent] = -4;//调整,保证根节点width==0
	while (!S.empty() || p)
	{
		if (p)//直接找到最左端
		{
			//在第一步处理根节点的时候,p和parent都是tree.
			width[p] = width[parent] + 4;//用parent构造child

			for (int i = 0; i < width[p]; i++)//利用width打印输出
				putchar(' ');
			putchar(p->data);
			putchar('\n');

			S.push(p);
			parent = p;
			p = p->leftchild;
		} 
		else //此时p==NULL,要回溯了
		{
			p = S.top();
			S.pop();
			parent = p;//注意每次找child前存一下parent,不然构造childwidth出问题
			p = p->rightchild;
		}
	}
}

int main(void)
{
	freopen("input.txt", "r", stdin);
	Tree tree;
	preCreatTree(tree);
	preConcavePrintNoRecursion(tree);
	preConcavePrint(tree, 0);


	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

亦梦亦醒乐逍遥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值