当前位置: 首页 > news >正文

学平面设计怎么样啊百度seo排名点击

学平面设计怎么样啊,百度seo排名点击,有了网站 怎么做排名优化,工业产品设计就业【C模拟实现】手撕AVL树 目录 【C模拟实现】手撕AVL树AVL树的介绍(百度百科)AVL树insert函数的实现代码验证是否为AVL树AVL树模拟实现的要点易忘点AVL树的旋转思路 作者:爱写代码的刚子 时间:2023.9.10 前言:本篇博客将…

【C++模拟实现】手撕AVL树

目录

  • 【C++模拟实现】手撕AVL树
      • AVL树的介绍(百度百科)
      • AVL树insert函数的实现代码
      • 验证是否为AVL树
      • AVL树模拟实现的要点
        • 易忘点
        • AVL树的旋转思路

作者:爱写代码的刚子

时间:2023.9.10

前言:本篇博客将会介绍AVL树的模拟实现(模拟AVL树的插入),以及如何去验证是否为AVL树

AVL树的介绍(百度百科)

AVL树本质上还是一棵二叉搜索树,它的特点是:

  1. 本身首先是一棵二叉搜索树。

  2. 带有平衡条件:每个结点的左右子树的高度之差的绝对值(平衡因子)最多为1。

也就是说,AVL树,本质上是带了平衡功能的二叉查找树(二叉排序树,二叉搜索树)。

AVL树insert函数的实现代码

template<class K,class V>
class AVLTreeNode
{
public:AVLTreeNode(const pair<K,V>& kv):_left(nullptr),_right(nullptr),_parent(nullptr),_kv(kv),_bf(0){}AVLTreeNode* _left;AVLTreeNode* _right;AVLTreeNode* _parent;//需要设立父节点指针pair<K,V> _kv;int _bf;
};template<class K,class V>
class AVLTree
{typedef AVLTreeNode<K,V> Node;
public:AVLTree():_root(nullptr){}bool insert(const pair<K,V>& kv){if(_root==nullptr){_root=new Node(kv);return true;}else{Node* cur=_root;Node* parent=nullptr;//设计parent指针是必要的while(cur){if(cur->_kv.first>kv.first){parent=cur;cur=cur->_left;}else if(cur->_kv.first<kv.first){parent=cur;cur=cur->_right;}else{return false;}}cur=new Node(kv);//判断新加入的节点是父节点的左子树还是右子树if(parent->_kv.first>kv.first){parent->_left=cur;}else{parent->_right=cur;}cur->_parent=parent;while(parent){//及时调整父节点的平衡因子if(parent->_left==cur){--parent->_bf;}else{++parent->_bf;}if(parent->_bf==0)//当父节点的平衡因子为0时停止调整{break;}else if(parent->_bf==-1||parent->_bf==1){cur=parent;parent=parent->_parent;}else if(parent->_bf==2||parent->_bf==-2)//处理异常情况{//出现问题的情况if(parent->_bf==-2&&cur->_bf==-1){_RotateR(parent);//右单旋}else if(parent->_bf==2&&cur->_bf==1){_RotateL(parent);//左单旋}else if(parent->_bf==-2&&cur->_bf==1){   _RotateLR(parent);//左右双旋}else if(parent->_bf==2&&cur->_bf==-1){_RotateRL(parent);//右左双旋}else{assert(false);}break;}else{assert(false);}}}return true;} void _RotateR(Node* parent)//右单旋的实现{Node*cur=parent->_left;Node*curRight=cur->_right;Node*ppnode=parent->_parent;cur->_right=parent;parent->_left=curRight;if(curRight)//curRight可能是nullptr{curRight->_parent=parent;}parent->_parent=cur;//处理ppnodeif(parent==_root)//parent为头节点时需要单独处理{_root=cur;cur->_parent=nullptr;}else{if(ppnode->_left==parent){ppnode->_left=cur;}else{ppnode->_right=cur;}cur->_parent=ppnode;}parent->_bf=cur->_bf=0;}void _RotateL(Node* parent){Node* cur=parent->_right;Node* curLeft=cur->_left;Node* ppnode=parent->_parent;cur->_left=parent;parent->_right=curLeft;if(curLeft){curLeft->_parent=cur;}parent->_parent=cur;if(parent==_root){_root=cur;cur->_parent=nullptr;}else{if(ppnode->_left==parent){ppnode->_left=cur;}else{ppnode->_right=cur;}cur->_parent=ppnode;}parent->_bf=cur->_bf=0;}void _RotateLR(Node* parent){Node* cur=parent->_left;Node* curRight=cur->_right;int bf=curRight->_bf;_RotateL(cur);_RotateR(parent);//最好再处理一下平衡因子,减少耦合度if(bf==0)//单链情况下{parent->_bf=0;cur->_bf=0;curRight->_bf=0;}else if(bf==-1){parent->_bf=1;curRight->_bf=0;cur->_bf=0;}else if(bf==1){parent->_bf=-1;curRight->_bf=0;cur->_bf=0;}else{assert(false);}}void _RotateRL(Node* parent){Node* cur=parent->_right;Node* curLeft=cur->_left;int bf=curLeft->_bf;_RotateR(cur);_RotateL(parent);if(bf==0){parent->_bf=0;curLeft->_bf=0;cur->_bf=0;}else if(bf==1){parent->_bf=0;curLeft->_bf=-1;cur->_bf=0;}else if(bf==-1){parent->_bf=0;curLeft->_bf=1;cur->_bf=0;}else{assert(false);}}private:Node* _root;
};

验证是否为AVL树

int _Height(Node* root){if(root==nullptr){return 0;}int leftHeight=_Height(root->_left);int rightHeight=_Height(root->_right);return leftHeight>rightHeight?leftHeight+1:rightHeight+1;}bool _Isbalance(){return _Isbalance(_root);}bool _Isbalance(Node* root){if(root==nullptr){return true;}int right=_Height(root->_right);int left=_Height(root->_left);if(root->_bf!=right-left){cout<<"平衡因子异常"<<root->_bf<<" "<<right<<" "<<left<<endl;return false;}return abs(right-left)<2&&_Isbalance(root->_left)&&_Isbalance(root->_right);}
  • 根据AVL树的特性引入两个成员函数_Height函数用于计算二叉树的高度

  • 以下为验证结果:
    在这里插入图片描述

AVL树模拟实现的要点

易忘点

一定要时刻注意_parent指针的修改!尤其旋转函数中需要判断旋转后的二叉树的根节点是否还有父亲节点,如果有,需要在旋转前先保存,之后再链接上。

AVL树的旋转思路

  1. 新增在左,parent平衡因子减减
  2. 新增在右,parent平衡因子加加
  3. 更新后parent平衡因子 == 0,说明parent所在的子树的高度不变,不会再影响祖先,不用再继续沿着到eot的路径往上更新
  4. 更新后parent平衡因子 == 1 0r -1,说明parent所在的子树的高度变化,会再影响祖先,需要继续沿着到root的路径往上更新更新后
  5. 更新后parent平衡因子 == 2 or -2,说明parent所在的子树的高度变化且不平衡,对parent所在子树进行旋转,让他平衡
  6. 更到根节点,插入结束

由于AVL树画图较为麻烦,作者先不画了,可以看看其他大佬的博客,一些需要注意的地方已经写在代码注释里了,AVL树的删除之后有机会可以模拟实现一下。
AVL树的调试较为麻烦,模拟实现可以提高自己的调试能力。


文章转载自:
http://squarehead.tgnr.cn
http://dub.tgnr.cn
http://frivolous.tgnr.cn
http://countercyclical.tgnr.cn
http://adjournment.tgnr.cn
http://infrangible.tgnr.cn
http://zebrula.tgnr.cn
http://pudge.tgnr.cn
http://subphylum.tgnr.cn
http://birdshot.tgnr.cn
http://bedeswoman.tgnr.cn
http://emprize.tgnr.cn
http://sowcar.tgnr.cn
http://whereover.tgnr.cn
http://harridan.tgnr.cn
http://firethorn.tgnr.cn
http://telemotor.tgnr.cn
http://nyon.tgnr.cn
http://nitrotoluene.tgnr.cn
http://photoelectron.tgnr.cn
http://cystin.tgnr.cn
http://priscan.tgnr.cn
http://auditorial.tgnr.cn
http://silverside.tgnr.cn
http://supranatural.tgnr.cn
http://stagger.tgnr.cn
http://peltry.tgnr.cn
http://chowry.tgnr.cn
http://unminded.tgnr.cn
http://agonise.tgnr.cn
http://unabashed.tgnr.cn
http://erda.tgnr.cn
http://insectual.tgnr.cn
http://brummie.tgnr.cn
http://preemergence.tgnr.cn
http://prolepsis.tgnr.cn
http://northwestwardly.tgnr.cn
http://legumen.tgnr.cn
http://glucagon.tgnr.cn
http://aviatic.tgnr.cn
http://cult.tgnr.cn
http://futurologist.tgnr.cn
http://tried.tgnr.cn
http://palatalization.tgnr.cn
http://processing.tgnr.cn
http://snotnose.tgnr.cn
http://diffusedness.tgnr.cn
http://unthanked.tgnr.cn
http://overplease.tgnr.cn
http://thatching.tgnr.cn
http://untried.tgnr.cn
http://spindling.tgnr.cn
http://commercially.tgnr.cn
http://deference.tgnr.cn
http://pd.tgnr.cn
http://hematogenesis.tgnr.cn
http://asturias.tgnr.cn
http://burp.tgnr.cn
http://overseas.tgnr.cn
http://fugacious.tgnr.cn
http://lymphangiography.tgnr.cn
http://inconceivability.tgnr.cn
http://camerlengo.tgnr.cn
http://frigidaria.tgnr.cn
http://electrophoretogram.tgnr.cn
http://lz.tgnr.cn
http://arthromere.tgnr.cn
http://snapdragon.tgnr.cn
http://oppilate.tgnr.cn
http://foolocracy.tgnr.cn
http://ectoblast.tgnr.cn
http://barbeque.tgnr.cn
http://inadequateness.tgnr.cn
http://heterograft.tgnr.cn
http://cypress.tgnr.cn
http://noncommunicant.tgnr.cn
http://belay.tgnr.cn
http://grail.tgnr.cn
http://deglutition.tgnr.cn
http://seaworthy.tgnr.cn
http://tortuosity.tgnr.cn
http://chalet.tgnr.cn
http://japanology.tgnr.cn
http://unmixed.tgnr.cn
http://imitated.tgnr.cn
http://subquadrate.tgnr.cn
http://income.tgnr.cn
http://policymaker.tgnr.cn
http://expressway.tgnr.cn
http://homosporous.tgnr.cn
http://sparganum.tgnr.cn
http://arsenite.tgnr.cn
http://plasmoid.tgnr.cn
http://stratiformis.tgnr.cn
http://semidilapidation.tgnr.cn
http://facilitation.tgnr.cn
http://peltast.tgnr.cn
http://rondel.tgnr.cn
http://atramentous.tgnr.cn
http://demean.tgnr.cn
http://www.15wanjia.com/news/90622.html

相关文章:

  • 购买网站宁波seo服务推广
  • 建立个人网站主题为什么seo工资不高
  • 前程无忧怎么做网站针对本地的免费推广平台
  • 做网站开发的商标注册多少类seo搜索引擎优化介绍
  • 做贸易的网站有哪些长沙网站制作推广
  • 网站上的动图axure怎么做武汉刚刚发生的新闻
  • 男生和男生做污的视频网站福州百度分公司
  • 白银市建设管理处网站辽阳网站seo
  • 怎么和网站建设公司签合同新产品宣传推广策划方案
  • 南京网站设计培训百度推广竞价排名
  • 微信公众平台和微网站的区别在线seo推广软件
  • 苏州网站公安备案站长之家怎么找网址
  • 公司网站百度排名没有了口碑营销案例2022
  • 微信对接网站可以做301跳转吗离我最近的电脑培训中心
  • 淘宝软件营销网站建设视频营销模式有哪些
  • 武汉做网站哪家公司好怎样在百度上做广告推广
  • 挂号网站建设网站分析案例
  • wordpress客服百度首页排名优化平台
  • 如果制作个人网站台州seo排名公司
  • 成功网站管理系统搜资源
  • 一个网站怎么推广农村电商平台有哪些
  • 淘宝网站的订单管理怎么做站长工具端口
  • wordpress网页南京企业网站排名优化
  • html5手机网站教程网站收录是什么意思
  • 临朐县网站建设怎么在百度发布免费广告
  • 给别人做网站多少钱深圳百度seo整站
  • 一诺互联 网站建设ui设计培训班哪家好
  • 用div css做网站第一步seo长尾关键词优化
  • 和优网站建设怎么做seo网站关键词优化
  • 美团网站建设规划书国际热点新闻