C++计算二叉树的节点数和高度

用struct结构体的写法:





/* * description: 计算二叉树的层数和节点数 * writeby: nick * date: 2012-10-23 16:16 * */ #include <iostream> using namespace std; struct node { int item; node *l, *r; node(int n) {item=n; l=0; r=0;} }; typedef node *link; //计算节点总数 int count(link h) { if(h==0) return 0; return count(h->l) + count(h->r) + 1; }

//计算叶子节点总数方法1
int leafcount(link h)
{
    if(h==0) return 0;
if(h->l==null&&h->r==null) return 1; return count(h->l) + count(h->r); }
//计算叶子节点总数方法2
int leafcount(link h)
{
static int num=0;//static不建议这样写,最好放外边,这样写的话程序执行完了,这个变量还在内存中。 if(h==0) return 0;
if(h->l==null&&h->r==null) num++;
leafcount(h->l);
leafcount(h->r); return num; }


//计算高度 int height(link h) 
{ if(h==0) return -1;
int u=height(h->l);
int v=height(h->r);
return u>v?u+1:v+1; }
int main() {
link root = new node(4);
root -> l = new node(5);
root -> r = new node(6);
root->l->l = new node(7);
root->l->r = new node(8);
cout << count(root) << " " << height(root);
return 0; }

  

带class类的写法:

//叶子节点的个数
/*
(1)如果二叉树为空,返回0
(2)如果二叉树不为空且左右子树为空,返回1
(3)如果二叉树不为空,且左右子树不同时为空,返回左子树中叶子节点个数加上右子树中叶子节点个数
*/
[cpp] view plain copy print?
int GetLeafNodeNum(BTree* root)  
{  
    if(root == NULL)  
        return 0;  
    if(root->m_pLeft == NULL && root->m_pRight == NULL)  
        return 1;  
  
    int LeafNumOfLeft = GetLeafNodeNum(root->m_pLeft);  
    int LeafNumOfRight = GetLeafNodeNum(root->m_pRight);  
  
    int ret = LeafNumOfLeft + LeafNumOfRight;  
  
    return ret;  
  
}  

 
/*
判断量个二叉树的结构是否相同
1:如果两个二叉树都为空,那么返回true
2:如果一个二叉树为空,另外一个不为空,那么返回false
3:如果两个二叉树都不为空,那么如果它们的左子树和右子树的结果相同,返回true否则返回false
*/
[cpp] view plain copy print?
bool isEqual(BTree*  root1, BTree* root2)  
{  
    if(root1 == NULL && root2 == NULL)  
        return true;  
    else if ((root1 == NULL && root2!= NULL)|| (root1 != NULL && root2 == NULL))  
        return false;  
  
    bool equalLeft = isEqual(root1->m_pLeft,root2->m_pLeft);  
    bool equalRight = isEqual(root1->m_pRight,root2->m_pRight);  
  
    return (equalLeft && equalRight);  
}  

完整测试代码:
[cpp] view plain copy print?
// BTNumOfKLevel.cpp : 定义控制台应用程序的入口点。  
//  
  
#include "stdafx.h"  
#include <iostream>  
using namespace std;  
  
  
  
class BTree  
{  
public:  
    int     m_nValue;  
    BTree*  m_pLeft;  
    BTree*  m_pRight;  
  
    BTree(int m):m_nValue(m)  
    {  
        m_pLeft = m_pRight = NULL;  
    }  
  
};  
//二叉树的插入实现  
void Insert(int value, BTree* &root)  
{  
    if (root == NULL)  
    {  
        root = new BTree(value);  
    }  
    else if(value < root->m_nValue)  
        Insert(value,root->m_pLeft);  
    else if(value > root->m_nValue)  
        Insert(value,root->m_pRight);  
    else  
        ;  
}  
  
  
//叶子节点的个数  
/* 
(1)如果二叉树为空,返回0 
(2)如果二叉树不为空且左右子树为空,返回1 
(3)如果二叉树不为空,且左右子树不同时为空,返回左子树中叶子节点个数加上右子树中叶子节点个数 
*/  
int GetLeafNodeNum(BTree* root)  
{  
    if(root == NULL)  
        return 0;  
    if(root->m_pLeft == NULL && root->m_pRight == NULL)  
        return 1;  
  
    int LeafNumOfLeft = GetLeafNodeNum(root->m_pLeft);  
    int LeafNumOfRight = GetLeafNodeNum(root->m_pRight);  
  
    int ret = LeafNumOfLeft + LeafNumOfRight;  
  
    return ret;  
  
}  
  
/* 
判断量个二叉树的结构是否相同 
1:如果两个二叉树都为空,那么返回true 
2:如果一个二叉树为空,另外一个不为空,那么返回false 
3:如果两个二叉树都不为空,那么如果它们的左子树和右子树的结果相同,返回true否则返回false 
 
*/  
  
bool isEqual(BTree*  root1, BTree* root2)  
{  
    if(root1 == NULL && root2 == NULL)  
        return true;  
    else if ((root1 == NULL && root2!= NULL)|| (root1 != NULL && root2 == NULL))  
        return false;  
  
    bool equalLeft = isEqual(root1->m_pLeft,root2->m_pLeft);  
    bool equalRight = isEqual(root1->m_pRight,root2->m_pRight);  
  
    return (equalLeft && equalRight);  
}  
  
  
int _tmain(int argc, _TCHAR* argv[])  
{  
    BTree* m_pRoot = new BTree(4);  
    Insert(3,m_pRoot);  
    Insert(6,m_pRoot);  
    Insert(1,m_pRoot);  
    Insert(2,m_pRoot);  
    Insert(5,m_pRoot);  
    Insert(8,m_pRoot);  
    Insert(7,m_pRoot);  
    Insert(10,m_pRoot);  
  
    BTree* m_pRoot2 = new BTree(4);  
    Insert(3,m_pRoot2);  
    Insert(6,m_pRoot2);  
    Insert(1,m_pRoot2);  
    Insert(2,m_pRoot2);  
    Insert(5,m_pRoot2);  
    Insert(8,m_pRoot2);  
    Insert(7,m_pRoot2);  
    Insert(10,m_pRoot2);  
  
    int count = GetLeafNodeNum(m_pRoot);  
    cout<<"叶子节点的个数为:"<<count<<endl;  
  
    cout<<"两个树的结构是否相同:"<<isEqual(m_pRoot,m_pRoot2);  
  
    getchar();  
    return 0;  
}  

  

原文地址:https://www.cnblogs.com/imqsl/p/7715885.html