#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. matlab三维画图

    matlab三维画图主要有三个命令:plot3命令.mesh命令和surf命令. plot3 plot3是三维画图的基本函数,绘制的是最为主要的3D曲线图,最主要的调用格式是: plot3(X,Y,Z ...

  2. hdu 4861 Couple doubi(数论)

    题目链接:hdu 4861 Couple doubi 题目大意:两个人进行游戏,桌上有k个球,第i个球的值为1i+2i+⋯+(p−1)i%p,两个人轮流取,假设DouBiNan的值大的话就输出YES, ...

  3. webBrowser中操作网页元素全攻略

    原文 webBrowser中操作网页元素全攻略 1.获取非input控件的值: webBrowser1.Document.All["控件ID"].InnerText; 或webBr ...

  4. static在C和C++中的用法和区别

    static主要有三个作用: (1)局部静态变量 (2)外部静态变量/函数 (3)静态数据成员/成员函数 前两种C和C++都有,第三种仅在C++中有,下面分别作以下介绍: 一.局部静态变量 在C/C+ ...

  5. 在webform中调用JS的技巧

    一,执行删除操作,点击按钮时弹出对话框询问是否确认删除,点击确定,删除并在删除完成后弹出删除成功:点击取消不删除 1.在aspx源 代码中加入JavaScript代码 <script langu ...

  6. MSSQL - 逻辑主键、业务主键和复合主键

    转载自:http://blog.csdn.net/sunrise918/article/details/5575054 这几天对逻辑主键.业务主键和复合主键进行了一些思考,也在网上搜索了一下相关的讨论 ...

  7. jquery如何在加载完iframe的内容后才进行下一步操作

    为iframe添加onload事件 ie使用attachEvent("onload",function(){}) firefox.chrome使用addEventListener( ...

  8. strip 命令的使用方法

    用途 通过除去绑定程序和符号调试程序使用的信息,降低扩展公共对象文件格式(XCOFF)的对象文件的大小. 语法 strip [ -V ] [ -r [ -l ] | -x [ -l ] | -t | ...

  9. Android 界面滑动实现---Scroller类 从源码和开发文档中学习(让你的布局动起来)

    在android学习中,动作交互是软件中重要的一部分,其中的Scroller就是提供了拖动效果的类,在网上,比如说一些Launcher实现滑屏都可以通过这个类去实现..   例子相关博文:Androi ...

  10. css3圆角代码

    div+css3普通圆角代码示例 <style type="text/css"> #round { width:200px; height:100px; margin: ...