#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. Qt读取ANSI格式文件——利用QTextCodec将其他编码格式的QByteArray转换为Unicode格式,或者从文件中读出后直接做转换

    t使用Unicode来表示字符串.但是通常需要访问一些非Unicode格式的字符串,例如打开一个GBK编码的中文文本文件,甚至一些非Unicode编码的日文,俄文等. Qt提供了QTextCodec类 ...

  2. Python,PIL压缩裁剪图片

    自己写了用来压缩 DC 照片的,批量处理整目录文件,非常方便.需要安装 PIL #!/usr/bin/env python import Image import os import os.path ...

  3. Struts 2最新0day破坏性漏洞(远程任意代码执行)等的重现方法

    Struts 2的远程任意代码执行和重定向漏洞,是这两天互联网上最重大的安全事件,据说国内互联网企业中,很多电商纷纷中招,应该已经有大规模的用户隐私泄露.这里我们简单总结下怎样在自己机子上重现这些漏洞 ...

  4. linux-shell脚本命令之sed

    [ sed简单介绍: ] sed是一个非常好的文件处理工具, 它本身是一个管道命令, 以行为单位进行处理, 能够用于对数据行进行新增.选取.替换.删除等操作. sed命令行格式:sed [-nefri ...

  5. The Dole Queue

    The Dole Queue Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit cid ...

  6. 向服务器写入错误日志-log

    /// <summary> /// 写日志,方便测试(看网站需求,也可以改成把记录存入数据库) /// </summary> /// <param name=" ...

  7. 中文分词算法之最大正向匹配算法(Python版)

    最大匹配算法是自然语言处理中的中文匹配算法中最基础的算法,分为正向和逆向,原理都是一样的. 正向最大匹配算法,故名思意,从左向右扫描寻找词的最大匹配. 首先我们可以规定一个词的最大长度,每次扫描的时候 ...

  8. jquery mobile实现拨打电话功能的几种方法

    3.使用wtai协议进行拨打电话. 在wml中可以调用设备的wtai函数来呼叫特定的电话号码.目前,越来越多的浏览器都支持这个功能,但还不是所有. 代码如下所示: 复制代码 代码如下: <inp ...

  9. list view Item 里面有ImageButton

    list view Item 里面有ImageButton listview 的item中有ImageButton, 或者 Button. 目的: ImageButton 和 Button的click ...

  10. 深入研究Clang(四) Clang编译器的简单分析

    作者:史宁宁(snsn1984) 首先我们确定下Clang编译器的详细内容和涵盖范围.之前在<LLVM每日谈之二十 Everything && Clang driver>中 ...