【数据结构】树——前序中序推后序、中序后序推前序,二叉平衡树

《【数据结构】树——前序中序推后序、中序后序推前序,二叉平衡树》

#include<windows.h>
#include<assert.h>
#include<limits.h>
#include<iostream>
#include<vector>
#include<time.h>
using namespace std;
#define END -1

typedef int ElemType;

typedef struct BtNode
{
	BtNode *leftchild;
	BtNode *rightchild;
	ElemType data;
}BtNode,*BinaryTree;

BtNode* BuyNode()
{
	BtNode *s = (BtNode*)malloc(sizeof(BtNode));
	if(s == NULL)
	{
		exit(1);
	}
	memset(s,0,sizeof(BtNode));
	return s;
}

void FreeNode(BtNode *s)
{
	free(s);
}

///////////////////////////////////////////////////

int FindPos(ElemType *is,ElemType x,int n)
{
	int pos = -1;
	for(int i = 0;i < n;i++)
	{
		if(is[i] == x)
		{
			pos = i;
			break;
		}
	}
	return pos;
}

void PreOrder(BtNode *p)  //前序遍历顺序
{
	if(p != NULL)
	{
		cout<<p->data<<" ";
		PreOrder(p->leftchild);
		PreOrder(p->rightchild);
	}
}

void InOrder(BtNode *p)  //中序遍历顺序
{
	if(p != NULL)
	{
		InOrder(p->leftchild);
		cout<<p->data<<" ";
		InOrder(p->rightchild);
	}
}

void PastOrder(BtNode *p)  //后序遍历顺序
{
	if(p != NULL)
	{
		PastOrder(p->leftchild);
		PastOrder(p->rightchild);
		cout<<p->data<<" ";
	}
}

BtNode *CreatPI(ElemType *ps,ElemType *is,int n)  //前序+中序-->后序
{
	BtNode *s = NULL;  
	if(n > 0)
	{
		s = BuyNode();
		s->data = ps[0];
		int pos = FindPos(is,ps[0],n);
		if(pos == -1)
		{
			exit(-1);
		}
		s->leftchild = CreatPI(ps+1,is,pos);
		s->rightchild = CreatPI(ps+pos+1,is+pos+1,n-pos-1);
	}
	return s;
}

BtNode *CreatTreePI(ElemType *ps,ElemType *is,int n)  //前序+中序-->后序
{
	if(ps == NULL || is == NULL || n < 1)
	{
		return NULL;
	}
	else
		return CreatPI(ps,is,n);
}

BtNode *CreatIL(ElemType *is,ElemType *ls,int n)  //中序+后序-->前序
{
	BtNode *p = NULL;
	if(n > 0)
	{
		p = BuyNode();
		p->data = ls[n-1];
		int pos = FindPos(is,ls[n-1],n);
		p->leftchild = CreatIL(is,ls,pos);
		p->rightchild = CreatIL(is+pos+1,ls+pos,n-pos-1);
	}
	return p;
}

BtNode *CreatTreeIL(ElemType *is,ElemType *ls,int n)  //中序+后序-->前序
{
	if(is == NULL || ls == NULL || n < 1)
	{
		return NULL;
	}
	else 
		return CreatIL(is,ls,n);
}

///////////////////////////////////////////////////
void InOrderl_Ar(ElemType *ar,int i,int n)  //构建二叉树数组输出(方法1)
{
	if(i<n && ar[i] != END )
	{
		InOrderl_Ar(ar,i*2+1,n);// left
		cout<<ar[i]<<" ";
		InOrderl_Ar(ar,i*2+2,n); // right
	}
}
void InOrder_Ar(ElemType *ar,int n)
{
	if(NULL == ar || n < 1)
		return;
	InOrderl_Ar(ar,0,n);
	cout<<endl;
}

BtNode *CreateAr(ElemType *ar,int i,int n)  //构建二叉树链表输出(方法2)
{
	BtNode *s = NULL;
	if(i < n && ar[i] != END)
	{
		s = BuyNode();
		s->data = ar[i];
		s->leftchild  = CreateAr(ar,2*i+1,n);
		s->rightchild = CreateAr(ar,2*i+2,n);
	}
	return s;
}
BtNode *CreateTreeAr(ElemType *ar,int n)
{
	if(ar == NULL || n < 1)
		return NULL;
	else
		return CreateAr(ar,0,n);
}

void LinkMakeAr(BtNode *ptr,ElemType *buff,int i)  //构建二叉树数组输出(方法3)
{
	if(ptr != NULL)
	{
		buff[i] = ptr->data;
		LinkMakeAr(ptr->leftchild,buff,2*i+1);
		LinkMakeAr(ptr->rightchild,buff,2*i+2);
	}

}
void LinkCreateAr(BtNode *root,ElemType *buff,int n)
{
	if(root == NULL || buff == NULL)
	{
		return ;
	}
	for(int i = 0;i < n;i++)
	{
		buff[i] = END;
	}
	LinkMakeAr(root,buff,0);
}

BtNode *CreateBin(ElemType *ar,int left,int right) //利用二分查找构建平衡二叉树
{                                  //平衡二叉树:左、右子树深度之差绝对值不大于1
	BtNode *s = NULL;
	if(ar != NULL)
	{
		s = BuyNode();
		int mid = (right-left+1)/2+left;
		s->leftchild = CreateBin(ar,left,mid-1);
		s->rightchild = CreateBin(ar,mid+1,right);
	}
	return s;
}
BtNode * CreateBinary(ElemType *ar,int n)
{
	if(ar == NULL || n < 1)
	{
		return NULL;
	}
	else 
		return CreateBin(ar,0,n);
}

///////////////////////////////////////////////////
int GetSize(BtNode *ptr)  //结点个数
{
	if(ptr == NULL)
	{
		return 0;
	}
	else 
		return GetSize(ptr->leftchild)+GetSize(ptr->rightchild)+1;
}

int Depth(BtNode *ptr)  //树的深度
{
	BtNode * max;
	if(ptr == NULL)
	{
		return 0;
	}
	else return 
		max = (ptr->leftchild > ptr->rightchild) ? (ptr->leftchild +1):(ptr->rightchild +1)
}

bool Is_Empty(BtNode *ptr)  //是否为空树
{
	return ptr == NULL;
}

BtNode * FindValue(BtNode *ptr ,ElemType x)
{
	if(NULL == ptr || ptr->data == x)
	{
		return ptr;
	}
	else
	{
		BtNode *p =  FindValue(ptr->leftchild,x);
		if(NULL == p)
		{
			p = FindValue(ptr->rightchild,x);
		}
		return p;
	}
}
BtNode * Parent(BtNode *ptr,BtNode *child)  //寻找结点父母
{
	if(NULL == ptr || ptr->leftchild == child || ptr->rightchild == child)
	{
		return ptr;
	}
	else
	{
		BtNode *p = Parent(ptr->leftchild,child);
		if(NULL == p)  //如果==写错,系统会给提示 [因为编译器不允许对常量赋值]
		{
			p = Parent(ptr->rightchild,child);
		}
		return p;
	}
}
BtNode * FindParent(BtNode *ptr,BtNode *child)
{
	if(NULL == ptr || NULL == child || ptr == child)
	{
		return NULL;
	}
	else
	{
		return Parent(ptr,child);
	}
}
void PrintPath(BtNode *ptr,vector<int> &vec,int val)  //结点路径和
{
	if(NULL != ptr)
	{
		vec.push_back(ptr->data);
		if(ptr->leftchild == NULL && ptr->rightchild == NULL)
		{
			int sum = 0;
			int n = vec.size();
			for(int i = 0;i<n;++i)
			{
				sum += vec[i];
			}
			if(sum == val)
			{
				for(int i = 0;i<n;++i)
				{
					cout<<vec[i]<<" ";
				}
				cout<<"=> "<<sum<<endl;
			}
		}
		PrintPath(ptr->leftchild,vec,val);
		PrintPath(ptr->rightchild,vec,val);
		vec.pop_back();
	}
}

/*
int main()
{
	int ar[]={10,5,4,-1,-1,7,-1,-1,12,-1,-1};
	int *p = ar;
	BinaryTree root = CreateTree2(p);
	InOrder(root);
	cout<<endl;
	vector<int> vec;
	PrintPath(root,vec,22);
	return 0;
}
int main()
{
	ElemType *str = "ABC##DE##F##G#H##";
	BinaryTree root = CreateTree2(str);
	InOrder(root);
	cout<<endl;
	return 0;
}
int main()
{
	int ar[]={12,23,34,45,56,67,78,89,95,100,110};
	int n = sizeof(ar)/sizeof(ar[0]);
	BinaryTree root = NULL;
	root = CreateBinary(ar,n);
	InOrder(root);
	cout<<endl;
	return 0;
}

*/
/*
int main()
{
	int ar[]={31,23,12,66,-1,5,17,70,62,-1,-1,-1,88,-1,55};
	const int n = sizeof(ar)/sizeof(ar[0]);
	int br[n];
	InOrder_Ar(ar,n);
	BinaryTree root = NULL;
	root = CreateTreeAr(ar,n);
	InOrder(root);
	cout<<endl;

	LinkCreateAr(root,br,n);
}
*/

/*
int main()
{
	ElemType *ps = "ABCDEFGH"; 
	ElemType *is = "CBEDFAGH";
	ElemType *ls = "CEFDBHGA"; 
	int n = strlen(ps);
	int n = strlen(ls);
	BinaryTree root = NULL;
	root  = CreatTreePI(ps,is,n);
	root  = CreatTreeIL(is,ls,n);
	
	PreOrder(root);
	cout<<endl;
	PastOrder(root);
	cout<<endl;

	return 0;
}
*/

 

    原文作者:平衡二叉树
    原文地址: https://blog.csdn.net/weixin_40740059/article/details/86696486
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞