C++数据结构--计算二叉树叶子数和深度
2013-08-01 08:31
381 查看
1.示例二叉树
2.计算二叉树深度:
*先计算左右子树的深度,然后整棵树的深度就是左右子树深度较大值加1
*递归遍历结束条件:定义空树的深度为-1,于是得到叶子节点的深度计算公式
Depth(叶节点)= 1 + max(depth(左子树),depth(右子树))
= 1 + max(-1,-1)
= 1 + -1
=0
计算二叉树深度图解:#后的数字代表遍历的步骤,字母的下标表示节点的深度
实现代码:
template<typename T>
class node
{
public:
T val; //节点值
node<T>* left;
//左节点
node<T>* right;
//右节点
node():val(T()),left(nullptr),right(nullptr){}
node(T v, node<T>* l=nullptr, node<T>* r=nullptr):val(v),left(l),right(r){}
};
node<char>* createBTree() //构建一棵示例二叉树
{
node<char>* g=new node<char>('G');
node<char>* e=new node<char>('E');
node<char>* f=new node<char>('F');
node<char>* d=new node<char>('D',nullptr,g);
node<char>* c=new node<char>('C',nullptr,f);
node<char>* b=new node<char>('B',d,e);
node<char>* a=new node<char>('A',b,c);
return a;
}
//计算叶子节点数只需用前序,中序,后序等任一种方式遍历二叉树
void leafCounts(node<char>* root,int &cnt)
{
if(root)
{
if(root->left==nullptr&&root->right==nullptr)
//判断是否为叶子节点
{
cnt++;
return;
}
else
{
leafCounts(root->left,cnt);
//遍历左子树
leafCounts(root->right,cnt);
//遍历右子树
}
}
}
//计算二叉树的深度
int depth(node<char>* root)
{
int rdepth=0; //根节点深度
int lval=0;
//左子树深度
int rval=0;
//右子树深度
if(!root) //当到达叶子节点的左子树或右子树或整棵树为空树时
{
rdepth=-1;
}
else
{
lval=depth(root->left);//遍历左子树
rval=depth(root->right);
//遍历右子树
rdepth=1+(lval>rval?lval:rval);
}
return rdepth;
}
int main()
{
node<char>* root=createBTree();
int count=0;
leafCounts(root,count);
cout<<count<<endl; //3
int dpt=depth(root);
cout<<dpt<<endl; //3
return 0;
}
2.计算二叉树深度:
*先计算左右子树的深度,然后整棵树的深度就是左右子树深度较大值加1
*递归遍历结束条件:定义空树的深度为-1,于是得到叶子节点的深度计算公式
Depth(叶节点)= 1 + max(depth(左子树),depth(右子树))
= 1 + max(-1,-1)
= 1 + -1
=0
计算二叉树深度图解:#后的数字代表遍历的步骤,字母的下标表示节点的深度
实现代码:
template<typename T>
class node
{
public:
T val; //节点值
node<T>* left;
//左节点
node<T>* right;
//右节点
node():val(T()),left(nullptr),right(nullptr){}
node(T v, node<T>* l=nullptr, node<T>* r=nullptr):val(v),left(l),right(r){}
};
node<char>* createBTree() //构建一棵示例二叉树
{
node<char>* g=new node<char>('G');
node<char>* e=new node<char>('E');
node<char>* f=new node<char>('F');
node<char>* d=new node<char>('D',nullptr,g);
node<char>* c=new node<char>('C',nullptr,f);
node<char>* b=new node<char>('B',d,e);
node<char>* a=new node<char>('A',b,c);
return a;
}
//计算叶子节点数只需用前序,中序,后序等任一种方式遍历二叉树
void leafCounts(node<char>* root,int &cnt)
{
if(root)
{
if(root->left==nullptr&&root->right==nullptr)
//判断是否为叶子节点
{
cnt++;
return;
}
else
{
leafCounts(root->left,cnt);
//遍历左子树
leafCounts(root->right,cnt);
//遍历右子树
}
}
}
//计算二叉树的深度
int depth(node<char>* root)
{
int rdepth=0; //根节点深度
int lval=0;
//左子树深度
int rval=0;
//右子树深度
if(!root) //当到达叶子节点的左子树或右子树或整棵树为空树时
{
rdepth=-1;
}
else
{
lval=depth(root->left);//遍历左子树
rval=depth(root->right);
//遍历右子树
rdepth=1+(lval>rval?lval:rval);
}
return rdepth;
}
int main()
{
node<char>* root=createBTree();
int count=0;
leafCounts(root,count);
cout<<count<<endl; //3
int dpt=depth(root);
cout<<dpt<<endl; //3
return 0;
}
相关文章推荐
- 基本数据结构——二叉树的建立,遍历,求叶子节点,深度计算
- c++实现二叉树的查找,插入,删除,深度,叶子节点数,度为1的节点数(递归方法)及运行实例结果
- c++实现二叉树的查找,插入,删除,深度,叶子节点数,度为1的节点数(递归方法)及运行实例结果
- c++实现二叉树的查找,插入,删除,深度,叶子节点数,度为1的节点数(递归方法)及运行实例结果
- c++实现二叉树的查找,插入,删除,深度,叶子节点数,度为1的节点数(递归方法)及运行实例结果
- 【数据结构】二叉树的实现(如:默认成员函数、(叶子)节点数、深度、四种遍历)
- c++实现二叉树的查找,插入,删除,深度,叶子节点数,度为1的节点数(递归方法)及运行实例结果
- c++实现二叉树的查找,插入,删除,深度,叶子节点数,度为1的节点数(递归方法)及运行实例结果
- 【数据结构】二叉树的实现(如:默认成员函数、(叶子)节点数、深度、四种遍历)
- C++二叉树的构建及求深度,叶子数量,层次遍历
- 二叉树的建立、遍历、叶子节点计数、深度计算
- c++实现二叉树的查找,插入,删除,深度,叶子节点数,度为1的节点数(递归方法)及运行实例结果
- c++实现二叉树的查找,插入,删除,深度,叶子节点数,度为1的节点数(递归方法)及运行实例结果
- c++实现二叉树的查找,插入,删除,深度,叶子节点数,度为1的节点数(递归方法)及运行实例结果
- C++使用递归和非递归算法实现的二叉树叶子节点个数计算方法
- c++实现二叉树的查找,插入,删除,深度,叶子节点数,度为1的节点数(递归方法)及运行实例结果
- c++实现二叉树的查找,插入,删除,深度,叶子节点数,度为1的节点数(递归方法)及运行实例结果
- 数据结构与算法 -- 二叉树链式详解((非)/递归遍历,叶子个数,深度计算)
- 【数据结构】二叉树的定义,递归遍历,非递归遍历,层次遍历,深度等
- 【数据结构_树_Tree_0972】统计利用先序计算二叉树宽度