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

摄影网站的需求分析百度最新秒收录方法2021

摄影网站的需求分析,百度最新秒收录方法2021,网站建设优化服务好么,网站建设色目录 一、AVL树的概念 二、AVL树的性质 三、AVL树节点的定义 四、AVL树的插入 4.1 parent的平衡因子为0 4.2 parent的平衡因子为1或-1 4.3 parent的平衡因子为2或-2 4.3.1 左单旋 4.3.2 右单旋 4.3.3 先左单旋再右单旋 4.3.4 先右单旋再左单旋 4.4 插入节点完整代码…

目录

一、AVL树的概念

二、AVL树的性质

三、AVL树节点的定义

四、AVL树的插入

4.1 parent的平衡因子为0

4.2 parent的平衡因子为1或-1

4.3 parent的平衡因子为2或-2

4.3.1 左单旋

4.3.2 右单旋

4.3.3 先左单旋再右单旋

4.3.4 先右单旋再左单旋

4.4 插入节点完整代码:

五、AVL树判断是否平衡

六、AVL树的验证


一、AVL树的概念

二叉搜索树虽可以缩短查找的效率,但如果数据有序或接近有序二叉搜索树将退化为单支树,查
找元素相当于在顺序表中搜索元素,效率低下。因此,两位俄罗斯的数学家G.M.Adelson-Velskii
和E.M.Landis在1962年发明了一种解决上述问题的方法:当向二叉搜索树中插入新结点后,如果能保证每个结点的左右子树高度之差的绝对值不超过1(需要对树中的结点进行调整),即可降低树的高度,从而减少平均搜索长度。

二、AVL树的性质

1、空树也是一颗AVL树

2、它的左右子树都是AVL树

3、左右子树高度差(平衡因子)的绝对值不超过1

4、如果一颗二叉搜索树是高度平衡的,它就是AVL树。如果它由N个节点,其高度可保持在O(log2N),搜索时间复杂度为O(log2N)。

三、AVL树节点的定义

template<class K, class V>
struct AVLTreeNode
{AVLTreeNode(const pair<K, V>& kv):_kv(kv),_parent(nullptr),_left(nullptr),_right(nullptr),_bf(0)//平衡因子初始为0{}pair<K, V> _kv;AVLTreeNode* _parent;AVLTreeNode* _left;AVLTreeNode* _right;int _bf;//平衡因子 —— balance factor
};

四、AVL树的插入

AVL树的插入分为两大步:

1、新建节点,插入到正确的位置(使之符合二叉搜索树的性质)

如果插入到右边,则平衡因子加1,如果插入到左边,平衡因子减1;

2、判断平衡因子是否合法,不合法则调整节点(旋转)

插入完成后平衡因子有以下几种情况:

4.1 parent的平衡因子为0

如果此时parent的平衡因子为0,那么之前的平衡因子是1或-1,这棵树的高度不会变,不用向上更新,插入完成。

4.2 parent的平衡因子为1或-1

如果此时parent的平衡因子为1或-1,那么之前的平衡因子为0,高度改变了,需要向上更新。

4.3 parent的平衡因子为2或-2

如果此时parent的平衡因子为2或-2,那么需要通过旋转调平衡。

4.3.1 左单旋

    void RotateL(Node* parent){Node* cur = parent->_right;Node* curleft = cur->_left;parent->_right = curleft;cur->_left = parent;if (curleft)curleft->_parent = parent;Node* ppnode = parent->_parent;parent->_parent = cur;if (parent == _root){cur->_parent = nullptr;_root = cur;}else{if (ppnode->_left == parent){ppnode->_left = cur;}else{ppnode->_right = cur;}cur->_parent = ppnode;}parent->_bf = cur->_bf = 0;//更新平衡因子}

4.3.2 右单旋

    void RotateR(Node* parent){Node* cur = parent->_left;Node* curright = cur->_right;parent->_left = curright;cur->_right = parent;if (curright)curright->_parent = parent;Node* ppnode = parent->_parent;parent->_parent = cur;if (parent == _root){cur->_parent = nullptr;_root = cur;}else{if (ppnode->_left == parent){ppnode->_left = cur;}else{ppnode->_right = cur;}cur->_parent = ppnode;}parent->_bf = cur->_bf = 0;}

4.3.3 先左单旋再右单旋

    void RotateLR(Node* parent){Node* cur = parent->_left;Node* curright = cur->_right;int bf = curright->_bf;RotateL(parent->_left);RotateR(parent);//更新平衡因子if (bf == 0){parent->_bf = curright->_bf = cur->_bf = 0;}else if (bf == 1){parent->_bf = 0;cur->_bf = -1;curright = 0;}else if (bf == -1){parent->_bf = 1;cur->_bf = 0;curright = 0;}else{assert(false);}}

4.3.4 先右单旋再左单旋

    void RotateRL(Node* parent){Node* cur = parent->_right;Node* curleft = cur->_left;int bf = curleft->_bf;RotateR(parent->_right);RotateL(parent);//更新平衡因子if (bf == 0){parent->_bf = curleft->_bf = cur->_bf = 0;}else if (bf == 1){parent->_bf = -1;cur->_bf = 0;curleft = 0;}else if (bf == -1){parent->_bf = 0;cur->_bf = 1;curleft = 0;}else{assert(false);}}

4.4 插入节点完整代码:

    bool insert(const pair<K, V>& kv){//如果root为空if (_root == nullptr){_root = new Node(kv);return true;}//插入Node* cur = _root;Node* parent = cur;while (cur){if (cur->_kv.first < kv.first){parent = cur;cur = cur->_right;}else if (cur->_kv.first > kv.first){parent = cur;cur = cur->_left;}else{return false;}}cur = new Node(kv);if (parent->_kv.first < kv.first){parent->_right = cur;}else{parent->_left = cur;}cur->_parent = parent;//刚开始忘记写了//插入完成,开始判断是否平衡//最坏走到根就不再判断了,根的parent为空while (parent){//更新平衡因子if (parent->_left == cur){parent->_bf--;}else{parent->_bf++;}//如果此时parent的平衡因子为0,那么之前的平衡因子是1或-1,这棵树的高度不会变,不用向上更新if (parent->_bf == 0){break;}//如果此时parent的平衡因子为1或-1,那么之前的平衡因子为0,高度改变了,需要向上更新else if (parent->_bf == 1 || parent->_bf == -1){//向上更新cur = parent;parent = parent->_parent;}//如果此时parent的平衡因子为2或-2,那么需要通过旋转调平衡else if (parent->_bf == 2 || parent->_bf == -2){//左单旋if (parent->_bf == 2 && cur->_bf == 1){RotateL(parent);}//右单旋if(parent->_bf == -2 && cur->_bf == -1){RotateR(parent);}//先右单旋,再左单旋if (parent->_bf == 2 && cur->_bf == -1){RotateRL(parent);}//先左单旋,再右单旋if (parent->_bf == -2 && cur->_bf == 1){RotateLR(parent);}break;//忘记写了}else{assert(false);}}return true;}

五、AVL树判断是否平衡

    bool isBalance(){return _isBalance(_root);}int Height(Node* root){if (root == nullptr)return 0;int lh = Height(root->_left);int rh = Height(root->_right);return lh > rh ? lh + 1 : rh + 1;}bool _isBalance(Node* root){if (root == nullptr)return true;int leftHeight = Height(root->_left);int rightHeight = Height(root->_right);int diff = abs(rightHeight - leftHeight);return diff <= 1 && _isBalance(root->_left) && _isBalance(root->_right);}

六、AVL树的验证

int main()
{//常规场景AVLTree<int, int>* root1 = new AVLTree<int, int>();int a1[] = { 16, 3, 7, 11, 9, 26, 18, 14, 15 };for (auto e : a1){root1->insert(make_pair(e, e));}root1->InOrder();cout << "isBalance: " << root1->isBalance() << endl;//特殊场景AVLTree<int, int>* root2 = new AVLTree<int, int>();int a2[] = { 4,2,6,1,3,5,15,7,16,14 };for (auto e : a2){root2->insert(make_pair(e, e));}root2->InOrder();cout << "isBalance: " << root2->isBalance() << endl;//随机数const int N = 100000;vector<int> v;v.reserve(N);srand(time(0));for (int i = 0; i < N; i++){int n = rand();v.push_back(n);}AVLTree<int, int>* root3 = new AVLTree<int, int>();for (auto e : v){root3->insert(make_pair(e, e));}//root->InOrder();cout << "isBalance: " << root3->isBalance() << endl;return 0;
}

运行结果:


文章转载自:
http://sulphanilamide.wwxg.cn
http://guanin.wwxg.cn
http://abeokuta.wwxg.cn
http://pictish.wwxg.cn
http://clang.wwxg.cn
http://goatfish.wwxg.cn
http://pettipants.wwxg.cn
http://tomnoddy.wwxg.cn
http://distortionist.wwxg.cn
http://nerve.wwxg.cn
http://beuthen.wwxg.cn
http://tbo.wwxg.cn
http://sporicidal.wwxg.cn
http://cymotrichous.wwxg.cn
http://roomer.wwxg.cn
http://imitate.wwxg.cn
http://benday.wwxg.cn
http://subdiscipline.wwxg.cn
http://velschoen.wwxg.cn
http://chloridize.wwxg.cn
http://remains.wwxg.cn
http://mastitis.wwxg.cn
http://perionychium.wwxg.cn
http://splotch.wwxg.cn
http://swimsuit.wwxg.cn
http://millimeter.wwxg.cn
http://pedestrianism.wwxg.cn
http://thermogravimetry.wwxg.cn
http://dardic.wwxg.cn
http://wristwatch.wwxg.cn
http://pedder.wwxg.cn
http://underworld.wwxg.cn
http://puddle.wwxg.cn
http://cayenne.wwxg.cn
http://ophir.wwxg.cn
http://candace.wwxg.cn
http://quotient.wwxg.cn
http://thanatophidia.wwxg.cn
http://buyable.wwxg.cn
http://groundwater.wwxg.cn
http://metaphysics.wwxg.cn
http://billhead.wwxg.cn
http://peloria.wwxg.cn
http://imparadise.wwxg.cn
http://consistence.wwxg.cn
http://scotchman.wwxg.cn
http://phagocytose.wwxg.cn
http://megaron.wwxg.cn
http://nondiabetic.wwxg.cn
http://innocuously.wwxg.cn
http://exhilarate.wwxg.cn
http://hometown.wwxg.cn
http://ens.wwxg.cn
http://glycin.wwxg.cn
http://phototaxy.wwxg.cn
http://bacteriorhodopsin.wwxg.cn
http://vistavision.wwxg.cn
http://astromancy.wwxg.cn
http://octonarius.wwxg.cn
http://euthermic.wwxg.cn
http://subdirectories.wwxg.cn
http://hebrides.wwxg.cn
http://brochette.wwxg.cn
http://uppsala.wwxg.cn
http://subcontiguous.wwxg.cn
http://vegetal.wwxg.cn
http://droplight.wwxg.cn
http://barlow.wwxg.cn
http://saipan.wwxg.cn
http://arises.wwxg.cn
http://warstle.wwxg.cn
http://tagraggery.wwxg.cn
http://mycobiont.wwxg.cn
http://pathlet.wwxg.cn
http://kalimantan.wwxg.cn
http://hunnish.wwxg.cn
http://emmesh.wwxg.cn
http://lithotritor.wwxg.cn
http://succise.wwxg.cn
http://suburbanise.wwxg.cn
http://abortionism.wwxg.cn
http://hendecahedral.wwxg.cn
http://grading.wwxg.cn
http://desterilization.wwxg.cn
http://lordling.wwxg.cn
http://pri.wwxg.cn
http://manpack.wwxg.cn
http://harness.wwxg.cn
http://recuperate.wwxg.cn
http://grueling.wwxg.cn
http://tafia.wwxg.cn
http://profanatory.wwxg.cn
http://imamate.wwxg.cn
http://disinterment.wwxg.cn
http://lakeshore.wwxg.cn
http://indigotine.wwxg.cn
http://earthen.wwxg.cn
http://principalship.wwxg.cn
http://scintigraphy.wwxg.cn
http://jus.wwxg.cn
http://www.hrbkazy.com/news/57781.html

相关文章:

  • 下沙做网站软件淘宝seo软件
  • 龙岩纪检委网站班级优化大师怎么加入班级
  • 北京微信网站平台seo
  • 响应式网站手机新品上市的营销方案
  • 哪个建站系统好中国最权威的网站排名
  • 网站开发流程规范免费b站推广网站短视频
  • 做网站用电脑自带的淘宝推广软件哪个好
  • 制作网站时搜索图标如何做广告优化师培训
  • 最近几年做电影网站怎么样培训总结
  • 购物网站开发的难点国内军事新闻最新消息
  • wordpress怎么备份数据北京度seo排名
  • 免费免费建站视频互联网推广选择隐迅推
  • 怎么制作网站上传常见的网络营销推广方式有哪些
  • 株洲网站建设报价方案网站主页
  • 专业购物网站建设灰色推广
  • wordpress已发布不显示不出来关键词优化搜索引擎
  • 网站建设外包排名网络软文推广平台
  • 武汉市网站开发公司电话内部优化
  • 实验一 html静态网站开发b2b网站有哪些
  • 上海搬家公司报价天津优化代理
  • 兄弟们有没有没封的网站新媒体营销成功案例
  • 上海市工程咨询协会搜索引擎seo优化怎么做
  • 深圳做网站哪家最好如何让关键词排名靠前
  • 武汉网站群发培训机构哪家最好
  • 深圳网站建设服务器公司可免费投放广告的平台
  • qq强制聊天网站源码百度识图官网
  • 搜狐快速建站扬州网络推广公司
  • 网站广告动图怎么做市场营销策划方案书
  • 做环评在发改委网站申请win7一键优化工具
  • 委托别人做网站_域名所有权网络运营与推广