poj2418 Hardwood Species(AVL树(平衡二叉树)中序遍历=排序)

//poj 2418 Hardwood Species(AVL树(平衡二叉树)+中序遍历=>排序)
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
struct AVLtree
{
    char name[33];
    int count;
    int nheight;
    struct AVLtree *pleft;
    struct AVLtree *pright;
};
int n;
int Max(int a,int b)
{
    return a>b?a:b;
}
int Height(AVLtree *pnode)
{
    if(NULL==pnode)
    {
        return -1;
    }
    return pnode->nheight;
}
AVLtree *LLRotate(AVLtree *pnode)
{
    /*=======================================
        pnode               pnode->pleft
        /                      /    \
    pnode->pleft    ==>       /      \
       /                     /      pnode
pnode->pleft->pleft       pnode->pleft->pleft
    =========================================*/
    AVLtree *p;
    p=pnode->pleft;
    pnode->pleft=p->pright;
    p->pright=pnode;
    pnode->nheight=Max(Height(pnode->pleft),Height(pnode->pright))+1;
    p->nheight=Max(Height(p->pleft),pnode->nheight)+1;
    return p;
}
AVLtree *RRRotate(AVLtree *pnode)
{
    /*=======================================
    pnode                    pnode->pleft
        \                      /    \
    pnode->pleft    ==>       /      \
          \                  /      pnode->pleft->pleft
pnode->pleft->pleft       pnode
    =========================================*/
    AVLtree *p;
    p=pnode->pright;
    pnode->pright=p->pleft;
    p->pleft=pnode;
    pnode->nheight=Max(Height(pnode->pleft),Height(pnode->pright))+1;
    p->nheight=Max(Height(p->pright),pnode->nheight)+1;
    return p;
}
AVLtree *LRRotate(AVLtree *pnode)
{
       /*=======================================
        pnode              pnode               pnode->pleft
          /              /                      /    \
    pnode->pleft     pnode->pleft    ==>       /      \
           \            /                     /      pnode
pnode->pleft->pleft  pnode->pleft->pleft       pnode->pleft->pleft
    =========================================*/
    pnode->pleft=RRRotate(pnode->pleft);
    return LLRotate(pnode);
}
AVLtree *RLRotate(AVLtree *pnode)
{
    /*=======================================
        pnode              pnode               pnode->pleft
          \              /                      /    \
    pnode->pleft     pnode->pleft    ==>       /      \
           /            /                     /      pnode
pnode->pleft->pleft  pnode->pleft->pleft   pnode->pleft->pleft
    =========================================*/
    pnode->pright=LLRotate(pnode->pright);
    return RRRotate(pnode);
}
AVLtree *_insert(char *s,AVLtree *pnode)
{
    if(NULL==pnode)
    {
        pnode=new AVLtree;
        strcpy(pnode->name,s);
        pnode->pleft=NULL;
        pnode->pright=NULL;
        pnode->count=1;
        pnode->nheight=0;
    }
    else if(strcmp(s,(pnode)->name)==0)
    {
        pnode->count++;
    }
    else if(strcmp(s,(pnode)->name)<0)
    {
        pnode->pleft=_insert(s,pnode->pleft);
        if(Height(pnode->pleft)-Height(pnode->pright)==2)
        {
            if(strcmp(s,pnode->pleft->name)<0)
            {
                pnode=LLRotate(pnode);
            }
            else
            {
                pnode=LRRotate(pnode);
            }
        }
    }
    else
    {
        pnode->pright=_insert(s,pnode->pright);
        if(Height(pnode->pleft)-Height(pnode->pright)==-2)
        {
            if(strcmp(s,pnode->pright->name)>0)
            {
                pnode=RRRotate(pnode);
            }
            else
            {
                pnode=RLRotate(pnode);
            }
        }
    }
    pnode->nheight=Max(Height(pnode->pleft),Height(pnode->pright))+1;
    return pnode;
}
void mid_cal(AVLtree *root)
{
    if(root!=NULL)
    {
        mid_cal(root->pleft);
        printf("%s %0.4lf\n",root->name,((double)(root->count)/(double)n)*100);
        mid_cal(root->pright);
    }
}
int main()
{
    freopen("pj2418.in","r",stdin);
    AVLtree *root=NULL;
    char s[33];
    while(gets(s)!=NULL)
    {
        root=_insert(s,root);
        n++;
    }
    mid_cal(root);
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

u014068781

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值