二叉树竖形打印、相似判断、路径

本文探讨了如何通过竖状和凹入打印方式展示二叉树,并实现二叉树的相似性判断功能。涉及InOrder2函数、嵌入式打印以及IsLike函数的实现。
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<iostream>
#include<string>
#include<vector>
#include<queue>
using namespace std;

typedef int DataType;
typedef struct Node {
	DataType data;
	struct Node *LChild;
	struct Node *RChild;
}BiTNode, *BiTree;

//竖状打印二叉树
void InOrder2(BiTree root, string &InStr)
{
	if (root != NULL) {
		InOrder2(root->LChild, InStr);
		InStr += root->data;
		InOrder2(root->RChild, InStr);
	}
}

//拓展先序创建二叉树
void CreateBiTree(BiTree *root)
{
	char ch;
	ch = getchar();
	if (ch == '.') *root = NULL;
	else {
		*root = (BiTNode *)malloc(sizeof(BiTNode));
		(*root)->data = ch;
		CreateBiTree(&((*root)->LChild));
		CreateBiTree(&((*root)->RChild));
	}
}

void PrintVertically(BiTree root)
{
	if (root == NULL) return;
	//获取二叉树中序遍历序列
	string InStr;
	InOrder2(root, InStr);

	queue<BiTNode*> q;
	q.push(root);
	while ( !q.empty() ) {
		vector<BiTNode*> cache;
		while ( !q.empty() ) {
			cache.push_back(q.front());
			q.pop();
		}
		string line = "";
		for (int i = 0; i < InStr.length(); i++)
			line += " ";
		for (auto p : cache) {
			if (p) {
				line[InStr.find(p->data)] = p->data;
				if (p->LChild)  q.push(p->LChild);
				if (p->RChild)  q.push(p->RChild);
			}
		}
		cout << line << endl;
		//把每层的结点全部放入队列中,然后取出时每层的结点全部取出
	}
}

void PrintHelp(BiTree root, string ss)
{
	if (root == NULL) return;
	ss += " ";
	PrintHelp(root->RChild, ss);
	cout << ss;
	printf("%c\n", root->data);
	PrintHelp(root->LChild, ss);
}

//二叉树凹入打印
void EmbededPrint(BiTree root)
{
	string ss = "";
	printf("\n The Embeded Result is:\n");
	PrintHelp(root, ss);
}

//二叉树相似性判断
int IsLike(BiTree bt1, BiTree bt2)
{
	if (bt1 == NULL && bt2 == NULL)
		return 1;
	else if (bt1 == NULL || bt2 == NULL)
		return 0;
	else {
		int like1 = IsLike(bt1->LChild, bt2->LChild);
		int like2 = IsLike(bt1->RChild, bt2->RChild);
		return like1 && like2;
	}
}


//求根结点到r结点的路径
//思路:可以使用后序遍历,为了能控制输出路径,使用自定义的栈而非系统隐式栈
#define STACK_SIZE 14
void FindPath(BiTree root, BiTNode * r)
{
	BiTNode *p = root, *q = NULL;
	BiTNode * pathStack[STACK_SIZE];
	int top = 0;
	while (  p!=NULL || top != 0) {
		while (p != NULL) {
			top++;
			if (top >= STACK_SIZE - 1) {
				printf("\n栈满无法继续添加元素\n");
				return;
			}
			pathStack[top] = p;
			p = p->LChild;
		}
		if (top > 0) {
			p = pathStack[top];
			if (p->RChild == NULL || p->RChild == q) {
				if (p == r) {
					for (int i = 1; i <= top; i++)
						printf("%c", pathStack[i]->data);
					printf("\n");
					return;
				}
				else {
					q = p;
					top--;//一定记得退栈
					p = NULL;
				}
			}
			else
				p = p->RChild;
		}
	}
}

int main()
{
	BiTree root = NULL,root2 = NULL;
	CreateBiTree(&root);
	PrintVertically(root);
	EmbededPrint(root);//ABE.F..CG..D...

	root2 = root;
	printf("The root and root2 are like? %d\n", IsLike(root, root2));
	printf("The root and NULL are like?%d\n", IsLike(root, NULL) );

	FindPath(root, root->LChild->RChild->RChild);//AB.D.E..FG..KE...
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值