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

目录

前言

实例

代码(有大量注释)


前言

本文只讨论前序

首先,凹入输出是要注意层次的,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;
}


版权声明:本文为weixin_50295745原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。