二叉树的重建

给定二叉树的前序遍历和中序遍历,重建二叉树,并打印后序遍历的序列

#include <iostream>
using namespace std;

struct BinaryTreeNode
{
	int _value;
	BinaryTreeNode* _pleft;
	BinaryTreeNode* _pright;
};
BinaryTreeNode* ConstructCore(int* startPreorder, int* endPreorder, int* startInorder, int* endInorder);

BinaryTreeNode* Construct(int* preorder, int* inorder, int length)
{
	if (NULL == preorder || NULL == inorder || length <= 0)
		return NULL;
	return ConstructCore(preorder, preorder + length-1, inorder, inorder + length-1);
		
}

BinaryTreeNode* ConstructCore(int* startPreorder, int* endPreorder, int* startInorder, int* endInorder)
{
	int rootValue = startPreorder[0];
	BinaryTreeNode* root = new BinaryTreeNode();
	root->_value = rootValue;
	root->_pleft = NULL;
	root->_pright = NULL;

	if (startPreorder == endPreorder)
	{
		if (startInorder == endInorder&& *startPreorder == *endPreorder)
			return root;
		else
		{
			return NULL;
		}
	}
	


	int* rootInorder = startInorder;
	while (rootInorder <= endInorder&&*rootInorder != rootValue)
	{
		rootInorder++;
	}

	if (rootInorder == endInorder&&*rootInorder != rootValue)
	{
		return NULL;
	}

	int leftLength = rootInorder - startInorder;
	int* leftPreorderEnd = startPreorder + leftLength;

   if (leftLength > 0)
	{
		root->_pleft = ConstructCore(startPreorder + 1, leftPreorderEnd, startInorder, rootInorder - 1);
	}

	if (leftLength < endPreorder - startPreorder)//说明有右子树
	{
		root->_pright = ConstructCore(leftPreorderEnd + 1, endPreorder, rootInorder + 1, endInorder);
	}
	return root;
}


void endPrint(BinaryTreeNode* root)
{
	if (root == NULL)
	{
		return;
	}

	endPrint(root->_pleft);
	endPrint(root->_pright);
	cout << root->_value << " ";
	

}

//递归解决办法:
//1.先找到返回条件(算是一种特殊情况吧,本题中:startPreorder==endPreorder,startInorder==endInorder,*starPreorder==root->value. 满足这个条件即说明当前的范围只有一个数据,所以返回)
//2.找到解决办法(要重建二叉树,从根节点开始建,然后是左子树,再是右子树。要建立左子树:从根节点开始建,然后是左子树,再是右子树。要建立右子树:等左子树建立完成后,从根节点开始建,然后是左子树,再是右子树)

//测试代码:


//普通二叉树
void Test1()
{
	int Preorder[] = { 1, 2, 4, 7, 3, 5, 6, 8 };
	int Inorder[] = { 4, 7, 2, 1, 5, 3, 8, 6 };
	int length = 8;
	BinaryTreeNode* root = Construct(Preorder, Inorder,length);

	endPrint(root);

}


//只有左节点的二叉树

void Test2()
{
	int Preorder[] = { 1, 2, 3, 4, 5, 6 };
	int Inorder[] = { 6, 5, 4, 3, 2, 1 };
	int length =6;
	BinaryTreeNode* root = Construct(Preorder, Inorder, length);

	endPrint(root);
}
//只有右节点的二叉树
void Test3()
{
	int Preorder[] = { 1, 2, 3, 4, 5, 6 };
	int Inorder[] = { 1, 2, 3, 4, 5, 6 };
	int length =6;
	BinaryTreeNode* root = Construct(Preorder, Inorder, length);

	endPrint(root);
}

//完全二叉树
void Test4()
{
	int Preorder[] = { 1, 2, 4, 5, 3, 6, 7 };
	int Inorder[] = { 4, 2, 5, 1, 6, 3, 7 };
	int length = 7;
	BinaryTreeNode* root = Construct(Preorder, Inorder, length);

	endPrint(root);
}

//只有一个节点的二叉树
void Test5()
{
	int Preorder[] = { 1 };
	int Inorder[] = { 1 };
	int length = 1;
	BinaryTreeNode* root = Construct(Preorder, Inorder, length);

	endPrint(root);
}

//空
void Test6()
{
	BinaryTreeNode* root = Construct(NULL, NULL, 0);

	endPrint(root);
}

//前序序列和中序序列描述的不是一个二叉树
void Test7()
{
	int Preorder[] = { 1, 2, 4, 5, 3, 6, 7 };
	int Inorder[] = { 4, 2, 8, 1, 6, 3, 7 };
	int length = 7;
	BinaryTreeNode* root = Construct(Preorder, Inorder, length);

	endPrint(root);
}



int main()
{
	Test1();
	cout << endl;

	Test2();
	cout << endl;

	Test3();
	cout << endl;

	Test4();
	cout << endl;

	Test5();
	cout << endl;

	Test6();
	cout << endl;

	Test7();
	cout << endl;

	//Test8();

	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值