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

做网站需要看的书苏州seo

做网站需要看的书,苏州seo,房屋设计师游戏下载,网站网页模板文章目录 AVL树AVL树的规则或原理 AVL树的实现1.节点的定义2.功能和接口等的实现默认构造函数#xff0c;析构函数拷贝构造函数插入搜索打印函数检查是否为平衡树#xff0c;检查平衡因子旋转 AVL树 AVL树#xff0c;全称Adelson-Velsky和Landis树#xff0c;是一种自平衡… 文章目录 AVL树AVL树的规则或原理 AVL树的实现1.节点的定义2.功能和接口等的实现默认构造函数析构函数拷贝构造函数插入搜索打印函数检查是否为平衡树检查平衡因子旋转 AVL树 AVL树全称Adelson-Velsky和Landis树是一种自平衡的二叉搜索树。它于1962年由苏联科学家Adelson-Velsky和Landis首次提出。AVL树具有以下特点树中任一节点的左右子树高度差不超过1因此AVL树是一种严格平衡的二叉搜索树。在AVL树上进行查找、插入和删除操作的时间复杂度均为O(log n)大大提高了搜索效率。 AVL树的规则或原理 当向二叉搜索树中插入新结点后如果能保证每个结点的左右子树高度之差的绝对值不超过1(需要对树中的结点进行调整)即可降低树的高度从而减少平均搜索长度。 一棵AVL树或者是空树或者是具有以下性质的二叉搜索树 它的左右子树都是AVL树左右子树高度之差(简称平衡因子)的绝对值不超过1(-1/0/1) 如果一棵二叉搜索树是高度平衡的它就是AVL树。如果它有n个结点其高度可保持在 O ( l o g 2 n ) O(log_2 n) O(log2​n)搜索时间复杂度O( l o g 2 n log_2 n log2​n)。 AVL树的实现 1.节点的定义 首先我们定义AVL树的节点结构 templateclass K, class V struct AVLTreeNode {pairK, V _kv;//值 AVLTreeNodeK, V* _left;//该节点的左孩子AVLTreeNodeK, V* _right;//该节点的右孩子AVLTreeNodeK, V* _parent;//该节点的父节点int _bf; // balance factor平衡因子AVLTreeNode(const pairK, V kv):_kv(kv), _left(nullptr), _right(nullptr), _parent(nullptr), _bf(0){} };2.功能和接口等的实现 默认构造函数析构函数 templateclass K, class V class AVLTree {typedef AVLTreeNodeK, V Node; public://默认构造函数用来初始化AVL树将根节点置空来表示树是空的AVLTree() default;//析构函数~AVLTree(){Destroy(_root);_root nullptr;} private:void Destroy(Node* root){if (root nullptr)return;Destroy(root-_left);Destroy(root-_right);delete root;} private:Node* _root nullptr; };拷贝构造函数 templateclass K, class V class AVLTree {//拷贝构造AVLTree(const AVLTreeK, V t){_root Copy(t._root);}private://用递归来进行赋值AVL树的节点Node* Copy(Node* root){if (root nullptr)return nullptr;Node* newRoot new Node(root-_key, root-_value);newRoot-_left Copy(root-_left);newRoot-_right Copy(root-_right);return newRoot;}private:Node* _root nullptr; };插入 templateclass K, class V class AVLTree { private:bool Insert(const pairK, V kv){if (_root nullptr){_root new Node(kv);return true;}Node* parent nullptr;Node* cur _root;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;// 更新平衡因子while (parent){if (cur parent-_left)parent-_bf--;elseparent-_bf;if (parent-_bf 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){RotateL(parent);}else if (parent-_bf -2 cur-_bf -1){RotateR(parent);}else if (parent-_bf 2 cur-_bf -1){RotateRL(parent);}else{RotateLR(parent);}break;}else{assert(false);}}return true;}private:Node* _root nullptr; };搜索 templateclass K, class V class AVLTree { private:Node* Find(const K key){Node* cur _root;while (cur){if (cur-_key key){cur cur-_right;}else if (cur-_key key){cur cur-_left;}else{return cur;}}return nullptr;}private:Node* _root nullptr; };打印函数 templateclass K, class V class AVLTree {void InOrder(){_InOrder(_root);cout endl;} private:void _InOrder(Node* root){if (root nullptr){return;}_InOrder(root-_left);cout root-_kv.first : root-_kv.second endl;_InOrder(root-_right);} private:Node* _root nullptr; };检查是否为平衡树检查平衡因子 templateclass K, class V class AVLTree { bool IsBanlanceTree(){return _IsBanlanceTree(_root);} private://计算平衡因子函数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 _IsBanlanceTree(Node* root){//空树返回真if (nullptr root){return true;}//计算root的平衡因子即root左右子树高度差int leftHeight _Height(root-_left);int rightHeight _Height(root-_right);int diff rightHeight - leftHeight;//如果计算出的平衡因子与root的平衡因子不相等//或root平衡因子的绝对值超过一则不是AVL树/*if (abs(diff) 2 || root-_bf ! diff){return false;}*/if (abs(diff) 2){cout root-_kv.first 高度差异常 endl;return false;}if (root-_bf ! diff){cout root-_kv.first 平衡因子异常 endl;return false;}//root的左右都是AVL树那么概述一定是AVL树return _IsBanlanceTree(root-_left) _IsBanlanceTree(root-_right);} private:Node* _root nullptr; };旋转 templateclass K, class V class AVLTree {void RotateL(Node* parent){Node* subR parent-_right;Node* subRL subR-_left;parent-_right subRL;if (subRL)subRL-_parent parent;Node* parentParent parent-_parent;csubR-_left parent;parent-_parent subR;if (parentParent nullptr){_root subR;subR-_parent nullptr;}else{if (parent parentParent-_left){parentParent-_left subR;}else{parentParent-_right subR;}subR-_parent parentParent;}parent-_bf subR-_bf 0;}void RotateR(Node* parent){Node* subL parent-_left;Node* subLR subL-_right;parent-_left subLR;if (subLR)subLR-_parent parent;Node* parentParent parent-_parent;subL-_right parent;parent-_parent subL;if (parentParent nullptr){_root subL;subL-_parent nullptr;}else{if (parent parentParent-_left){parentParent-_left subL;}else{parentParent-_right subL;}subL-_parent parentParent;}parent-_bf subL-_bf 0;}void RotateRL(Node* parent){Node* subR parent-_right;Node* subRL subR-_left;int bf subRL-_bf;RotateR(parent-_right);RotateL(parent);if (bf 0){subR-_bf 0;subRL-_bf 0;parent-_bf 0;}else if (bf 1){subR-_bf 0;subRL-_bf 0;parent-_bf -1;}else if (bf -1){subR-_bf 1;subRL-_bf 0;parent-_bf 0;}else{assert(false);}}void RotateLR(Node* parent){Node* subL parent-_left;Node* subLR subL-_right;int bf subLR-_bf;RotateL(parent-_left);RotateR(parent);if (bf 0){subL-_bf 0;subLR-_bf 0;parent-_bf 0;}else if (bf -1){subL-_bf 0;subLR-_bf 0;parent-_bf 1;}} private:Node* _root nullptr; };
http://www.dnsts.com.cn/news/22418.html

相关文章:

  • 东莞市主营网站建设平台模版建站
  • 网站前置审批文件网站建设成都云
  • 门户网站时代青岛网站seo优化
  • 做网站投注员挣钱吗自己做的旅游网站简介
  • 网站集约化建设推进情况网络空间安全专业大学排名
  • 网站怎么集成支付宝国外的外贸网站
  • 临沂网站公司哪家好wordpress企业h5主题
  • 网站上线发布流程专业网络推广软件
  • 网站上传根目录蜘蛛不抓取网站的原因
  • 外汇做单在什么网站品辰设计的网站谁做的
  • 网站维护一般要几天上国外网站用什么机箱好
  • 合肥专业做网站的公司有哪些wordpress 目录下拉框
  • qq空间实名认证网站wordpress 主题安全建设
  • 网站模板建站教程视频织梦手机网站建设
  • 广州 网站优化企业建站用什么主机
  • t恤图案设计网站网站建设源码开发
  • 宽屏网站和普通网站软件开发文档国标
  • 如何让网站被百度快速收录win8风格网站开发实例
  • 编程 网站建设长沙网站运营
  • 郑州做网站建设html设计网页作业代码
  • 网匠网站建设有限公司中国石化工程建设有限公司王龙哲
  • 个人网站设计论文摘要关键词湖南企业建网站
  • 建设摩托车官方网站机械设备东莞网站建设
  • .net网站开发项目东营住房和城乡建设厅网站
  • 成都公司建站模板兰溪市建设局网站 图片
  • 网站建设流程案例it公司排名
  • 制作网站培训怎么做电商网站推广
  • 微网站建设公司豆芽网站建设douyanet
  • 网站制作品牌公司公司网站建设开发济南兴田德润简介图片
  • 自助建站申请书建设环境竣工验收网站