博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
1066 Root of AVL Tree
阅读量:7078 次
发布时间:2019-06-28

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

1066 Root of AVL Tree (25)(25 分)
An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child subtrees of any node differ by at most one; if at any time they differ by more than one, rebalancing is done to restore this property. Figures 1-4 illustrate the rotation rules.
 

 

 

Now given a sequence of insertions, you are supposed to tell the root of the resulting AVL tree.
Input Specification:
Each input file contains one test case. For each case, the first line contains a positive integer N (<=20) which is the total number of keys to be inserted. Then N distinct integer keys are given in the next line. All the numbers in a line are separated by a space.
Output Specification:
For each test case, print the root of the resulting AVL tree in one line.
Sample Input 1:
5
88 70 61 96 120
Sample Output 1:
70
Sample Input 2:
7
88 70 61 96 120 90 65
Sample Output 2:
88
 
题意:
给出AVL树的插入顺序,要求构建AVL树,并输出根结点的值。
 
思路:
AVL模板题,原理不赘述。详见《算法笔记》。
需要引起注意的是,若操作会改变结点的地址,则要
引用传值。如insert(),leftRotation(),rightRotation()操作。而updateHeight()操作只是改变了结点的数据域,并未改变结点的地址,所以无需加引用(当然,加引用也没关系)。
 
代码:
#include 
#include
using namespace std;struct Node{ int val; int height;//以该结点为根结点的子树高度 Node *lchild,*rchild; Node(int v):val(v),height(1),lchild(nullptr),rchild(nullptr){}};int getHeight(Node* pNode){ if(pNode==nullptr) return 0; else return pNode->height;}int getBalancedFactor(Node* pNode){ return getHeight(pNode->lchild)-getHeight(pNode->rchild);}void updateHeight(Node* pNode){ pNode->height=max(getHeight(pNode->lchild),getHeight(pNode->rchild))+1;}void leftRotation(Node* &pNode){ Node* temp=pNode->rchild; pNode->rchild=temp->lchild; temp->lchild=pNode; updateHeight(pNode); updateHeight(temp); pNode=temp;}void rightRotation(Node* &pNode){ Node* temp=pNode->lchild; pNode->lchild=temp->rchild; temp->rchild=pNode; updateHeight(pNode); updateHeight(temp); pNode=temp;}void insert(Node* &root,int val){ if(root==nullptr){ root=new Node(val); return; } if(val < root->val){ insert(root->lchild,val); updateHeight(root); if(getBalancedFactor(root)==2){ if(getBalancedFactor(root->lchild)==1){
//LL型 rightRotation(root); }else if(getBalancedFactor(root->lchild)==-1){
//LR型 leftRotation(root->lchild); rightRotation(root); } } }else{ insert(root->rchild,val); updateHeight(root); if(getBalancedFactor(root)==-2){ if(getBalancedFactor(root->rchild)==-1){
//RR型 leftRotation(root); }else if(getBalancedFactor(root->rchild)==1){
//RL型 rightRotation(root->rchild); leftRotation(root); } } }}int main(){ int n,val; Node* root=nullptr; scanf("%d",&n); for(int i=0;i
val); return 0;}

 

转载于:https://www.cnblogs.com/kkmjy/p/9528158.html

你可能感兴趣的文章
Tomcat抛出异常:ClientAbortException: java.net.SocketException: Connection
查看>>
关闭百度广告联盟广告推送
查看>>
Atitit 项目中的勋章体系,,mvp建设 ,荣典体系建设
查看>>
树莓派重装系统
查看>>
IIS-反向代理
查看>>
C# 之 串口数据侦听的实现
查看>>
白盒测试的测试用例设计有哪些方法
查看>>
sql group by+字段
查看>>
python+uwsgi导致redis无法长链接引起性能下降问题记录
查看>>
Android:关于声明文件中android:process属性说明
查看>>
ArcSDE中Compress与Compact的区别
查看>>
[Most.js] Create Streams From Single Values With Most.js
查看>>
jQuery文档加载完毕的几种写法
查看>>
windows server远程连接提示“终端服务器超出了最大允许连接”
查看>>
Spring MVC表单处理
查看>>
Oracle数据库迁移
查看>>
java图片截取组件ImageIO
查看>>
用Qemu搭建aarch32学习环境
查看>>
微信小程序把玩(三十七)location API
查看>>
实现一个超简单的开关
查看>>