二叉树实验(两种链式存储与各种遍历)

采用c++

//测试用例:AB##CD##EF##G##
#include<stdio.h>
#include<stack>
#include <vector>
#include <queue>

using namespace std;
 
typedef struct Node {
	char data;
	struct Node* lchild;
	struct Node* rchild;
}TreeNode, *Tree;

void creatTree(Tree* T);
void xianxu(Tree T);
void zhongxu(Tree T);
void houxu(Tree T);
void zhongxu2(Tree T);
void qianxu2(Tree T);
void houxu2(Tree T);
void cengci(Tree T);
void cengci2(Tree T);
void aixing();
void cengcilianshi(Tree* T);
Tree createTree2(int n, int iq, int iz);

int qian[1000];
int zhong[1000];

int main(void) 
{
	Tree T = NULL;
	int choice;

	printf("由题目要求得,需要创建链式存储方法需要两个方法,请选择:\n\n1.前序构造,并使用#代表没有(左右)孩子,例如:AB##CD##EF##G##  \n\n2.前序和中序构造,例如:\n7\nBADCFEG\nABCDEFG\n\n请输入你的选择:");
	scanf("%d", &choice);
	switch (choice)
	{
	case 1:
		getchar();
		printf("请按照前序输入一个二叉树(无左右孩子使用#表示):");
		creatTree(&T);
		break;
	case 2:
		int n;
		char t;
		printf("\n采用前序和中序构造链式存储(请输入一个二叉树的中序与前序):\n");
		printf("请输入个数:");
		scanf("%d", &n);
		printf("中序:");
		getchar();
		for (int i = 0; i < n; i++)
		{
			scanf("%c", &t);
			zhong[i] = t-'A'+1;
		}
		printf("\n前序:");
		getchar();
		for (int i = 0; i < n; i++)
		{
			scanf("%c", &t);
			qian[i] = t - 'A'+1;
		}
		T = createTree2(n, 0, 0);
		break;
	default:
		printf("\n输入错误");
		exit(0);
	}
	printf("\n\n---------------");
	printf("--------------");
	printf("\n|   递归算法   |");
	printf(" 非递归算法  |");
	printf("\n---------------");
	printf("--------------");
	printf("\n|前序: |");
	xianxu(T);
	printf("|前序:|");
	qianxu2(T);
	printf("|\n|中序: |");
	zhongxu(T);
	printf("|中序:|");
	zhongxu2(T);
	printf("|\n|后续: |");
	houxu(T);
	printf("|后续:|");
	houxu2(T);
	printf("|\n-----------------------------");
	printf("\n|   层次遍历   ");
	printf("|\n---------------");
	printf("\n|递归  |");
	cengci(T);
	printf("|\n|非递归|");
	cengci2(T);
	printf("|\n---------------");
	aixing();
	return 0;
}


//构造二叉树链式存储(先序)
void creatTree(Tree* T) 
{
	char c;

	scanf("%c", &c);
	if (c == '#')
	{
		T = NULL;
		return;
	}


	if ((*T) == NULL)
		(*T) = (Tree)malloc(sizeof(TreeNode));
	(*T)->data = c;
	(*T)->lchild = NULL;
	(*T)->rchild = NULL;
	creatTree(&((*T)->lchild));
	creatTree(&((*T)->rchild));
}

void xianxu(Tree T)
{
	if (T != NULL)
	{
		printf("%c", T->data);
		xianxu(T->lchild);
		xianxu(T->rchild);
	}
	
}

void zhongxu(Tree T)
{
	if (T != NULL)
	{
		zhongxu(T->lchild);
		printf("%c", T->data);
		zhongxu(T->rchild);
	}
}

void houxu(Tree T)
{
	if (T != NULL)
	{
		houxu(T->lchild);
		houxu(T->rchild);
		printf("%c", T->data);
	}
}


void qianxu2(Tree T)
{
	stack<Tree>s;
	Tree p;
	s.push(T);
	while (!s.empty())
	{
		while ((p = s.top()))
		{
			printf("%c", p->data);
			s.push(p->lchild);
		}
		s.pop();
		if (!s.empty())
		{
			p = s.top();
			s.pop();
			s.push(p->rchild);
		}
	}
}

void zhongxu2(Tree T)
{
	stack<Tree>s;
	Tree p;
	s.push(T);
	while (!s.empty())
	{
		while ((p = s.top())) s.push(p->lchild);
		s.pop();
		if (!s.empty())
		{
			p = s.top();
			s.pop();
			printf("%c", p->data);
			s.push(p->rchild);
		}
	}
}

void houxu2(Tree root) {
	Tree p = root;
	Tree r = NULL;
	stack<Tree> s;
	while (p || !s.empty()) {
		if (p) {//走到最左边
			s.push(p);
			p = p->lchild;
		}
		else {
			p = s.top();
			if (p->rchild && p->rchild != r)//右子树存在,未被访问
				p = p->rchild;
			else {
				s.pop();
				printf("%c", p->data);
				r = p;//记录最近访问过的节点
				p = NULL;//节点访问完后,重置p指针
			}
		}//else
	}//while
}

//层次递归
void cengci(Tree T)
{
	queue<Tree> q;
	Tree p;

	q.push(T);
	while (!q.empty())
	{
		p = q.front();
		q.pop();
		printf("%c", p->data);
		if (p->lchild != NULL)
			q.push(p->lchild);
		if (p->rchild != NULL)
			q.push(p->rchild);
	}
}

void cengci2(Tree T)
{
	queue<Tree> q;
	Tree p;
	
	q.push(T);
	while (!q.empty())
	{
		p = q.front();
		q.pop();
		printf("%c", p->data);
		if (p->lchild != NULL)
			q.push(p->lchild);
		if (p->rchild != NULL)
			q.push(p->rchild);
	}
}

void aixing()
{
	float a, x, y;

	for (y = 1.5f; y > -1.5f; y -= 0.1f)

	{

		for (x = -1.5f; x < 1.5f; x += 0.06f)

		{

			a = x * x + y * y - 1;
			char ch[3];
			if (a * a * a - x * x * y * y * y <= 0.0f)
			{
				strcpy(ch, "月");
			}
			else
				strcpy(ch, "  ");

			printf("%s", ch);

		}

		printf("\n");

	}
}

//中序前序构造链式存储
Tree createTree2(int n, int iq, int iz)
{
	if (n <= 0)
		return NULL;
	Tree T = (Tree)malloc(sizeof(TreeNode));
	T->lchild = NULL;
	T->rchild = NULL;
	int i = 0;

	T->data = char(qian[iq]-1+'A');
	for (i = 0; zhong[i + iz] != qian[iq]; i++);
	T->lchild = createTree2(i, iq + 1, iz);
	T->rchild = createTree2(n - i - 1, iq + i + 1, iz + i + 1);


	return T;
}

运行测试:
在这里插入图片描述

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

I Am Rex

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

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

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

打赏作者

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

抵扣说明:

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

余额充值