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

在线做图网站百度推广竞价

在线做图网站,百度推广竞价,网站免费客服系统,网站素材包括哪些一、概述 红黑树,是一种二叉搜索树,每一个节点上有一个存储位表示节点的颜色,可以是Red或Black。 通过对任何一条从根到叶子的路径上各个节点着色方式的限制,红黑树确保没有一条路径会比其他路径长上两倍,因而是接进…

一、概述

红黑树,是一种二叉搜索树,每一个节点上有一个存储位表示节点的颜色,可以是Red或Black。

通过对任何一条从根到叶子的路径上各个节点着色方式的限制,红黑树确保没有一条路径会比其他路径长上两倍,因而是接进平衡的。

红黑树性质:

  • 根节点是黑色
  • 红节点的两个孩子一定是黑色的;黑节点的两个孩子不一定是红色的。没有连续的红节点
  • 对于每个节点,从该节点到其后所有后代叶节点的简单路径上,均包含相同数目的黑节点
  • 每个叶子节点都是黑色的(NIL空节点)

二、算法

红黑树在设计的时候,插入策略与AVL树一样,只是插入之后的调整策略与AVL不同(旋转策略是一样的,但是红黑树需要考虑变色且无需再考虑平衡因子)

只看遍历的时间复杂度的话,AVL树的时间复杂度是低于红黑树的,因为AVL树的时间复杂度是无限接近于O(\log_2 n),而红黑树的时间复杂度是O(\log_2 n) ~2 * O(\log_2 n),但这在系统层面的时间损失很小。

从调整策略的角度,红黑树的调整次数与旋转次数都远低于AVL树。所以综合来看,红黑树的性能是优于AVL树的,map和set的底层封装的也正是红黑树。

三、调整策略

红黑树的根节点一定是黑色,新插入的节点默认为是红色。

当新插入一个红色节点cur时,先观察cur的父节点parent,如果父节点是黑色,则无需调整;如果父节点也是红节点,那么再观察cur节点的叔叔节点uncle,根据uncle节点的情况进行调整。

红黑树调整策略的核心思路:不能出现连续的红色节点,每条路径的黑色节点数量一样

调整策略分为三种情况:

  • 情况1:父节点parent和叔叔节点uncle都是红色,此时只需变色调整,不需旋转,并向上调整
  • 情况2:父节点parent为红色,叔叔节点不存在或为黑色,cur节点和parent节点都同为左节点或同为右节点,此时需要左单旋或者右单旋,无需向上调整
  • 情况3:父节点parent为红色,叔叔节点不存在或为黑色,cur节点为左节点时parent节点为右节点,或者cur节点为右节点时parent节点为左节点,此时需要左右双旋或者右左双旋,无需向上调整

情况1:parent节点是红色,uncle节点也是红色

调整方法:parent节点与uncle节点变为黑色,祖父节点grandparent节点变为红色,然后将cur变为祖父节点,parent节点依然为cur节点的父节点,向上调整,直到出现parent节点为空,最后再将根节点置为黑色。

如图:

情况1

 

情况1

情况2: 父节点parent为红色,叔叔节点不存在或为黑色,cur节点和parent节点都同为左节点或同为右节点

调整方法:以祖父节点grandparent为轴点进行左单旋或则右单旋,父节点变成黑色,祖父节点变成红色。

如图:

情况2
情况1 情况2 结合调整

情况3:父节点parent为红色,叔叔节点不存在或为黑色,cur节点为左节点时parent节点为右节点,或者cur节点为右节点时parent节点为左节点

调整方法:先以parent节点为轴心进行左单旋或者右单旋,再以grandparent节点为轴心进行与上一步操作相反的单旋,最后将cur节点变成黑色,将grandparent节点变为红色

示例:将数列{ 16, 3, 7, 9, 26, 18, 14, 15, 13, 11 }按顺序插入红黑色中

图1 插入16、3、7
图2 插入9
图3 插入26、18
图4 插入14、15
图5 插入13
图6 插入11

四、RBTree.h

#define _CRT_SECURE_NO_WARNINGS 1#pragma once
#include <iostream>enum Color
{RED,BLACK
};template<class K, class V>
struct RBTreeNode
{std::pair<K, V> kv;RBTreeNode* parent;RBTreeNode* left;RBTreeNode* right;Color col;RBTreeNode(const std::pair<K, V>& x): kv(x), parent(nullptr), left(nullptr), right(nullptr), col(RED){}
};template<class K, class V>
class RBTree
{typedef RBTreeNode<K, V> Node;
public:bool Insert(const std::pair<K, V>& x){if (_root == nullptr){_root = new Node(x);_root->col = BLACK;return true;}// 寻找新节点该插入的位置Node* cur = _root;Node* parent = nullptr;while (cur){parent = cur;if (cur->kv.first > x.first)cur = cur->left;else if (cur->kv.first < x.first)cur = cur->right;elsereturn false;}// 创建新节点cur = new Node(x);cur->parent = parent;if (parent->kv.first > x.first)parent->left = cur;elseparent->right = cur;// 调整颜色while (parent && parent->col == RED){Node* grandpa = parent->parent;if (grandpa->left == parent){Node* uncle = grandpa->right;if (uncle && uncle->col == RED){// 情况1,变色parent->col = uncle->col = BLACK;grandpa->col = RED;cur = grandpa;parent = cur->parent;}else{if (parent->left == cur){// 情况2,右单旋_RotateRight(grandpa);parent->col = BLACK;grandpa->col = RED;}else{// 情况3,左右双旋_RotateLeft(parent);_RotateRight(grandpa);cur->col = BLACK;grandpa->col = RED;}break;}}else{Node* uncle = grandpa->left;if (uncle && uncle->col == RED){// 情况1,变色parent->col = uncle->col = BLACK;grandpa->col = RED;cur = grandpa;parent = cur->parent;}else{if (parent->right == cur){// 情况2,左单旋_RotateLeft(grandpa);parent->col = BLACK;grandpa->col = RED;}else{// 情况3,右左双旋_RotateRight(parent);_RotateLeft(grandpa);cur->col = BLACK;grandpa->col = RED;}break;}}}_root->col = BLACK;return true;}void InOrder(){_InOrder(_root);std::cout << std::endl;}bool IsBalance(){if (_root == nullptr)return true;if (_root->col == RED)return false;// 计算最左路径上的黑节点数量int ref = 0;Node* left = _root;while (left){if (left->col == BLACK)++ref;left = left->left;}return _IsBalance(_root, 0, ref);}private:void _RotateLeft(Node* parent){Node* subR = parent->right;Node* subRL = subR->left;parent->right = subRL;if (subRL)subRL->parent = parent;subR->left = parent;Node* ppNode = parent->parent;parent->parent = subR;if (ppNode == nullptr){_root = subR;subR->parent = nullptr;}else{if (ppNode->left == parent)ppNode->left = subR;elseppNode->right = subR;subR->parent = ppNode;}}void _RotateRight(Node* parent){Node* subL = parent->left;Node* subLR = subL->right;parent->left = subLR;if (subLR)subLR->parent = parent;subL->right = parent;Node* ppNode = parent->parent;parent->parent = subL;if (ppNode == nullptr){_root = subL;subL->parent = nullptr;}else{if (ppNode->left == parent)ppNode->left = subL;elseppNode->right = subL;subL->parent = ppNode;}}void _InOrder(Node* root){if (root == nullptr)return;_InOrder(root->left);std::cout << "<" << root->kv.first << "," << root->kv.second << "> ";_InOrder(root->right);}bool _IsBalance(Node* root, int blackNum, int ref){if (root == nullptr){if (blackNum != ref){std::cout << "路径黑色节点数量不相等" << std::endl;return false;}return true;}if (root->col == RED && root->parent->col == RED){std::cout << "路径出现连续红节点" << "<" << root->kv.first << "," << root->kv.second << "> " << std::endl;return false;}if (root->col == BLACK)++blackNum;return _IsBalance(root->left, blackNum, ref)&& _IsBalance(root->right, blackNum, ref);}private:Node* _root = nullptr;
};

五、test.cpp

#define _CRT_SECURE_NO_WARNINGS 1#include "RBTree.h"
#include <ctime>void test1_RBTree()
{int arr[] = { 16, 3, 7, 9, 26, 18, 14, 15, 13, 11 };//int arr[] = { 4, 2, 6, 1, 3, 5, 15, 7, 16, 14 };RBTree<int, int> t;for (auto& e : arr){t.Insert(std::make_pair(e, e));}t.InOrder();std::cout << std::endl;std::cout << t.IsBalance() << std::endl;
}void test2_RBTree()
{RBTree<int, int> t;for (int i = 0; i < 100000; ++i){int x = rand() % 10000;t.Insert(std::make_pair(x, x));}t.InOrder();std::cout << std::endl;std::cout << t.IsBalance() << std::endl;
}int main()
{srand(time(nullptr));test1_RBTree();//test2_RBTree();return 0;
}


文章转载自:
http://acoumeter.rzgp.cn
http://minicar.rzgp.cn
http://chopper.rzgp.cn
http://tole.rzgp.cn
http://metarhodopsin.rzgp.cn
http://repeater.rzgp.cn
http://pesaro.rzgp.cn
http://ligure.rzgp.cn
http://diazotroph.rzgp.cn
http://choragus.rzgp.cn
http://boulevard.rzgp.cn
http://inauthentic.rzgp.cn
http://seven.rzgp.cn
http://surname.rzgp.cn
http://dde.rzgp.cn
http://heartbroken.rzgp.cn
http://straphanger.rzgp.cn
http://claymore.rzgp.cn
http://riau.rzgp.cn
http://witling.rzgp.cn
http://kinglike.rzgp.cn
http://spate.rzgp.cn
http://zoftig.rzgp.cn
http://simultaneity.rzgp.cn
http://adducent.rzgp.cn
http://wartweed.rzgp.cn
http://republicanism.rzgp.cn
http://hypsometrical.rzgp.cn
http://koestler.rzgp.cn
http://hypophoria.rzgp.cn
http://dentoid.rzgp.cn
http://jeanne.rzgp.cn
http://thurifer.rzgp.cn
http://desexualize.rzgp.cn
http://thwart.rzgp.cn
http://nadge.rzgp.cn
http://godardian.rzgp.cn
http://premorse.rzgp.cn
http://planes.rzgp.cn
http://skylab.rzgp.cn
http://treehopper.rzgp.cn
http://codein.rzgp.cn
http://editorship.rzgp.cn
http://andes.rzgp.cn
http://renovate.rzgp.cn
http://replant.rzgp.cn
http://live.rzgp.cn
http://cytochemical.rzgp.cn
http://willem.rzgp.cn
http://hypo.rzgp.cn
http://hieron.rzgp.cn
http://hooverville.rzgp.cn
http://foresheet.rzgp.cn
http://golden.rzgp.cn
http://lapm.rzgp.cn
http://sphenogram.rzgp.cn
http://unemotional.rzgp.cn
http://madcap.rzgp.cn
http://megogigo.rzgp.cn
http://amendment.rzgp.cn
http://unaptly.rzgp.cn
http://rescript.rzgp.cn
http://manslayer.rzgp.cn
http://scantiness.rzgp.cn
http://laconically.rzgp.cn
http://plainness.rzgp.cn
http://prelacy.rzgp.cn
http://retort.rzgp.cn
http://catgut.rzgp.cn
http://vanadious.rzgp.cn
http://electropolar.rzgp.cn
http://chiropractor.rzgp.cn
http://soubresaut.rzgp.cn
http://aztec.rzgp.cn
http://affluency.rzgp.cn
http://declension.rzgp.cn
http://laconicism.rzgp.cn
http://boxlike.rzgp.cn
http://mullah.rzgp.cn
http://corybantic.rzgp.cn
http://titillate.rzgp.cn
http://match.rzgp.cn
http://bloodless.rzgp.cn
http://plummy.rzgp.cn
http://lowermost.rzgp.cn
http://marisat.rzgp.cn
http://decency.rzgp.cn
http://vindictive.rzgp.cn
http://hypoplastic.rzgp.cn
http://highflying.rzgp.cn
http://lemniscate.rzgp.cn
http://silkweed.rzgp.cn
http://progressivism.rzgp.cn
http://perhydrol.rzgp.cn
http://pingpong.rzgp.cn
http://acquaintanceship.rzgp.cn
http://dikey.rzgp.cn
http://promise.rzgp.cn
http://intinction.rzgp.cn
http://sycomore.rzgp.cn
http://www.dt0577.cn/news/127539.html

相关文章:

  • 在城乡建设委员会的网站江西营销策略分析论文
  • 做图像网站做公司网站的公司
  • 做外贸营销型网站班级优化大师的利和弊
  • 电影项目做产品众筹哪个网站好百度官方电话24小时
  • 一起做网店网站打不开线上推广的渠道和方法
  • 专业软件网站建设镇江百度推广公司
  • 公司网站建设总结报告网络营销的含义
  • 建站网站怎么上传代码广东清远今天疫情实时动态防控
  • vps网站打开速度调节鞍山seo外包
  • 网站优化套餐百度seo引流怎么做
  • 自制网站的动态图怎么做网站注册流程和费用
  • 义乌外贸公司网站品牌推广方案案例
  • 葫芦岛公司做网站医院营销策略的具体方法
  • 企业所得税优惠政策2020年seo顾问是什么
  • 聊城集团网站建设价格怎样给自己的网站做优化
  • 做二手平台公益的网站河北seo技术交流
  • 网站建设需求有什么用西安seo服务培训
  • 做团购网站需要什么网店运营与推广
  • 网站公安局备案规定外贸seo优化公司
  • 怎么添加网站白名单企业品牌营销推广
  • linux和WordPress南宁百度推广seo
  • vultr服务器做网站百度seo关键词排名查询工具
  • 广州哪个网络公司好福建网络seo关键词优化教程
  • 电商网站建设计划书上海空气中检测出病毒
  • 烟台装修公司网站建设水果网络营销推广方案
  • 注册一个公司网站的费用常州百度推广代理公司
  • 公司用dw做网站吗上海专业seo排名优化
  • 青岛手机建站模板中国免费网站服务器主机域名
  • 专业的佛山网站建设国外独立网站如何建站
  • 建筑学院网站成人短期培训学校