前序和中序重建二叉树、二叉排序树转换为排序的双向链表

#include<iostream>
using namespace std;
int Pre[1000]={
	492,211,123,59,34,21,12,6,4,2,0,1,3,5,9,8,7,11,10,18,16,14,13,15,17,19,20,27,23,22,
	25,24,26,31,29,28,30,33,32,50,43,39,36,35,37,38,41,40,42,47,45,44,46,49,48,57,54,52,51,53,
	55,56,58,87,80,69,62,60,61,66,64,63,65,67,68,75,73,71,70,72,74,78,77,76,79,82,81,84,83,86,
	85,107,97,94,91,89,88,90,93,92,95,96,102,100,98,99,101,105,104,103,106,114,111,109,108,110,
	112,113,117,115,116,121,119,118,120,122,167,150,135,128,125,124,127,126,133,131,130,129,132,
	134,143,139,137,136,138,141,140,142,147,145,144,146,149,148,155,152,151,153,154,159,157,156,
	158,163,161,160,162,165,164,166,183,176,172,170,168,169,171,174,173,175,178,177,180,179,181,
	182,198,189,186,184,185,188,187,193,191,190,192,196,195,194,197,206,202,200,199,201,204,203,
	205,209,207,208,210,335,281,248,229,222,215,213,212,214,218,217,216,220,219,221,226,224,223,
	225,228,227,237,234,232,231,230,233,235,236,244,241,239,238,240,242,243,246,245,247,270,259,
	253,250,249,251,252,255,254,257,256,258,263,261,260,262,266,264,265,268,267,269,276,274,272,
	271,273,275,279,278,277,280,299,286,284,282,283,285,292,290,288,287,289,291,296,294,293,295,
	298,297,315,308,305,303,301,300,302,304,306,307,311,309,310,313,312,314,324,320,317,316,319,
	318,322,321,323,329,327,326,325,328,333,331,330,332,334,421,383,353,343,340,338,336,337,339,
	342,341,348,346,344,345,347,351,350,349,352,368,362,358,355,354,356,357,360,359,361,365,364,
	363,367,366,373,370,369,372,371,378,376,375,374,377,381,379,380,382,403,393,386,385,384,390,
	388,387,389,391,392,398,396,395,394,397,400,399,401,402,413,407,405,404,406,409,408,411,410,
	412,416,414,415,418,417,420,419,441,429,426,424,422,423,425,428,427,434,432,431,430,433,437,
	436,435,439,438,440,470,456,447,444,443,442,445,446,451,449,448,450,454,452,453,455,465,460,
	458,457,459,463,462,461,464,467,466,468,469,481,478,474,472,471,473,476,475,477,479,480,486,
	483,482,485,484,490,488,487,489,491,777,651,567,537,518,507,499,497,494,493,496,495,498,503,
	501,500,502,505,504,506,512,510,508,509,511,516,514,513,515,517,526,522,520,519,521,524,523,
	525,532,529,528,527,530,531,535,534,533,536,545,540,539,538,542,541,543,544,555,551,548,547,
	546,550,549,553,552,554,560,557,556,559,558,563,562,561,565,564,566,601,584,578,573,570,568,
	569,571,572,576,574,575,577,581,579,580,582,583,595,590,587,586,585,589,588,593,591,592,594,
	599,597,596,598,600,630,618,608,606,604,603,602,605,607,611,610,609,614,613,612,616,615,617,
	624,621,619,620,622,623,626,625,628,627,629,640,637,633,631,632,635,634,636,639,638,644,642,
	641,643,648,646,645,647,649,650,690,669,660,656,653,652,654,655,658,657,659,665,662,661,663,
	664,667,666,668,684,676,673,671,670,672,675,674,682,679,677,678,681,680,683,688,686,685,687,
	689,736,715,709,701,695,692,691,693,694,697,696,699,698,700,705,703,702,704,707,706,708,711,
	710,713,712,714,723,720,717,716,718,719,721,722,729,726,725,724,728,727,732,730,731,734,733,
	735,763,750,743,740,738,737,739,742,741,746,744,745,748,747,749,757,754,752,751,753,756,755,
	761,759,758,760,762,771,769,765,764,767,766,768,770,773,772,775,774,776,886,846,808,793,784,
	782,780,779,778,781,783,788,786,785,787,790,789,791,792,799,796,795,794,797,798,802,801,800,
	805,804,803,806,807,829,818,813,811,810,809,812,815,814,816,817,823,821,819,820,822,826,825,
	824,828,827,839,833,831,830,832,836,834,835,837,838,841,840,844,842,843,845,862,853,850,848,
	847,849,851,852,857,855,854,856,859,858,860,861,873,868,865,863,864,867,866,871,869,870,872,
	878,876,875,874,877,882,880,879,881,884,883,885,956,926,905,895,890,888,887,889,893,892,891,
	894,902,899,897,896,898,900,901,904,903,914,908,907,906,911,910,909,912,913,919,916,915,917,
	918,924,921,920,923,922,925,936,929,927,928,932,930,931,934,933,935,945,940,938,937,939,943,
	941,942,944,951,949,947,946,948,950,954,952,953,955,980,965,959,958,957,963,961,960,962,964,
	973,971,969,967,966,968,970,972,976,975,974,978,977,979,990,985,982,981,984,983,987,986,988,
	989,994,992,991,993,997,996,995,999,998,
};
int In[1000]={
	0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,
	34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,
	65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,
	96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,
	120,121,122,123,124,125,126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,
	143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,162,163,164,165,
	166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,181,182,183,184,185,186,187,188,
	189,190,191,192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,208,209,210,211,
	212,213,214,215,216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,234,
	235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255,256,257,
	258,259,260,261,262,263,264,265,266,267,268,269,270,271,272,273,274,275,276,277,278,279,280,
	281,282,283,284,285,286,287,288,289,290,291,292,293,294,295,296,297,298,299,300,301,302,303,
	304,305,306,307,308,309,310,311,312,313,314,315,316,317,318,319,320,321,322,323,324,325,326,
	327,328,329,330,331,332,333,334,335,336,337,338,339,340,341,342,343,344,345,346,347,348,349,
	350,351,352,353,354,355,356,357,358,359,360,361,362,363,364,365,366,367,368,369,370,371,372,
	373,374,375,376,377,378,379,380,381,382,383,384,385,386,387,388,389,390,391,392,393,394,395,
	396,397,398,399,400,401,402,403,404,405,406,407,408,409,410,411,412,413,414,415,416,417,418,
	419,420,421,422,423,424,425,426,427,428,429,430,431,432,433,434,435,436,437,438,439,440,441,
	442,443,444,445,446,447,448,449,450,451,452,453,454,455,456,457,458,459,460,461,462,463,464,
	465,466,467,468,469,470,471,472,473,474,475,476,477,478,479,480,481,482,483,484,485,486,487,
	488,489,490,491,492,493,494,495,496,497,498,499,500,501,502,503,504,505,506,507,508,509,510,
	511,512,513,514,515,516,517,518,519,520,521,522,523,524,525,526,527,528,529,530,531,532,533,
	534,535,536,537,538,539,540,541,542,543,544,545,546,547,548,549,550,551,552,553,554,555,556,
	557,558,559,560,561,562,563,564,565,566,567,568,569,570,571,572,573,574,575,576,577,578,579,
	580,581,582,583,584,585,586,587,588,589,590,591,592,593,594,595,596,597,598,599,600,601,602,
	603,604,605,606,607,608,609,610,611,612,613,614,615,616,617,618,619,620,621,622,623,624,625,
	626,627,628,629,630,631,632,633,634,635,636,637,638,639,640,641,642,643,644,645,646,647,648,
	649,650,651,652,653,654,655,656,657,658,659,660,661,662,663,664,665,666,667,668,669,670,671,
	672,673,674,675,676,677,678,679,680,681,682,683,684,685,686,687,688,689,690,691,692,693,694,
	695,696,697,698,699,700,701,702,703,704,705,706,707,708,709,710,711,712,713,714,715,716,717,
	718,719,720,721,722,723,724,725,726,727,728,729,730,731,732,733,734,735,736,737,738,739,740,
	741,742,743,744,745,746,747,748,749,750,751,752,753,754,755,756,757,758,759,760,761,762,763,
	764,765,766,767,768,769,770,771,772,773,774,775,776,777,778,779,780,781,782,783,784,785,786,
	787,788,789,790,791,792,793,794,795,796,797,798,799,800,801,802,803,804,805,806,807,808,809,
	810,811,812,813,814,815,816,817,818,819,820,821,822,823,824,825,826,827,828,829,830,831,832,
	833,834,835,836,837,838,839,840,841,842,843,844,845,846,847,848,849,850,851,852,853,854,855,
	856,857,858,859,860,861,862,863,864,865,866,867,868,869,870,871,872,873,874,875,876,877,878,
	879,880,881,882,883,884,885,886,887,888,889,890,891,892,893,894,895,896,897,898,899,900,901,
	902,903,904,905,906,907,908,909,910,911,912,913,914,915,916,917,918,919,920,921,922,923,924,
	925,926,927,928,929,930,931,932,933,934,935,936,937,938,939,940,941,942,943,944,945,946,947,
	948,949,950,951,952,953,954,955,956,957,958,959,960,961,962,963,964,965,966,967,968,969,970,
	971,972,973,974,975,976,977,978,979,980,981,982,983,984,985,986,987,988,989,990,991,992,993,
	994,995,996,997,998,999,
};
struct BinTreeNode 
{
	int key;
	BinTreeNode *llink;
	BinTreeNode *rlink;
};
//前序遍历递归
void preOrder(BinTreeNode *tree)
{
	if(tree==NULL)
		return;
	cout<<tree->key<<',';
	preOrder(tree->llink);
	preOrder(tree->rlink);
}
//中序遍历递归
void inOrder(BinTreeNode *tree)
{
	if(tree==NULL)
		return;
	inOrder(tree->llink);
	cout<<tree->key<<',';
	inOrder(tree->rlink);
}
//后序遍历递归
void postOrder(BinTreeNode *tree)
{
	if(tree==NULL)
		return;
	postOrder(tree->llink);
	postOrder(tree->rlink);
	cout<<tree->key<<',';
}
#include<stack>
//前序遍历非递归
void nPreOrder(BinTreeNode *tree)
{
	if(tree==NULL)
		return;
	stack<BinTreeNode*> st;
	BinTreeNode *p=tree;
	st.push(p);
	while(!st.empty())
	{
		p=st.top();
		st.pop();
		if(p)
		{
			cout<<p->key<<',';
			st.push(p->rlink);
			st.push(p->llink);
		}
	}
	cout<<endl;
}
//中序遍历非递归
void nInOrder(BinTreeNode *tree)
{
	if(tree==NULL)
		return;
	stack<BinTreeNode*> st;
	BinTreeNode *p=tree;
	while(p||!st.empty())
	{
		while(p)
		{
			st.push(p);
			p=p->llink;
		}
		p=st.top();
		st.pop();
		cout<<p->key<<',';
		p=p->rlink;
	}
	cout<<endl;
}
//后序遍历非递归
void nPostOrder(BinTreeNode *tree)
{
	if(tree==NULL)
		return;
	stack<BinTreeNode*> st;
	BinTreeNode *p=tree;
	BinTreeNode *pp=NULL;
	while(p||!st.empty())
	{
		while(p)
		{
			st.push(p);
			pp=p->rlink;
			p=p->llink;
			if(p==NULL)
				p=pp;
		}
		p=st.top();
		st.pop();
		cout<<p->key<<',';
		if(!st.empty()&&st.top()->llink==p)
			p=st.top()->rlink;
		else
			p=NULL;
	}
	cout<<endl;
}
//重建二叉树
BinTreeNode *constructCore(int *preStart,int *preEnd,int *inStart,int *inEnd)
{
	int rootValue=*preStart;
	BinTreeNode *root=new BinTreeNode;
	root->key=rootValue;
	root->llink=root->rlink=NULL;

	int leftLen=0;
	while(inStart[leftLen]!=rootValue)
		leftLen++;
	if(leftLen>=1)
		root->llink=constructCore(preStart+1,preStart+leftLen,inStart,inStart+leftLen-1);
	if(*inEnd!=rootValue)
		root->rlink=constructCore(preStart+leftLen+1,preEnd,inStart+leftLen+1,inEnd);
	return root;
}
BinTreeNode *construct(int *pre,int *in,int n)
{
	if(pre==NULL||in==NULL||n<=0)
		return NULL;
	return constructCore(pre,pre+n-1,in,in+n-1);
}
#include<vector>
//重建二叉树
BinTreeNode *constructV(vector<int> pre,vector<int> in)
{
	int rootValue=pre[0];
	BinTreeNode *root=new BinTreeNode;
	root->key=rootValue;
	root->llink=root->rlink=NULL;

	int leftlen=0;
	while(in[leftlen]!=rootValue)
		++leftlen;
	if(leftlen>0)
	{
		vector<int> preleft(pre.begin()+1,pre.begin()+leftlen+1);
		vector<int> inleft(in.begin(),in.begin()+leftlen);
		root->llink=constructV(preleft,inleft);
	}
	if(in.back()!=rootValue)
	{
		vector<int> preright(pre.begin()+leftlen+1,pre.end());
		vector<int> inright(in.begin()+leftlen+1,in.end());
		root->rlink=constructV(preright,inright);
	}
	return root;
}
//二叉排序树转换为排序双向链表
BinTreeNode *tree2list(BinTreeNode *tree)
{
	if(tree==NULL)
		return NULL;
	stack<BinTreeNode*> st;
	BinTreeNode *p=tree;
	BinTreeNode *last=NULL;
	while(p||!st.empty())
	{
		while(p)
		{
			st.push(p);
			p=p->llink;
		}
		p=st.top();
		st.pop();

		if(last!=NULL)
			last->rlink=p;
		p->llink=last;
		last=p;

		p=p->rlink;
	}
	while(tree->llink!=NULL)
		tree=tree->llink;
	return tree;
}
int main()
{
	vector<int> prev(Pre,Pre+1000);
	vector<int> inv(In,In+1000);
	BinTreeNode *tree=constructV(prev,inv);         //重建二叉树
	preOrder(tree);                                 
	cout<<endl;
	nPreOrder(tree);
	inOrder(tree);
	cout<<endl;
	nInOrder(tree);
	postOrder(tree);
	cout<<endl;
	nPostOrder(tree);

	tree=tree2list(tree);                          //二叉排序树转换为排序双向链表
	BinTreeNode *p=tree;
	while(p!=NULL)                                 //打印双向链表
	{
		cout<<p->key<<' ';
		p=p->rlink;
	}
	p=tree;
	while(p)                                       //销毁双向链表
	{
		tree=p->rlink;
		delete p;
		p=tree;
	}
	return 0;	
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值