用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;
}