POJ 2418 Hardwood Species( AVL-Tree )

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>

typedef struct AVLTree{

    char name[31];
    int nCount;
    int nHeight;

    struct AVLTree* pLeft;
    struct AVLTree* pRight;

}AVLTree;

int Max( int a, int b );
int Height( AVLTree* pNode );
AVLTree* Insert( char* s, AVLTree* pNode );
AVLTree* LLRotate( AVLTree* pNode );
AVLTree* RRRotate( AVLTree* pNode );
AVLTree* LRRotate( AVLTree* pNode );
AVLTree* RLRotate( AVLTree* pNode );
void PrintTree( AVLTree* pNode );

int n = 0;

int main(){

    char s[31];
    AVLTree* pRoot = NULL;

    while( gets( s ) != NULL ){

        pRoot = Insert( s, pRoot );
        n++;

    }

    PrintTree( pRoot );

    return 0;
}

int Max( int a, int b ){
    return ( a > b ) ? a : b;
}

int Height( AVLTree* pNode ){

    if( pNode == NULL )
        return -1;

    return pNode->nHeight;
}

AVLTree* Insert( char* s, AVLTree* pNode ){

    if( pNode == NULL ){
        pNode          = ( AVLTree* ) malloc( sizeof( AVLTree ) );
        strcpy( pNode->name, s );
        pNode->nCount  = 1;
        pNode->nHeight = 0;
        pNode->pLeft   = NULL;
        pNode->pRight  = NULL;
    }
    else if( strcmp( s, pNode->name ) == 0 ){
        pNode->nCount++;
    }
    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 if( strcmp( s, pNode->name ) > 0 ){

        pNode->pRight = Insert( s, pNode->pRight );

        if( Height( pNode->pRight ) - Height( pNode->pLeft ) == 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;
}

AVLTree* LLRotate( AVLTree* pNode ){

    AVLTree* pNodeLeft = pNode->pLeft;
    pNode->pLeft       = pNodeLeft->pRight;
    pNodeLeft->pRight  = pNode;
    pNode->nHeight     = Max( Height( pNode->pLeft ),     Height( pNode->pRight ) ) + 1;
    pNodeLeft->nHeight = Max( Height( pNodeLeft->pLeft ), pNode->nHeight ) + 1;

    return pNodeLeft;

}

AVLTree* RRRotate( AVLTree* pNode ){

    AVLTree* pNodeRight = pNode->pRight;
    pNode->pRight       = pNodeRight->pLeft;
    pNodeRight->pLeft   = pNode;
    pNode->nHeight      = Max( Height( pNode->pLeft ),       Height( pNode->pRight ) ) + 1;
    pNodeRight->nHeight = Max( Height( pNodeRight->pRight ), pNode->nHeight ) + 1;

    return pNodeRight;

}

AVLTree* LRRotate( AVLTree* pNode ){

    pNode->pLeft = RRRotate( pNode->pLeft );

    return LLRotate( pNode );
}

AVLTree* RLRotate( AVLTree* pNode ){

    pNode->pRight = LLRotate( pNode->pRight );

    return RRRotate( pNode );
}

void PrintTree( AVLTree* pRoot ){

    if( pRoot == NULL )
        return;

    PrintTree( pRoot->pLeft );
    printf( "%s %.4f\n", pRoot->name, ( ( double )( pRoot->nCount ) / ( double )n ) * 100 );
    PrintTree( pRoot->pRight );

}
时间: 2024-11-10 14:44:18

POJ 2418 Hardwood Species( AVL-Tree )的相关文章

[字典树] poj 2418 Hardwood Species

题目链接: http://poj.org/problem?id=2418 Hardwood Species Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 17511   Accepted: 6949 Description Hardwoods are the botanical group of trees that have broad leaves, produce a fruit or nut, and gene

poj 2418 Hardwood Species (trie树)

poj   2418   Hardwood Species http://poj.org/problem?id=2418 trie树+dfs 题意: 给你多个单词,问每个单词出现的频率. 方法:通过字典树,将所有单词放入树中,通过dfs遍历(题目要求按ASSIC码顺序输出单词及其频率),dfs可满足 注意:单词中不一定只出现26个英文字母,ASSIC码表共有256个字符 1 #include <stdio.h> 2 #include <string.h> 3 #include &l

POJ 2418 Hardwood Species(字典树)

题目链接:POJ 2418 Hardwood Species [题意]给出一大串树的名字,可能有重复,然后按字典序输出名字和百分比. [思路]我已开始偷懒用了map来做,这道题给的时间是10s,用map的8s也还是水过了,真是神奇啊,后来还是写了一下字典树,700ms+就过了,效率提升显著啊.这里要注意的是建字典树的ChildSize是256,题目输入不只有字母,还有一些其它字符. 下面贴上代码,先是map的: 1 /* 2 ** POJ 2418 Hardwood Species 3 ** C

POJ 2418 Hardwood Species(STL中map的应用)

题目地址:POJ 2418 通过这个题查了大量资料..知道了很多以前不知道的东西.... 在代码中注释说明吧. 代码如下: #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <stdlib.h> #include <math.h> #include <ctype.h> #include <queue&

POJ 2418 Hardwood Species Trie解法

计算一个字符串数组中有多少个重复字符串出现. 如果直接使用map容器,那么这条题就很简单了,一下就AC了,因为map已经处理好一切了: 不过时间超过1532ms,有点慢. 如下: int main() { map<string, int> msi; int total = 0; char treeName[40]; while (gets(treeName)) { msi[treeName]++; total++; } for (map<string, int>::iterator

poj 2418 Hardwood Species(二叉排序树)

Hardwood Species Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 19428 Accepted: 7658 http://poj.org/problem?id=2418 Description Hardwoods are the botanical group of trees that have broad leaves, produce a fruit or nut, and generally go d

POJ 2418 Hardwood Species(字典树 || map运用)

题目链接:http://poj.org/problem?id=2418 Description Hardwoods are the botanical group of trees that have broad leaves, produce a fruit or nut, and generally go dormant in the winter. America's temperate climates produce forests with hundreds of hardwood

[ACM] POJ 2418 Hardwood Species (Trie树或map)

Hardwood Species Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 17986   Accepted: 7138 Description Hardwoods are the botanical group of trees that have broad leaves, produce a fruit or nut, and generally go dormant in the winter. Ameri

[ACM] POJ 2418 Hardwood Species (Trie树或者map)

Hardwood Species Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 17986   Accepted: 7138 Description Hardwoods are the botanical group of trees that have broad leaves, produce a fruit or nut, and generally go dormant in the winter. Ameri

POJ 2418 Hardwood Species(字典树)

Hardwood Species Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 20085   Accepted: 7911 Description Hardwoods are the botanical group of trees that have broad leaves, produce a fruit or nut, and generally go dormant in the winter. Ameri