中序遍历
if(T)
{
蓝色的玫瑰InOrderTraver(T->lchild);
cout << T->data;
InOrderTraver(T->rchild);
}
}
void PreOrderTraver(BiTree T)
{
/*先序遍历*/
if(T)
{
cout << T->data;
PreOrderTraver(T->lchild);
PreOrderTraver(T->rchild);
}
春字的拼音}
void PostOrderTraver(BiTree T)
{
/*后序遍历*/
if(T)
{
PostOrderTraver(T->lchild);
PostOrderTraver(T->rchild);
cout << T->data;
}
}
/*统计⼆叉树中结点的个数*/
int NodeCount(BiTree T)
{
if(T ==NULL)
return0;
el
return NodeCount(T->lchild)+NodeCount(T->rchild)+1; }
/*求树的深度*/
int Depth(BiTree T)
浙江省护理质量上报系统
{
if(T ==NULL)
return0;
el
{
int i =Depth(T->lchild);
int j =Depth(T->rchild);
return i > j ? i +1: j +1;
}
}
/*复制⼆叉树*/
void Copy(BiTree T, BiTree &NewT)
{
if(T =NULL)
{
NewT =NULL;
return;
}
el
{
NewT =new BiNode;
NewT->data = T->data;
Copy(T->lchild, NewT->lchild);
Copy(T->rchild, NewT->rchild);
中国港口博物馆
}
}
/*统计⼆叉树中叶⼦结点的个数*/
int LeafCount(BiTree T)
{
{
if(!T)
return0;
侏罗纪公园3d
if(!T->lchild &&!T->rchild)
return1;
/*如果⼆叉树左⼦树和右⼦树皆为空,说明该⼆叉树根节点为叶⼦结点,结果为1*/ el
return LeafCount(T->lchild)+LeafCount(T->rchild);
}
/*⼆叉树中从每个叶⼦结点到跟结点的路径*/
void PrintAllPath(BiTree T,char path[],int pathlen)
{
int i;
if(T !=NULL)
{
path[pathlen]= T->data;/*将当前结点放⼊路径中*/
if(T->lchild ==NULL&& T->rchild ==NULL)
{
/*若这个节点是叶⼦结点*/
for(i = pathlen; i >=0; i--)
cout << path[i]<<" ";
cout <<"\n";
}汉语发音
el
{
PrintAllPath(T->lchild, path, pathlen +1);
PrintAllPath(T->rchild, path, pathlen +1);
}
}
}
/*判断⼆叉树是否为空*/
int BiTree_empty(BiTree T)
{
if(T)
return1;
el
return0;
}
int main()
{
BiTree T;
//测试数据AB#CD##E##F#GH###
cout <<"先序遍历输⼊(以#结束):";
CreateBiTree(T);
cout <<"中序遍历输出:";
InOrderTraver(T);
cout << endl
<<"先序遍历输出:";
PreOrderTraver(T);
感冒能不能喝鸡汤cout <<"\n"
<<"后序遍历输出:";
PostOrderTraver(T);
cout << endl
<<"树的深度:"<<Depth(T);
cout << endl
<<"结点的个数:"<<NodeCount(T);
桃花苗
cout << endl
<<"⼆叉树中从每个叶⼦结点到根结点的所有路径:"<< endl;
char path[256];
int pathlen =0;
PrintAllPath(T, path, pathlen);
return0;
}