#include<iostream>
using namespace std;
typedef char elemtype;
typedef struct binode
{
elemtype data;
struct binode* lch, * rch;
}binode, * bitree;
int copy(bitree t, bitree& newt)//复制二叉树
{
if (t == NULL)
{
newt = NULL;
return 0;
}
else
{
newt = new binode;
newt->data = t->data;
copy(t->lch, t->lch);
copy(t->rch, t->rch);
}
}
int depth(bitree t)//计算二叉树深度
{
if (t == NULL)
{
return 0;
}
else
{
m = depth(t->lch);
n = depth(t->rch);
if (m > n)
{
return m + 1;
}
else
{
return n + 1;
}
}
}
int nodecount(bitree t)//计算二叉树节点总数
{
if (t == NULL)
{
return 0;
}
else
{
return nodecount(t->lch) + nodecount(t->rch) + 1;
}
}
int leadcount(bitree t)//计算叶子节点总数
{
if (t == NULL)
{
return 0;
}
if (t->lch == NULL && t->rch == NULL)
{
return 1;
}
else
{
return leadcount(t->lch) + leadcount(t->rch);
}
}
二叉树的遍历算法应用
最新推荐文章于 2024-09-12 19:02:20 发布