一、二叉树中其他的操作
- 统计树中结点的数目
(左子树中节点的数目+右子树中节点的数目+1(根结点))
template<class T>
int BiTree<T>::count(BiNode<T>* root){
int number=0;
if (root==NULL)
number=0;
else
number=count(root->lchild)+count(root->rchild)+1;
return number;
}
2.统计树中叶子结点的数目
(左子树中叶子节点的数目+右子树中叶子节点的数目)
template<class T>
int BiTree<T>::leafcount(BiNode<T>* root){
int number=0;
if (root==NULL)
number=0;
else if(root->lchild==NULL && root->rchild==NULL)
number=1;
else
number=leafcount(root->lchild)+leafcount(root->rchild);
return number;
}
3.
计算叶子的高度
Max(左子树的高度,右子树的高度)+1
template<typename T>
int BiTree<T>::cal_height(BiTreeNode<T> * root){
int lheight=0,rheight=0;
if (root==0) return 0;
lheight=cal_height(root->lchild);
rheight=cal_height(root->rchild);
if (lheight>rheight) return lheight+1;
else return rheight+1;
}
4.
计算二叉树的宽度(基于层次遍历实现)
struct q_element{ BiNode