根据二叉树前序中序输出后序和根据二叉树后序中序生成前序

本文介绍如何根据前序、中序及后序遍历结果构造二叉树,并实现相应的遍历输出。通过示例代码展示了二叉树的创建过程,包括查找元素索引、递归构建左右子树等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <iostream>  
#include <cstring>  
#define MAX 50 
using namespace std;
typedef char Element;
typedef struct _stBinaryTree
{
	Element data;//数据  
	struct _stBinaryTree *Lchild;//左孩子  
	struct _stBinaryTree *Rchild;//右孩子  
}stBinaryTree;
 
 
//查找某一元素在中序输出结果中的索引  
int indexIn_inOrder(Element num, Element *array, int len);  
 
//根据前序中序结果生成二叉树
stBinaryTree * makeTree_accordPreInOrder(Element *pre, Element *center, int len);
 
//根据后序中序结果生成二叉树
stBinaryTree * makeTree_accordPostInOrder(Element *post, Element *center, int len);
 
//后序遍历输出
void postOrderTravesal(stBinaryTree * root);
 
//前序遍历输出
void preOrderTravesal(stBinaryTree * root);
 
//销毁二叉树
void destroyTree(stBinaryTree * root);
 
int main(void)
{
	//根据前序中序结果生成二叉树,后序输出
	cout << "input preorder & inorder traversal data:\n";
	Element *preorder = new Element[MAX];//后序数组  
	Element *inorder = new Element[MAX];//中序前序数组  
	cin >> preorder;    //先输入前序
	cin >> inorder;        //再输入中序
	stBinaryTree * root1 = makeTree_accordPreInOrder(preorder, inorder, strlen(inorder));//由于本例结构元素为char,所以可以用strlen来获元素个数,其他情况可机变
	postOrderTravesal(root1);
	cout << endl;
	//释放内存
	delete[] preorder;
	delete[] inorder;
	destroyTree(root1);
 
	//根据后序中序结果生成二叉树,前序输出
	cout << "input postorder & inorder traversal data:\n";
	Element *postorder = new Element[MAX];//后序数组  
	Element *inorder2 = new Element[MAX];//中序数组  
	cin >> postorder;    //先输入后序
	cin >> inorder2;        //再输入中序
	stBinaryTree * root2 = makeTree_accordPostInOrder(postorder, inorder2, strlen(inorder2));//由于本例结构元素为char,所以可以用strlen来获元素个数,其他情况可机变
	preOrderTravesal(root2);
	cout << endl;
	//释放内存
	delete[] postorder;	
	delete[] inorder2;
	destroyTree(root2);
	return 0;
}
 
//查找某元素在中序结果中的索引
int indexIn_inOrder(Element num, Element *array, int len)
{
	for (int i = 0; i<len; i++)
		if (array[i] == num)
			return i;
	return -1;//没有找到  
}                 
 
//根据前序中序结果生成二叉树
stBinaryTree * makeTree_accordPreInOrder(Element *pre, Element *center, int len)
{
	if (len <= 0)
		return NULL;
	stBinaryTree *temp = new stBinaryTree;
	temp->data = pre[0];		//前序第一个元素即为根元素
	int index = indexIn_inOrder(temp->data, center, len);
	//遍历左孩子 (前序结果中,从第1个元素开始到第index个元素都是左子树(从0计数))
	if (index != -1)
	{
 
		temp->Lchild = makeTree_accordPreInOrder(pre + 1, center, index);
		//遍历右孩子	 (前序结果中,从第index+1个元素开始到第Len-1个元素都是右子树(从0计数)) 
		temp->Rchild = makeTree_accordPreInOrder(pre + index + 1, center + index + 1, len - index - 1);
		return temp;
	}
	else
	{
		cout << " Element '" << temp->data << "' is not found in inorder result!\n";
		return NULL;
	}
}
 
//根据后序中序结果生成二叉树
stBinaryTree * makeTree_accordPostInOrder(Element *post, Element *center, int len)
{
	if (len <= 0)
		return NULL;
	stBinaryTree *temp = new stBinaryTree;
	temp->data = post[len - 1];		//后序最后一个元素即为根元素
	int index = indexIn_inOrder(temp->data, center, len);
	//遍历左孩子 (后序结果中,从第0个元素开始到第index-1个元素都是左子树)
	if (index != -1)
	{
		temp->Lchild = makeTree_accordPostInOrder(post, center, index);
		//遍历右孩子	 (后序结果中,从第index个元素开始到第Len-2个元素都是右子树(从0计数)) 
		temp->Rchild = makeTree_accordPostInOrder(post + index, center + index + 1, len - index - 1);
		return temp;
	}
	else
	{
		cout << " Element '" << temp->data << "' is not found in inorder result!\n";
		return NULL;
	}
}
 
 
//后序遍历输出
void postOrderTravesal(stBinaryTree * root)
{
	if (root != NULL)
	{
		postOrderTravesal(root->Lchild);
		postOrderTravesal(root->Rchild);
		cout << root->data;
	}
}
 
 
//前序遍历输出
void preOrderTravesal(stBinaryTree * root)
{
	if (root != NULL)
	{
		cout << root->data;
		preOrderTravesal(root->Lchild);
		preOrderTravesal(root->Rchild);
	}
}
 
//销毁二叉树
void destroyTree(stBinaryTree * root)
{
	if (root)
	{
		stBinaryTree* tmpLeft = NULL, *tmpRight = NULL;
		if (root->Lchild)
			tmpLeft = root->Lchild;
		if (root->Rchild)
			tmpRight = root->Rchild;
		delete root;
		destroyTree(tmpLeft);
		destroyTree(tmpRight);
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值