求任意一颗二叉树的繁茂度

在《数据结构》严蔚敏版上的习题看到这个问题。
繁茂度的定义:各层节点数的最大值与树的高度的乘积
树的深度好求,递归调用就行了。关键在于如何求出各层的最大节点树?下面是我的一种实现方法:
bitree.h

#include 
#include 
#include 
#include 
using namespace std;

typedef struct TNode
{
    int data;
    struct TNode* lchild;
    struct TNode* rchild;
}BiTree, *pBiTree;

void creat_tree(pBiTree &rt)
{
    char ch;
    ch=getchar();
    if('#'==ch)
    {
        rt=NULL;
    }
    else
    {
        rt=(pBiTree)malloc(sizeof(BiTree));
        rt->data=ch;
        creat_tree(rt->lchild);        //构造左子树
        creat_tree(rt->rchild);    //构造右子树
    }
}

void PreOrderPrint(pBiTree &rt)
{
    cout << "PreOrderPrint: ";
    if(!rt)
        return;
    stack s;
    s.push(rt);
    while(!s.empty())
    {
        pBiTree cur = s.top();
        cout << (char)(cur->data);
        s.pop();
        if(cur->rchild)
            s.push(cur->rchild);
        if(cur->lchild)
            s.push(cur->lchild);
    }
    cout << '@' << endl;
}

void InOrderPrint(pBiTree &rt)
{
    cout << "InOrderPrint: ";
    if(!rt)
        return;
    stack s;
    pBiTree cur = rt;

    while(!s.empty() || cur != NULL)
    {
        while(cur)
        {
            s.push(cur);
            cur = cur->lchild;
        }
        if(!s.empty())
        {
            cur = s.top();
            cout << (char)(cur->data);
            s.pop();
            cur = cur->rchild;
        }
    }
    cout << '@' << endl;
}
/*
求二叉树的繁茂度
*/
#include "bitree.h"
#include 

iint Prosperity(pBiTree &rt)
{
    if(!rt)
        return 0;

    queue q;
    pBiTree cur = rt;
    int now = 1;//current layer's node number
    int next = 2;//next layer's node number
    int i = 0;//loop counter
    int maxLevelNodes = 1; //max among every level's node
    int layers = 0; //Depth, i.e, layers

    q.push(cur);
    while(!q.empty())
    {
        cur = q.front();
        if(cur->lchild)
            q.push(cur->lchild);
        else
            next -= 1;
        if(cur->rchild)
            q.push(cur->rchild);
        else
            next -= 1;
        i++;
        q.pop();
        if(i == now)
        {
            layers++;//if i == now, means go over one layer
            now = next;// let now equals next layer's node number, start next loop
            next = now * 2;//ideally, next layer's node number has twice nodes of current
                            //"ideally" means each node has both lchild and rchild
                            //if some node is lack of one branch, the var next will minus by 1
                            //as shown above;
            i = 0;
            if(maxLevelNodes < now)
                maxLevelNodes = now;
        }
    }
    return maxLevelNodes * layers;
}

int main(int argc, char const *argv[])
{
    pBiTree root = NULL;
    creat_tree(root);
    PreOrderPrint(root);

    cout << "Prosperity: " << Prosperity(root) << endl;

    return 0;
}

由于跟层有关,所以很自然地想到了用层序遍历的思路。如果层序遍历一颗满二叉树,那么下一层的结点树是上一层的两倍;那么,如果上一层的某个结点缺少一个分支,下一层就会少一个结点,充分利用这一点,在每一层遍历结束时就可以算出下一层有多少个节点,这样就能得出哪一层的结点数最多,now存储了当前层的结点数,next存储下一层的结点数(理想情况为两倍,缺少的减去就行了)。

你可能感兴趣的:(算法问题)