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

江西网站建设费用网络推广计划方案

江西网站建设费用,网络推广计划方案,专门做旅游保险的网站,apmserv网站模板目录 一. AVL的概念 二 AVL树的插入 2.1先按二叉搜索树的规则插入 2.2 AVL的重点:平衡因子更新 3.1 更新后parent的平衡因子等于0。 3.2 更新后parent的平衡因子等于1 或 -1,需要继续往上更新。 3.3 更新后parent的平衡因子等于2 或 -2,需…

目录

一. AVL的概念

二 AVL树的插入

2.1先按二叉搜索树的规则插入

2.2 AVL的重点:平衡因子更新

3.1 更新后parent的平衡因子等于0。

        3.2 更新后parent的平衡因子等于1 或 -1,需要继续往上更新。

 3.3 更新后parent的平衡因子等于2 或 -2,需要使用旋转处理。

三.旋转 和 平衡因子等于2 或 -2 的处理

1.右单旋(把左孩子变成爸爸) 

2.左单旋(把右孩子变成爸爸) 

3.左右双旋(把LR_Child 变为爸爸)

4.左右双旋(把RL_Child 变为爸爸)

总代码:


一. AVL的概念

1.AVL树是最先发明的自平衡二叉查找树,AVL是一颗空树,或者具备下列性质的二叉搜索树:它的
左右子树都是AV树,且 左右子树的高度差的绝对值不超过1AVL树是一颗高度平衡搜索二叉树,通过控制高度差去控制平衡!!!

2.AVL树实现这里我们引入一个平衡因子(balance factor)的概念,每个结点都有一个平衡因子,任何结点的平衡因子等于右子树的高度减去左子树的高度,也就是说任何结点的平衡因子等于0/1/-1
AVL树并不是必须要平衡因子,但是有了平衡因子可以更方便我们去进行观察和控制树是否平衡
就像一个风向标一样。


二 AVL树的插入

AVL节点 的大概结构                                                  AVL树 的大概结构

  


2.1先按二叉搜索树的规则插入

具体可以看这篇

二叉搜索树-CSDN博客

这里是二叉搜索树的,简单的代码总结


2.2 AVL的重点:平衡因子更新

 因为:左右子树的高度差的绝对值不超过1 。

这里我们可以规定

1. 平衡因子 = 右子树高度 - 左子树高度 。

2. 插入结点时,会增加高度,如果新增结点 在parent的右子树,parent的平衡因子++,新增结点在parent的左子树,parent平衡因子--,parent的平衡因子初始化为 0.

3. parent的停止更新条件分为3种:



3.1 更新后parent的平衡因子等于0。


        3.2 更新后parent的平衡因子等于1 或 -1,需要继续往上更新。


上面的总代码:


 3.3 更新后parent的平衡因子等于2 或 -2,需要使用旋转处理。

下面为具体的分析:


三.旋转 和 平衡因子等于2 或 -2 的处理

旋转总共分为四种,左单旋/右单旋/左右双旋/右左双旋。

这里我们规定:

1. 在parent的左边 插入孩子,parent的平衡因子 -  1

2 .在parent的右边 插入孩子,parent的平衡因子 + 1


1.右单旋(把左孩子变成爸爸) 

需要 单纯的左边高!!! 才可以使用

比如 if(parent->_bf == -2 && SubR->_bf == -1)

如图:

分析:

因为 左右子树的高度差的绝对值不超过1 

我们需要把 a 往上提,把parent往下压 ,让SubL变成爸爸 才可以解决。

总的来说就是,左边高就把左边提上来,把右边压下去。

在这种情况下 他们的平衡因子就会为0.

代码为:

RotateR:


2.左单旋(把右孩子变成爸爸) 

需要 单纯的右边高!!! 才可以使用

比如 if(parent->_bf == 2 && SubR->_bf == 1)

如图:

分析:

因为 左右子树的高度差的绝对值不超过1 

我们需要把 a 往上提,把parent往下压 ,让SubR变成爸爸 才可以解决。

总的来说就是,右边高就把右边提上来,把左边压下去。

在这种情况下 他们的平衡因子就会为0.

代码为:

RotateL:


3.左右双旋(把LR_Child 变为爸爸)

需要 左边的右边高!!! 才可以使用

如图所示:

当没插入节点时:

在LR_Child 的左边插入节点:

具体过程

1. 让节点5 左旋转:

2.让节点8 右旋转

平衡因子:

在LR_Child 为空时

平衡因子 都为0.

在LR_Child 的左边插入节点:

LR_Child  平衡因子 为 0

L_Child 平衡因子 为 0

parent 平衡因子为 1

在LR_Child 的右边插入节点:

LR_Child  平衡因子 为 0

L_Child 平衡因子 为 -1

parent 平衡因子为 0

代码:


4.左右双旋(把RL_Child 变为爸爸)

太长了,随便写点了,脑子坏了!!!

在LR_Child 为空时

平衡因子 都为0.

在LR_Child 的右边插入节点:

RL_Child  平衡因子 为 0

R_Child 平衡因子 为 0

parent 平衡因子为 -1

在RL_Child 的左边插入节点:

RL_Child  平衡因子 为 0

R_Child 平衡因子 为 1

parent 平衡因子为 0

代码:


总代码:

#pragma once
#include<iostream>
#include<assert.h>
using namespace std;
template<class T>struct AVLTreeNode
{AVLTreeNode(const T& data = T()): _pLeft(nullptr), _pRight(nullptr), _pParent(nullptr), _data(data), _bf(0){}AVLTreeNode<T>* _pLeft;AVLTreeNode<T>* _pRight;AVLTreeNode<T>* _pParent;T _data;int _bf;   // 节点的平衡因子
};// AVL: 二叉搜索树 + 平衡因子的限制
template<class T>
class AVLTree
{typedef AVLTreeNode<T> Node;
public:AVLTree(): _pRoot(nullptr){}// 在AVL树中插入值为data的节点bool Insert(const T& data){//如果树为空就建立一个根节点if (_pRoot == nullptr){_pRoot = new Node(data);}//树不为空else{Node* parent = nullptr;Node* tmp = _pRoot;//用cur找位置 while (tmp){//插入值比当前结点小往左走if (tmp->_data < data){parent = tmp;tmp = tmp->_pRight;}//插入值比当前结点大往右走else if (tmp->_data > data){parent = tmp;tmp = tmp->_pLeft;}else{assert(false);}}//在parent的左边或者右边插入插入Node* cur = new Node(data);if (parent->_data < data){parent->_pRight = cur;cur->_pParent = parent;}else if (parent->_data > data){parent->_pLeft = cur;cur->_pParent = parent;}//最困难的平衡因子部分while (parent){//cur插入在右边,平衡因子++if (cur == parent->_pRight)parent->_bf++;//反之亦然elseparent->_bf--;//平衡因子为0 结束if (parent->_bf == 0)break;//平衡因子为1 或 -1,往上更新else if(parent->_bf == 1 || parent->_bf == -1){cur = parent;parent = parent->_pParent;}else if(parent->_bf == -2 || parent->_bf == 2){//...// //单纯左边高if (parent->_bf == -2 && cur->_bf == -1){RotateR(parent);parent->_bf = 0;cur->_bf = 0;}//单纯右边高else if (parent->_bf == 2 && cur->_bf == 1){RotateL(parent);parent->_bf = 0;cur->_bf = 0;}//左边的右边高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;}// AVL树的验证bool IsAVLTree(){return _Height(_pRoot);}void InOrder(){return _InOrder(_pRoot);}size_t Height(){return _Height(_pRoot);}
private:// 根据AVL树的概念验证pRoot是否为有效的AVL树bool _IsAVLTree(Node* pRoot){if (pRoot == nullptr)return true;int left = _Height(pRoot->_pLeft);int right = _Height(pRoot->_pRight);int differ = right - left;if (differ >= 2 || differ <= -2)return false;if (differ != pRoot->_bf)return false;return _IsAVLTree(pRoot->_pLeft) && _IsAVLTree(pRoot->_pRight);}void _InOrder(Node* cur){if (cur == nullptr)return;_InOrder(cur->_pLeft);cout << cur->_data << " ";_InOrder(cur->_pRight);}size_t _Height(Node* pRoot){if (pRoot == nullptr)return 0;size_t left = _Height(pRoot->_pLeft);size_t right = _Height(pRoot->_pRight);return right > left ? right + 1 : left + 1;}// 右单旋void RotateR(Node* pParent){Node* L_Child = pParent->_pLeft;Node* LR_Child = L_Child->_pRight;//左边孩子的 右边的孩子 和parent相互连接if (LR_Child)LR_Child->_pParent = pParent;pParent->_pLeft = LR_Child;//左孩子变在上面 右边连接parent ,grandfather 相互连接Node* grandfather = pParent->_pParent;pParent->_pParent = L_Child;L_Child->_pRight = pParent;L_Child->_pParent = grandfather;if (grandfather == nullptr){_pRoot = L_Child;}else{if (grandfather->_pLeft == pParent)grandfather->_pLeft = L_Child;elsegrandfather->_pRight = L_Child;}}// 左单旋void RotateL(Node* pParent){Node* R_Child = pParent->_pRight;Node* RL_Child = R_Child->_pLeft;//if(RL_Child)RL_Child->_pParent = pParent;pParent->_pRight = RL_Child;Node* grandfather = pParent->_pParent;pParent->_pParent = R_Child;R_Child->_pLeft = pParent;R_Child->_pParent = grandfather;if (grandfather == nullptr){_pRoot = R_Child;}else{if (grandfather->_pLeft == pParent)grandfather->_pLeft = R_Child;elsegrandfather->_pRight = R_Child;}}// 右左双旋void RotateRL(Node* pParent){Node* RChild = pParent->_pRight;Node* RLChild = RChild->_pLeft;//旋转完之后再用bf来判断平衡因子int bf = RLChild->_bf;RotateR(RChild);RotateL(pParent);if (bf == 0){pParent->_bf = 0;RChild->_bf = 0;RLChild->_bf = 0;}else if (bf == 1){RLChild->_bf = 0;RChild->_bf = 0;pParent->_bf = -1;}else if (bf == -1){RLChild->_bf = 0;RChild->_bf = 1;pParent->_bf = 0;}elseassert(false);}// 左右双旋void RotateLR(Node* pParent){Node* LChild = pParent->_pLeft;Node* LRChild = LChild->_pRight;int bf = LRChild->_bf;RotateL(LChild);RotateR(pParent);if (bf == 0){LRChild->_bf = 0;pParent->_bf = 0;LChild->_bf = 0;}else if (bf == 1){LRChild->_bf = 0;LChild->_bf = -1;pParent->_bf = 0;}else if (bf == -1){LRChild->_bf = 0;LChild->_bf = 0;pParent->_bf = 1;}elseassert(false);}private:Node* _pRoot;};


文章转载自:
http://dinncodownloadable.stkw.cn
http://dinncopanmixis.stkw.cn
http://dinncofoi.stkw.cn
http://dinncotransportation.stkw.cn
http://dinncopoliceman.stkw.cn
http://dinncoziff.stkw.cn
http://dinncorelapse.stkw.cn
http://dinncobrutality.stkw.cn
http://dinncoslogan.stkw.cn
http://dinncojuridic.stkw.cn
http://dinncopensioner.stkw.cn
http://dinncoshache.stkw.cn
http://dinncoexacting.stkw.cn
http://dinncoreconcentrate.stkw.cn
http://dinncochimerism.stkw.cn
http://dinncotraveller.stkw.cn
http://dinncosri.stkw.cn
http://dinncomaorilander.stkw.cn
http://dinncohomothallic.stkw.cn
http://dinncofattypuff.stkw.cn
http://dinncomesmerize.stkw.cn
http://dinnconarrows.stkw.cn
http://dinncomidiron.stkw.cn
http://dinncountangle.stkw.cn
http://dinncofootfall.stkw.cn
http://dinncoaltitudinal.stkw.cn
http://dinncogorse.stkw.cn
http://dinncoassociate.stkw.cn
http://dinncocatania.stkw.cn
http://dinncolagthing.stkw.cn
http://dinncolivability.stkw.cn
http://dinncoinane.stkw.cn
http://dinncoretinaculum.stkw.cn
http://dinncosuburbicarian.stkw.cn
http://dinncotransship.stkw.cn
http://dinncophotopigment.stkw.cn
http://dinncopodalic.stkw.cn
http://dinncoapiece.stkw.cn
http://dinncomillimicron.stkw.cn
http://dinncotuneless.stkw.cn
http://dinncohex.stkw.cn
http://dinncofuribund.stkw.cn
http://dinncointroject.stkw.cn
http://dinncovegan.stkw.cn
http://dinncogilgai.stkw.cn
http://dinncotiswin.stkw.cn
http://dinncocoachwood.stkw.cn
http://dinncoconsumingly.stkw.cn
http://dinncooutstation.stkw.cn
http://dinncopsoas.stkw.cn
http://dinncoeudora.stkw.cn
http://dinncountrained.stkw.cn
http://dinncosarsa.stkw.cn
http://dinncoenglobe.stkw.cn
http://dinncoepiglottal.stkw.cn
http://dinncomilium.stkw.cn
http://dinncodimensional.stkw.cn
http://dinncomuskiness.stkw.cn
http://dinncoswedenborgian.stkw.cn
http://dinncorebirth.stkw.cn
http://dinncopulvinus.stkw.cn
http://dinncountitled.stkw.cn
http://dinncoconfinement.stkw.cn
http://dinncowisteria.stkw.cn
http://dinnconataraja.stkw.cn
http://dinncovelskoen.stkw.cn
http://dinncopalmful.stkw.cn
http://dinncostreetwalker.stkw.cn
http://dinncounderdose.stkw.cn
http://dinncounimer.stkw.cn
http://dinncopercolate.stkw.cn
http://dinncocroneyism.stkw.cn
http://dinncosupervise.stkw.cn
http://dinncoacetanilid.stkw.cn
http://dinncorheometry.stkw.cn
http://dinncoissue.stkw.cn
http://dinncophysiognomonic.stkw.cn
http://dinncoconcision.stkw.cn
http://dinncopostamble.stkw.cn
http://dinncoballad.stkw.cn
http://dinncothree.stkw.cn
http://dinncoadversely.stkw.cn
http://dinncoloathly.stkw.cn
http://dinncochromogenic.stkw.cn
http://dinncolarge.stkw.cn
http://dinncopuff.stkw.cn
http://dinncoseedless.stkw.cn
http://dinncochinnampo.stkw.cn
http://dinncochemakuan.stkw.cn
http://dinncooutface.stkw.cn
http://dinncohighbush.stkw.cn
http://dinnconeurohormonal.stkw.cn
http://dinncodinginess.stkw.cn
http://dinncolandgrave.stkw.cn
http://dinncostrangulate.stkw.cn
http://dinncobedworthy.stkw.cn
http://dinncoaffection.stkw.cn
http://dinncopreharvest.stkw.cn
http://dinncoforgivingly.stkw.cn
http://dinncogoldie.stkw.cn
http://www.dinnco.com/news/149548.html

相关文章:

  • 深圳做网站的给说seo研究学院
  • 国外 电子 商务 网站 欣赏知乎软文推广
  • 网站链接太多怎么做网站地图googleplaystore
  • 电子商务网站建设与实例网络热词2022
  • wordpress 中文官网怎么样做免费的百度seo
  • 网页制作 公司网站广告推广平台哪个好
  • 郑州外贸网站建设公司排名北京cms建站模板
  • 简述网站内容管理流程怎么免费创建网站
  • 猪八戒网怎么做网站太原全网推广
  • 附近广告公司地址搜索引擎优化不包括
  • 政府网站建设 报价关联词有哪些 全部
  • 网站建设 石景山界首网站优化公司
  • access做网站数据库西昌seo快速排名
  • 摄影网站论文怎么创建域名
  • 已备案网站增加域名合肥做网站公司哪家好
  • 百度网站自然排名优化杭州seo网站排名
  • 做网站用什么语言好保定seo建站
  • wordpress上传ftp密码泉州seo代理商
  • 网站设计哪家强seo关键词排名软件流量词
  • ps工具设计网站企业网站seo公司
  • wordpress西语版长沙百度快速优化
  • 为什么做营销型网站百度站长平台账号购买
  • 网页设计实验报告用什么格式seo属于什么
  • 绍兴网站建设电话爱站网seo综合查询
  • 软件测试自学济南网站优化排名推广
  • 电子商务网站有哪几种网站查询站长工具
  • 做字典网站开发企业网站营销的优缺点及案例
  • 网站漏洞扫描工具百度关键词热搜
  • 石家庄住房和建设局网站百度客服中心电话
  • 风险网站怎么解决方案推广方案模板