编程之美3.8 3.9 3.10二叉树问题实现

本文介绍了一种通过前序和中序遍历构建二叉树的方法,并实现了寻找二叉树中节点最大距离的算法。此外,还提供了层序打印及反序层序打印等实用功能。

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

// 编程之美之求二叉树节点的最大距离.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
#include <queue>//for the use of the layer print
#include <vector>
using namespace std;
#define N 10
struct TNode{
	char chValue;
	TNode *leftChild;
	TNode *rightChild;
	int nMaxLeft;
	int nMaxRight;
};
TNode* rebuildTree(char *preOrder,int start1,char *midOrder,int start2,int n)//根据前序和中序建立二叉树 长度为N
{
	TNode *root;
	if(n==0) return NULL;
	root = new TNode;
	int pivet;
	char chroot;
	chroot=preOrder[start1];
	root->chValue = chroot;
	root->nMaxLeft=0;
	root->nMaxRight=0;
	for(int i=start2;i<start2+n;i++)//include start1 
	{
		if(midOrder[i]==chroot) { pivet = i;break;}
	}
	if(pivet-start2>0)
		root->leftChild=rebuildTree(preOrder,start1+1,midOrder,start2,pivet-start2);
	else root->leftChild = NULL;
	if(n-pivet+start2-1>0)//pivet-start2 is the half number,
		root->rightChild=rebuildTree(preOrder,start1+1+pivet-start2,midOrder,pivet+1,n-pivet+start2-1);
	else root->rightChild = NULL;
	return root;
}
void layerprint(TNode *root)
{
	queue<TNode*> que;// = new queue<TNode>();
	TNode *h;
	//int layer=0;
	//int cur1=0,cur2=0;//current layer in the queue
	h=root;
	que.push(h);//initialize the queue with the root of the tree
	while(que.empty()==0)
	{
		int sz = que.size();//size is the number of the current layer
		for(int i=0;i<sz;i++)
		{
			TNode *p=que.front();
			cout<<p->chValue<<" ";
			que.pop();
			if(p->leftChild!=NULL) que.push(p->leftChild);
			if(p->rightChild!=NULL) que.push(p->rightChild);
		}
		cout<<endl;
	}
}
void layerprintreverse(TNode *root)//use a array record the number of each layer
{
	vector<TNode*> vecnodes;//because there is no pop operation, and the need of get the values of serial elements,so the queue is inconvinient,vector is the better choice
	TNode *h,*p;
	vector<int> layer;//used for the recording the number of each layer
	h=root;
	//size_t i=1;
	int nodes=1;//present the current position in the vecnodes
	vecnodes.push_back(h);//initialize the queue with the root of the tree
	int num=1;//the ending condition is that all the array elements are puted into the queue,as for that, we must know the number of the tree, no matter how,at least we need to know the deepth of the tree,except we use the vector that is dynamicly increased
	//we can know that the current layer is null only if the the last layer is null, so when the number of the current layer is zero,the troop ends.
	layer.push_back(num);
	while(num>0)
	{
		int numi=0;
		int lastnum = layer.back();//[i-1];//i presents the current layer
		if(lastnum==0)break;
		nodes=vecnodes.size();
		for(int j=nodes-lastnum;j<nodes;j++)
		{
			p=vecnodes[j];
			if(p->leftChild!=NULL) {vecnodes.push_back(p->leftChild);numi++;}
			if(p->rightChild!=NULL) {vecnodes.push_back(p->rightChild);numi++;}
		}
		num=numi;//the number of the current layer
		if (num!=0)
		{
			layer.push_back(num);
			//i++;
		}
	}
	while(layer.empty()==0)
	{
		int laynum=layer.back();
		layer.pop_back();
		int siz=vecnodes.size();
		for(int i=siz-laynum;i<siz;i++)
			cout<<vecnodes[i]->chValue<<" ";
		cout<<endl;
		for(int i=siz-laynum;i<siz;i++)
			vecnodes.pop_back();
	}
}
void preprint(TNode *root)
{
	if(root!=NULL)
	{
		cout<<root->chValue<<" ";
		preprint(root->leftChild);
		preprint(root->rightChild);
	}
}
int findMax(TNode* root,int &curmax)//return the height of the tree, at the same time judge the current longest distance which through the current root so that we can get the longest distance within one loop
{
	TNode *p=root;
	//int curmax=0;
	if(p->leftChild==NULL&&p->rightChild==NULL)
		return 0;
	if(p->leftChild!=NULL)
		p->nMaxLeft = findMax(p->leftChild,curmax) + 1;//defaut value is zero
	if(p->rightChild!=NULL)
		p->nMaxRight = findMax(p->rightChild,curmax) + 1;

	if(curmax<p->nMaxLeft + p->nMaxRight)
		curmax = p->nMaxLeft + p->nMaxRight;
	return max(p->nMaxLeft,p->nMaxRight);
}
int _tmain(int argc, _TCHAR* argv[])
{
	char preOrder[N],midOrder[N];
	int m;
	cout<<"input preorder"<<endl;
	for(int i=0;i<N;i++)
		cin>>preOrder[i];
	cout<<"input midorder"<<endl;
	for(int i=0;i<N;i++)
		cin>>midOrder[i];
	TNode *root;
	root=rebuildTree(preOrder,0,midOrder,0,N);
	cout<<endl<<" root value is "<<root->chValue<<endl;
	preprint(root);
	cout<<endl<<" next is layprint"<<endl;
	layerprint(root);
	cout<<endl<<" next is layprintreverse"<<endl;
	layerprintreverse(root);
	cout<<endl<<" next is find the distance"<<endl;
	findMax(root,m);
	cout<<"the longest distance is "<<m<<endl;
	system("pause");
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值