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

房产网站建设推广semester是什么意思

房产网站建设推广,semester是什么意思,政府网站建设要点,网站开发的具体流程图一.红黑树 1.1 红黑树的起源 当对对AVL树做一些结构修改的操作时候,性能较为低下,比如:插入时要维护其绝对平衡,旋转的次数比较多,更差的是在删除时,有可能一直要让旋转持续到根的位置。 因此1972年Rudolf…

一.红黑树

1.1 红黑树的起源

当对对AVL树做一些结构修改的操作时候,性能较为低下,比如:插入时要维护其绝对平衡,旋转的次数比较多,更差的是在删除时,有可能一直要让旋转持续到根的位置。
因此1972年Rudolf Bayer提出的对称二叉B树(Symmetric Binary B-Trees),随后在1978年Leo J. Guibas和Robert Sedgewick的工作中进一步发展和完善,最终形成了现代意义上的红黑树,它通过简单的规则和较少的旋转操作实现了有效的自平衡,广泛应用于各类需要高效查找、插入和删除操作的场合,例如在Java集合框架中的TreeMap和TreeSet类,以及哈希表中解决冲突时采用的链表+红黑树混合结构。

1.2 红黑树的概念

红黑树,是一种二叉搜索树,但在每个结点上增加一个存储位表示结点的颜色,可以是Red或Black。 通过对任何一条从根到叶子的路径上各个结点着色方式的限制,红黑树确保没有一条路径会比其他路径长出两倍,因而是接近平衡的。

红黑树具有以下性质:

  1. 每个结点不是红色就是黑色
  2. 根节点是黑色的
  3. 如果一个节点是红色的,则它的两个孩子结点是黑色的
  4. 对于每个结点,从该结点到其所有后代叶结点的简单路径上,均包含相同数目的黑色结点
  5. 每个叶子结点都是黑色的(此处的叶子结点指的是空结点)

1.3 红黑树节点的定义

enum Color
{RED,BLACK
};template<typename T>
struct RBTreeNode
{RBTreeNode(const T& data): _left(nullptr), _right(nullptr), _parent(nullptr), _data(data), _col(RED){}RBTreeNode<T>* _left;RBTreeNode<T>* _right;RBTreeNode<T>* _parent; //父节点T _data;Color _col; //颜色
};

1.4 红黑树的插入

红黑树是在二叉搜索树的基础上加上其平衡限制条件,因此红黑树的插入可分为两步:

  1. 按照二叉搜索的树规则插入新节点。
  2. 检测新节点插入后,红黑树的性质是否造到破坏。
bool insert(const T& data)
{if (_root == nullptr){_root = new Node(data);return true;}Node* grandparent = nullptr;Node* uncle = nullptr;Node* parent = nullptr;Node* cur = _root;while (cur){parent = cur;if (data < cur->_data){cur = cur->_left;}else if (data > cur->_data){cur = cur->_right;}else{return false;}}cur = new Node(data);if (data < parent->_data){parent->_left = cur;}else if (data > parent->_data){parent->_right = cur;}else{assert(false);}cur->_parent = parent;while (parent && parent->_col == RED){grandparent = parent->_parent;if (grandparent){if (parent == grandparent->_left){uncle = grandparent->_right;}else{uncle = grandparent->_left;}}else{break;}if (uncle && uncle->_col == RED){parent->_col = BLACK;uncle->_col = BLACK;grandparent->_col = RED;cur = grandparent;}else{cur = Rotate(grandparent, parent, cur);}parent = cur->_parent;_root->_col = BLACK;}return true;
}

大致思路如下:
以下简称插入节点为cur,cur的父节点为parent,parent的父节点为grandparent,parent的兄弟节点为uncle。

  1. 先先按照二叉搜索树的规则将节点插入到红黑树中,节点颜色为红色。
  2. 插入后,红黑树的性质可能遭到破坏,此时就要根据红黑树的性质进行检测。
  3. cur插入后,如果parent颜色为黑色,则没有破坏红黑树的性质,插入结束。
  4. 若parent为红色,则此时有两种情况(uncle为红,uncle为黑/uncle不存在)
    1. uncle为红时,将parent和uncle变为黑色,grandparent变为红色,然后把grandparent视为cur,继续向上调整。
    2. uncle为黑/uncle不存在时,进行旋转。 (旋转在1.5处详细解释)

1.5 红黑树的旋转

此处的旋转与AVL树的旋转思路较为相似。

当uncle为黑/uncle不存在时,parent为cur的(左/右)孩子且parent为grandparent的(左/右)孩子,进行(右单旋)/(左单旋)。
右单旋

void RotateR(Node* parent)
{Node* ppnode = parent->_parent;Node* cur = parent->_left;Node* cur_right = cur->_right;if (ppnode){if (ppnode->_left == parent){ppnode->_left = cur;}else if (ppnode->_right == parent){ppnode->_right = cur;}else{assert(false);}}else{_root = cur;}cur->_parent = ppnode;parent->_left = cur_right;if (cur_right){cur_right->_parent = parent;}cur->_right = parent;parent->_parent = cur;
}

左单旋

void RotateL(Node* parent)
{Node* ppnode = parent->_parent;Node* cur = parent->_right;Node* cur_left = cur->_left;if (ppnode){if (ppnode->_right == parent){ppnode->_right = cur;}else if (ppnode->_left == parent){ppnode->_left = cur;}else{assert(false);}}else{_root = cur;}cur->_parent = ppnode;parent->_right = cur_left;if (cur_left){cur_left->_parent = parent;}cur->_left = parent;parent->_parent = cur;
}

当uncle为黑/uncle不存在时,parent为cur的(右/左)孩子且parent为grandparent的(左/右)孩子,进行(右单旋)/(左单旋)。

Node* Rotate(Node* grandparent, Node* parent, Node* cur)
{if (parent == grandparent->_left){if (cur == parent->_left){RotateR(grandparent);parent->_col = RED;grandparent->_col = BLACK;cur->_col = BLACK;return parent;}else{RotateL(parent);RotateR(grandparent);parent->_col = BLACK;grandparent->_col = BLACK;cur->_col = RED;return cur;}}else{if (cur == parent->_left){RotateR(parent);RotateL(grandparent);parent->_col = BLACK;grandparent->_col = BLACK;cur->_col = RED;return cur;}else{RotateL(grandparent);parent->_col = RED;grandparent->_col = BLACK;cur->_col = BLACK;return parent;}}

1.6 红黑树的特点与应用

  1. 红黑树是一棵不追求绝对平衡的二叉搜索树,其只需保证最长路径不超过最短路径的2倍,降低了插入和旋转的次数,所以在经常进行增删的结构中性能比AVL树更优。
  2. 红黑树在C++ STL库中 map/set 等结构中充当底层结构,据说在java中哈希表中的哈希桶下的链表长度超过一定的阈值时,也会转换为红黑树提高效率。使得在处理大量冲突键时,极大的缓解了链表过长导致的哈希表查找效率退化。

1.7 完整代码

#pragma once
#include <iostream>
#include <assert.h>
using namespace std;enum Color
{RED,BLACK
};template<typename T>
struct RBTreeNode
{RBTreeNode(const T& data): _left(nullptr), _right(nullptr), _parent(nullptr), _data(data), _col(RED){}RBTreeNode<T>* _left;RBTreeNode<T>* _right;RBTreeNode<T>* _parent;T _data;Color _col;
};template<typename T>
class RBTree
{
public:typedef RBTreeNode<T> Node;RBTree():_root(nullptr){}bool insert(const T& data){if (_root == nullptr){_root = new Node(data);return true;}Node* grandparent = nullptr;Node* uncle = nullptr;Node* parent = nullptr;Node* cur = _root;while (cur){parent = cur;if (data < cur->_data){cur = cur->_left;}else if (data > cur->_data){cur = cur->_right;}else{return false;}}cur = new Node(data);if (data < parent->_data){parent->_left = cur;}else if (data > parent->_data){parent->_right = cur;}else{assert(false);}cur->_parent = parent;while (parent && parent->_col == RED){grandparent = parent->_parent;if (grandparent){if (parent == grandparent->_left){uncle = grandparent->_right;}else{uncle = grandparent->_left;}}else{break;}if (uncle && uncle->_col == RED){parent->_col = BLACK;uncle->_col = BLACK;grandparent->_col = RED;cur = grandparent;}else{cur = Rotate(grandparent, parent, cur);}parent = cur->_parent;_root->_col = BLACK;}return true;}Node* get_Root(){return _root;}bool checkColour(Node* root, int blacknum, int benchmark){if (root == nullptr){if (blacknum != benchmark)return false;return true;}if (root->_col == BLACK){++blacknum;}if (root->_col == RED && root->_parent && root->_parent->_col == RED){cout << root->_data << "出现连续红色节点" << endl;return false;}return checkColour(root->_left, blacknum, benchmark)&& checkColour(root->_right, blacknum, benchmark);}bool isBalance(){return isBalance(_root);}int height(){return height(_root);}
private:Node* _root;void RotateR(Node* parent){Node* ppnode = parent->_parent;Node* cur = parent->_left;Node* cur_right = cur->_right;if (ppnode){if (ppnode->_left == parent){ppnode->_left = cur;}else if (ppnode->_right == parent){ppnode->_right = cur;}else{assert(false);}}else{_root = cur;}cur->_parent = ppnode;parent->_left = cur_right;if (cur_right){cur_right->_parent = parent;}cur->_right = parent;parent->_parent = cur;}void RotateL(Node* parent){Node* ppnode = parent->_parent;Node* cur = parent->_right;Node* cur_left = cur->_left;if (ppnode){if (ppnode->_right == parent){ppnode->_right = cur;}else if (ppnode->_left == parent){ppnode->_left = cur;}else{assert(false);}}else{_root = cur;}cur->_parent = ppnode;parent->_right = cur_left;if (cur_left){cur_left->_parent = parent;}cur->_left = parent;parent->_parent = cur;}Node* Rotate(Node* grandparent, Node* parent, Node* cur){if (parent == grandparent->_left){if (cur == parent->_left){RotateR(grandparent);parent->_col = RED;grandparent->_col = BLACK;cur->_col = BLACK;return parent;}else{RotateL(parent);RotateR(grandparent);parent->_col = BLACK;grandparent->_col = BLACK;cur->_col = RED;return cur;}}else{if (cur == parent->_left){RotateR(parent);RotateL(grandparent);parent->_col = BLACK;grandparent->_col = BLACK;cur->_col = RED;return cur;}else{RotateL(grandparent);parent->_col = RED;grandparent->_col = BLACK;cur->_col = BLACK;return parent;}}}bool isBalance(Node* root){if (root == nullptr)return true;if (root->_col != BLACK){return false;}// 基准值int benchmark = 0;Node* cur = _root;while (cur){if (cur->_col == BLACK)++benchmark;cur = cur->_left;}return checkColour(root, 0, benchmark);}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;}
};

————————————————————
感谢大家观看,不妨点赞支持一下吧[doge]
如有错误,随时纠正,谢谢大家

http://www.hkea.cn/news/157873/

相关文章:

  • 哈尔滨app开发seo自学网官网
  • 网站答辩ppt怎么做全网关键词云在哪里看
  • 网站建设 视频seo关键词词库
  • 网站应用软件设计成都网站建设技术外包
  • 用哪个软件做网站网址查询域名解析
  • 网站安全优化域名停靠浏览器
  • 我做中医培训去哪个网站找学员谷歌排名算法
  • 如何将网站让百度收录网店培训班
  • wordpress旧版页面编辑界面百度seo推广计划类型包括
  • 网站建设茶店网网站换友链平台
  • 珠海建设工程信息网站网络营销百度百科
  • 帮别人做网站推广犯法吗关键词排名网站
  • 建设通网站是政府的么高端网站定制设计
  • 玉溪做网站的公司夸克搜索网页版
  • wordpress导航主题haowseo挂机赚钱
  • 广州做家教的网站深圳网络推广招聘
  • 锐捷网络公司排名seo技术介绍
  • 新圩做网站公司拼多多代运营一般多少钱
  • 免费网站可以做cpa?短视频营销的优势
  • b2b外贸营销型网站如何做电商赚钱
  • 建设无障碍网站seo分析报告怎么写
  • 电子商务网站开发进什么科目模板自助建站
  • 威海市住房和城乡建设局官方网站北京seo营销公司
  • 开网页卡优化关键词排名工具
  • wordpress右侧文章归档东莞公司seo优化
  • 个人网站建设需求说明书免费外链生成器
  • 湖南网站建设的公司排名网页制作网站制作
  • 公司网页网站建设 ppt模板app开发公司排行榜
  • 网站开发yuanmus联合早报 即时消息
  • 为什么只有中国人怕疫情seo 页面