#include
#include
#include
#include
#include
#include
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<data<<" ";
PreOrder(p->leftchild);
PreOrder(p->rightchild);
}
}
void InOrder(BtNode *p) //中序遍历顺序
{
if(p != NULL)
{
InOrder(p->leftchild);
cout<data<<" ";
InOrder(p->rightchild);
}
}
void PastOrder(BtNode *p) //后序遍历顺序
{
if(p != NULL)
{
PastOrder(p->leftchild);
PastOrder(p->rightchild);
cout<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(idata = 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 &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 "<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< vec;
PrintPath(root,vec,22);
return 0;
}
int main()
{
ElemType *str = "ABC##DE##F##G#H##";
BinaryTree root = CreateTree2(str);
InOrder(root);
cout<