博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
PAT A1066
阅读量:6982 次
发布时间:2019-06-27

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

clipboard.png

平衡二叉树AVL,写过总结,这里不再赘述;

#include
#include
#include
#include
#include
#include
using namespace std;using std::vector;using std::queue;const int maxn=30;int data[maxn];struct node{ int data; int height; node* lchild; node* rchild;};node* root;node* newNode(int x){ node* root=new node; root->data=x; root->height=1; root->lchild=NULL; root->rchild=NULL; return root;}int getHeight(node* root){ if(root==NULL) return 0; return root->height;}void updateHeight(node* root){ root->height=max(getHeight(root->lchild),getHeight(root->rchild))+1;}void L(node* &root){ node* temp=root->rchild; root->rchild=temp->lchild; temp->lchild=root; updateHeight(root); updateHeight(temp); root=temp;}void R(node* &root){ node* temp=root->lchild; root->lchild=temp->rchild; temp->rchild=root; updateHeight(root); updateHeight(temp); root=temp;}int getBalanceFactor(node* root){ return getHeight(root->lchild)-getHeight(root->rchild);}void insert_node(node* &root,int v){ if(root==NULL){ root=newNode(v); return; } if(v
data){ insert_node(root->lchild,v); updateHeight(root); if(getBalanceFactor(root)==2){ if(getBalanceFactor(root->lchild)==1){ R(root); }else if(getBalanceFactor(root->lchild)==-1){ L(root->lchild); R(root); } } }else{ insert_node(root->rchild,v); updateHeight(root); if(getBalanceFactor(root)==-2){ if(getBalanceFactor(root->rchild)==-1){ L(root); }else if(getBalanceFactor(root->rchild)==1){ R(root->rchild); L(root); } } }}node* create(int data[],int n){ node* root=NULL; for(int i=0;i
data); system("pause"); return 0;}

转载地址:http://iatpl.baihongyu.com/

你可能感兴趣的文章
阿里云人工智能ET夺肺结节诊断世界冠军
查看>>
研究人员发现利用Excel宏可发起跳板攻击
查看>>
绿盟科技发布OpenSSL高危漏洞技术分析与防护方案 G20成员国美国、中国、德国受影响较大...
查看>>
《VMware Virtual SAN权威指南》一2.2.4 容量层设备
查看>>
物联网发展年报显示 2016年智能家居市场快速增长
查看>>
如何在React中做到jQuery-free
查看>>
4G+宽带高歌猛进:移动双线虐杀联通
查看>>
带你了解超大规模数据中心究竟有何不同?
查看>>
用Python实现每秒处理120万次HTTP请求
查看>>
Android单元测试 - 几个重要问题
查看>>
DNS服务器不能响应的四大解决办法
查看>>
美国税局再遭攻击:原是偷来的社会安全号码作祟
查看>>
如何在Kali Linux中安装Google Chrome浏览器
查看>>
勒索软件防不胜防? 要先从了解它开始
查看>>
大数据精准医疗解读遗传密码 未来医疗健康的变革
查看>>
神经网络基础:七种网络单元,四种层连接方式
查看>>
2014末,Surface Pro 3叫好不叫座只是价格问题?
查看>>
Arimo利用Alluxio的内存能力提升深度学习模型的结果效率(Time-to-Result)
查看>>
代号“沙尘暴”:黑客剑指日本关键基础设施
查看>>
光纤光缆市场需求高于预期 我国将迎来流量经济
查看>>