博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ 2418 Hardwood Species( AVL-Tree )
阅读量:5318 次
发布时间:2019-06-14

本文共 3213 字,大约阅读时间需要 10 分钟。

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

转载于:https://www.cnblogs.com/zfyouxi/p/4224713.html

你可能感兴趣的文章
zabbix监控日志文件
查看>>
正则表达式
查看>>
pip install torch on windows, and the 'from torch._C import * ImportError: DLL load failed:' s...
查看>>
java基础(一):我对java的三个环境变量的简单理解和配置
查看>>
arcgis api 4.x for js 结合 Echarts4 实现散点图效果(附源码下载)
查看>>
YTU 2625: B 构造函数和析构函数
查看>>
apache自带压力测试工具ab的使用及解析
查看>>
C#使用Xamarin开发可移植移动应用(2.Xamarin.Forms布局,本篇很长,注意)附源码
查看>>
jenkins搭建
查看>>
C#中使用Split分隔字符串的技巧
查看>>
eclipse的调试方法的简单介绍
查看>>
加固linux
查看>>
IPSP问题
查看>>
10.17动手动脑
查看>>
WPF中Image显示本地图片
查看>>
Windows Phone 7你不知道的8件事
查看>>
实用拜占庭容错算法PBFT
查看>>
java的二叉树树一层层输出,Java构造二叉树、树形结构先序遍历、中序遍历、后序遍历...
查看>>
php仿阿里巴巴,php实现的仿阿里巴巴实现同类产品翻页
查看>>
Node 中异常收集与监控
查看>>