1.问题描述:

   判断一颗二叉树是否是平衡二叉树。

 

2.问题分析:

   平衡二叉树要求左子树和右子树的高度相差为1,且左右子树都是平衡二叉树,显然需要计算二叉树高度的函数。

 

3.代码:

 

template<typename T>
int DepthTree(BSTreeNode<T> *pbs)
{
	if (pbs==NULL)
		return 0;
	else
	{
		int leftLength=DepthTree(pbs->left);
		int rigthLength=DepthTree(pbs->right);
		return 1+(leftLength>rigthLength ? leftLength:rigthLength);
	}
}

template<typename T>
bool isBalanceTree(BSTreeNode<T> *pbs)
{
    if (pbs==NULL)
    {
		return true;
    }
    
	int depthLeft=DepthTree(pbs->left);
	int depthRight=DepthTree(pbs->right);
	
	if (abs(depthLeft-depthRight)>1) 
		 return false;
    else
		return isBalanceTree(pbs->left) && isBalanceTree(pbs->right);
}

  

/*
 *********************************************************************
     Version 1.0
     Author  fan
     Created 2011-6-29
 *********************************************************************
 */
#include<iostream>
#include<cmath>
using namespace std;

template<typename T>
struct BSTreeNode
{
    T data;
    BSTreeNode *left;
    BSTreeNode *right;
};

template<typename T>
int DepthTree(BSTreeNode<T> *pbs)
{
    if (pbs==NULL)
        return 0;
    else
    {
        int leftLength=DepthTree(pbs->left);
        int rigthLength=DepthTree(pbs->right);
        return 1+(leftLength>rigthLength ? leftLength:rigthLength);
    }
}

template<typename T>
bool isBalanceTree(BSTreeNode<T> *pbs)
{
    if (pbs==NULL)
    {
        return true;
    }
    
    int depthLeft=DepthTree(pbs->left);
    int depthRight=DepthTree(pbs->right);
    
    if (abs(depthLeft-depthRight)>1) 
         return false;
    else
        return isBalanceTree(pbs->left) && isBalanceTree(pbs->right);
}

void CreateTree1(BSTreeNode<int> *pHead)
{
     int m;
     int i;
     int rNum;                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       
     
     cin>>m;
    
     BSTreeNode<int> *currentNode;
     currentNode=pHead;
     pHead->left=NULL;
     pHead->right=NULL;

     cin>>currentNode->data;
      
     for(i=1;i<m;i++)
     {   
         rNum=rand()%2;
         if (rNum==0)
         {
             BSTreeNode<int> *leftNode;
             leftNode=new BSTreeNode<int>;
             cin>>leftNode->data;
             leftNode->left=NULL;
             leftNode->right=NULL;
             currentNode->left=leftNode;
             currentNode=leftNode;
         }
         else
         {
             BSTreeNode<int> *rightNode;
             rightNode=new BSTreeNode<int>;
             cin>>rightNode->data;
             rightNode->right=NULL;
             rightNode->left=NULL;
             currentNode->right=rightNode;
             currentNode=rightNode;
         }
     }
}

void CreateTree2(BSTreeNode<int> *pHead)
{
    int m;
    cin>>m;
    cin>>pHead->data;
    pHead->left=NULL;
    pHead->right=NULL;
    int i;
    BSTreeNode<int> *pCurrent;
    pCurrent=pHead;
   
    for (i=1;i<m;i++)
    { 
       BSTreeNode<int> *pTemp;
       pTemp=new BSTreeNode<int>;
       pTemp->left=NULL;
       pTemp->right=NULL;
       cin>>pTemp->data;
       pCurrent->left=pTemp;
       pCurrent=pCurrent->left;
    }
    
    pCurrent=pHead;
    while (pCurrent!=NULL)
    {
        cout<<pCurrent->data;
        pCurrent=pCurrent->left;
    }
}

int main()
{
    
    BSTreeNode<int> *head=NULL;

    head=new BSTreeNode<int>;
    CreateTree1(head);
    //CreateTree2(head);
    if (isBalanceTree(head))
    {
        cout<<"It is a Balance Tree!"<<endl;
    }else
    {
        cout<<"The tree is not Balance!"<<endl;
    }
    return 0;
}

5. 问题:

    测试数据中希望加入一个完全二叉树的测试样例,但我还不会建立完全二叉树,所以需要继续学习。