#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 ); }

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

  1. [字典树] poj 2418 Hardwood Species

    题目链接: id=2418">http://poj.org/problem?id=2418 Hardwood Species Time Limit: 10000MS   Memory ...

  2. POJ 2418 Hardwood Species

                                                     Hardwood Species Time Limit: 10000MS   Memory Limit ...

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

    Hardwood Species Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 17986   Accepted: 713 ...

  4. POJ 2418 Hardwood Species(STL在map应用)

    职务地址:id=2418">POJ 2418 通过这个题查了大量资料..知道了非常多曾经不知道的东西. . .. 在代码中凝视说明吧. 代码例如以下: #include <ios ...

  5. POJ - 2418 Hardwood Species(map,trie,BST)

    1.输入若干行树名,输入结束后,按字典序输出树名及其所占百分比. 2.多种方法:map,trie,BST 3. map: #include<iostream> #include<st ...

  6. poj 2418 Hardwood Species (map)

    题目:http://poj.org/problem?id=2418 在poj 上交题总是有各种错误,再次感叹各个编译器. c++ AC代码,G++为超时,上代码: #include<cstdio ...

  7. POJ 2418 Hardwood Species (哈希,%f 和 %lf)

    我的错因: 本来改用%f输出,我用了%lf,结果编译器直接判定为错误(一部分编译器认为lf是没有错的).当时我还以为是hash出错了.. 方法不止一种: 方法 时间   空间 Hash 891ms 5 ...

  8. 二叉搜索树 POJ 2418 Hardwood Species

    题目传送门 题意:输入一大堆字符串,问字典序输出每个字符串占的百分比 分析:二叉搜索树插入,然后中序遍历就是字典序,这里root 被new出来后要指向NULL,RE好几次.这题暴力sort也是可以过的 ...

  9. POJ 2418 Hardwood Species 【Trie树】

    <题目链接> 题目大意: 给你一堆字符串,让你按字典序输出他们出现的频率. 解题分析: 首先,这是Trie数词频统计的题目,以Trie树的边储存字母,节点存储以该节点结尾的链所代表的字符串 ...

随机推荐

  1. asp.net导出Excel类库

    using System; using System.Collections.Generic; using System.Reflection; using System.Web; using Exc ...

  2. input标签的hidden属性的应用及作用

    定义:传输关于客户端/服务器交互的状态信息. Transmits state information about client/server interaction. 解释: 此元素在页面中不显示,在 ...

  3. 大白痴学习webmagic

    摘要 webmagic 学习 从头 刚刚开始学,很多东西可能理解错了,还请各位指教 一些基本类: Request:包含要爬行的url和一些附加信息,是Page的一个成员变量 主要成员变量 String ...

  4. 单选按钮易忽略的Group属性

    Group就其意思就是一组的意思.就是说用于选择多个控件组合,选了TRUE后,你就可以为这组新建一个变量.把一组控件当一个控件来使用.例如多个单选按钮用group属性,这样你就可以用一个变量来管理这些 ...

  5. 使用BigDecimal来进行精确计算

    在一些以金融等行业中的计算是需要十分精确的,即使我们使用像double这样的类型,由于浮点数的原因,会使得数据计算变得不精确,例如下面的例子: double a = 0.1; double b = 0 ...

  6. java实现点卡生成

    点卡主要有2部分:卡号和密码.卡号一般由数字组成,密码就不多说了. java中随机数很强大,大家可以自己查.卡号生成使用java中随机数,密码使用uuid,密码可以自己再加点东西之类的.下面是完整代码 ...

  7. smartforms长文本处理方式

    因为长文本的话不好换行,在smartforms中对于长文本的处理有两种:一种是自定义文本模板,自己定义好的文本格式,然后再使用的时候再文本类型中选择文本模板,最好是勾上如果没有文本存在怎无错误.这种方 ...

  8. clear、REFRESH、free区别

    clear可以清楚一个工作区或变量.但是如果该内表是带表头的,清空内表时需要在该内表后加[].例如:clear gt_tab[]. free可以清空带表头的内表但是不会清空这个带表头内表的表头,但是也 ...

  9. 贴一个CMemDC 代码,这东西真不错噢,短小精悍,可谓极品

    罗索客 发布于 2006-11-28 21:53 点击:3941次  来自: 原文: http://yuantao82.spaces.live.com/Blog/cns!8FC0A772D812A22 ...

  10. Qt之设置QWidget背景色(QStyleOption->drawPrimitive(QStyle::PE_Widget)方法比较有趣)

    QWidget是所有用户界面对象的基类,这意味着可以用同样的方法为其它子类控件改变背景颜色. Qt中窗口背景的设置,下面介绍三种方法. 1.使用QPalette2.使用Style Sheet3.绘图事 ...