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 ); }
POJ 2418 Hardwood Species( AVL-Tree )的更多相关文章
- [字典树] poj 2418 Hardwood Species
题目链接: id=2418">http://poj.org/problem?id=2418 Hardwood Species Time Limit: 10000MS Memory ...
- POJ 2418 Hardwood Species
Hardwood Species Time Limit: 10000MS Memory Limit ...
- [ACM] POJ 2418 Hardwood Species (Trie树或map)
Hardwood Species Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 17986 Accepted: 713 ...
- POJ 2418 Hardwood Species(STL在map应用)
职务地址:id=2418">POJ 2418 通过这个题查了大量资料..知道了非常多曾经不知道的东西. . .. 在代码中凝视说明吧. 代码例如以下: #include <ios ...
- POJ - 2418 Hardwood Species(map,trie,BST)
1.输入若干行树名,输入结束后,按字典序输出树名及其所占百分比. 2.多种方法:map,trie,BST 3. map: #include<iostream> #include<st ...
- poj 2418 Hardwood Species (map)
题目:http://poj.org/problem?id=2418 在poj 上交题总是有各种错误,再次感叹各个编译器. c++ AC代码,G++为超时,上代码: #include<cstdio ...
- POJ 2418 Hardwood Species (哈希,%f 和 %lf)
我的错因: 本来改用%f输出,我用了%lf,结果编译器直接判定为错误(一部分编译器认为lf是没有错的).当时我还以为是hash出错了.. 方法不止一种: 方法 时间 空间 Hash 891ms 5 ...
- 二叉搜索树 POJ 2418 Hardwood Species
题目传送门 题意:输入一大堆字符串,问字典序输出每个字符串占的百分比 分析:二叉搜索树插入,然后中序遍历就是字典序,这里root 被new出来后要指向NULL,RE好几次.这题暴力sort也是可以过的 ...
- POJ 2418 Hardwood Species 【Trie树】
<题目链接> 题目大意: 给你一堆字符串,让你按字典序输出他们出现的频率. 解题分析: 首先,这是Trie数词频统计的题目,以Trie树的边储存字母,节点存储以该节点结尾的链所代表的字符串 ...
随机推荐
- MDK的优化应用
MDK的优化应用 http://blog.163.com/zhaojun_xf/blog/static/300505802011291384721/ 使用Keil/MDK这么多年了,一直都没有使用它的 ...
- c语言,变长数组
下面这个结构体,可以在malloc的时候指定数据data的长度,这样的形式就是变长数组:typedef struct{ int data_len; char data[0];//或char data[ ...
- jquery实现ajax提交form表单的方法总结
本篇文章主要是对jquery实现ajax提交form表单的方法进行了总结介绍,需要的朋友可以过来参考下,希望对大家有所帮助 方法一: function AddHandlingFeeToRefund( ...
- Jquery文本框小例(必填框)
<script src="../JavaScript/jquery-2.0.2.min.js"></script> <script type=&quo ...
- perl Exporter一些神奇写法
use base qw(Exporter); @JSON::EXPORT = qw(from_json to_json jsonToObj objToJson encode_json decode_j ...
- MFC画二维动态图表[GDI]
源博客:http://www.codeproject.com/Articles/9350/2D-Animated-Charts 源代码:http://download.csdn.net/detail/ ...
- Appium Server 传递的基本参数
Appium Server 传递的基本参数 官方列表 Appium server capabilities Capability Description Values automationName ...
- Java基础:泛型及其擦除性、不可协变性
转载请注明出处:jiq•钦's technical Blog 1泛型语法: 泛型类: class ClassName<T>{} 泛型方法:public <T> void f(T ...
- hdu 4507 数位dp(求和,求平方和)
http://acm.hdu.edu.cn/showproblem.php?pid=4507 Problem Description 单身! 依旧单身! 吉哥依旧单身! DS级码农吉哥依旧单身! 所以 ...
- uploadify,实际开发案例【选择完文件点击上传才上传】
<script type="text/javascript"> )+Math.floor(Math.random()*)+']-'; //设置随机文件前缀. $k(fu ...