本质还是一颗二叉搜索树,只是在其基础上增加了AddFix和RemoveFix来做平衡性修正,确保不会出现极端不平衡的情况。
【规则】
a) 根节点为黑
b) 红色节点的子节点只能是2个黑
c) 黑色节点的子节点只能是:1个红,2个红,2个黑或没有子节点,不可能出现1个黑(如下图所示)

d) 任一结点到各个叶子结点的路径都包含数量相同的黑色结点
e) 新添加的节点一开始总是为红色,后面根据需要调整颜色
# 根据上面的规则,下面的这些情况也不可能出现
a)

b)

c)

# 红黑树看似很复杂,其实只是过程比较繁琐而已。他对什么情况做什么操作都已经规定死了,只要照着这些规定的步骤走就行了。
【添加】
# 当遇到新添加节点后,出现连续的红色时,需要做修正

# 添加会遇到的所有情况:

uncle表示父节点的兄弟节点,gparent表示祖父节点,root表示根节点
# 情况1-1,parent和uncle设为黑色

# 情况1-2,parent,uncle设为黑色,gparent设为红色;然后将gparent作为当前节点继续往上处理,直到不再有连续的红色

# 情况2-1,右旋(绕蓝色描边节点),gparent设为红,parent设为黑
a) 不存在uncle

b) 存在uncle

# 情况2-2,左旋,gparent设为红,parent设为黑
a) 不存在uncle

b) 存在uncle

# 情况3-1,先左旋,变为情况2-1,然后再右旋
a) 不存在uncle

b) 存在uncle

# 情况3-2,先右旋,变为情况2-2,然后再左旋
a) 不存在uncle

b) 存在uncle

# 添加的代码同二叉搜索树,这边只展示添加后的修正
function BRTree:_AddFix(curNode, curNodeIsLeft, parent, parentLevel, stack)
while nil ~= parent and not parent.isBlack do
local gparent = stack[parentLevel-1]
local uncle = nil
local parentIsLeft = gparent.left == parent
if parentIsLeft then
uncle = gparent.right
else
uncle = gparent.left
end
if nil ~= uncle and not uncle.isBlack then --uncle存在且为红
parent.isBlack = true
uncle.isBlack = true
if gparent == self.root then --情况1-1: gparent为root
return
end
gparent.isBlack = false
local ggparentLevel = parentLevel - 2
local ggparent = stack[ggparentLevel]
if ggparent.isBlack then
return
end
--情况1-2: ggparent肯定存在且不为root
curNode = gparent
parentLevel = ggparentLevel
parent = ggparent
curNodeIsLeft = (parent.left == curNode)
elseif nil == uncle or uncle.isBlack then --uncle不存在或为黑
local ggparent = stack[parentLevel-2]
if parentIsLeft then
if curNodeIsLeft then --同侧1, 情况2-1
self:_RightRotate(gparent, ggparent)
parent.isBlack = true
gparent.isBlack = false
else --不同侧1, 情况3-1
self:_LeftRotate(parent, gparent)
local temp = parent
parent = curNode
curNode = temp
self:_RightRotate(gparent, ggparent)
parent.isBlack = true
gparent.isBlack = false
end
else
if curNodeIsLeft then --不同侧2, 情况3-2
self:_RightRotate(parent, gparent)
local temp = parent
parent = curNode
curNode = temp
self:_LeftRotate(gparent, ggparent)
parent.isBlack = true
gparent.isBlack = false
else --同侧2, 情况2-2
self:_LeftRotate(gparent, ggparent)
parent.isBlack = true
gparent.isBlack = false
end
end
return
end
end
end
# 红黑树的节点定义,比BSTree的节点多了个isBlack来记录颜色
local Node = {}
Node.__cname = "util.BRTree.Node"
Node.__index = Node
function Node.new(v)
local obj = {}
setmetatable(obj, Node)
obj:ctor(v)
return obj
end
function Node:ctor(v)
self.value = v
self.left = nil
self.right = nil
self.isBlack = false
end
# 左旋和右旋
---@param subTreeRoot "旋转节点"
---@param subTreeParent "旋转节点的父节点"
function BRTree:_LeftRotate(subTreeRoot, subTreeParent)
local newSubTreeRoot = subTreeRoot.right
subTreeRoot.right = newSubTreeRoot.left
newSubTreeRoot.left = subTreeRoot
if subTreeRoot == self.root then
self.root = newSubTreeRoot
else
if subTreeRoot == subTreeParent.left then
subTreeParent.left = newSubTreeRoot
else
subTreeParent.right = newSubTreeRoot
end
end
end
function BRTree:_RightRotate(subTreeRoot, subTreeParent)
local newSubTreeRoot = subTreeRoot.left
subTreeRoot.left = newSubTreeRoot.right
newSubTreeRoot.right = subTreeRoot
if subTreeRoot == self.root then
self.root = newSubTreeRoot
else
if subTreeRoot == subTreeParent.left then
subTreeParent.left = newSubTreeRoot
else
subTreeParent.right = newSubTreeRoot
end
end
end
【参考】
# 浅析红黑树(RBTree)原理及实现_芮小谭的博客-CSDN博客_红黑树
# 红黑树之删除节点 - 青儿哥哥 - 博客园 (cnblogs.com)
# 红黑树从头至尾插入和删除结点的全程演示图_v_JULY_v的博客-CSDN博客
当我使用Bundler时,是否需要在我的Gemfile中将其列为依赖项?毕竟,我的代码中有些地方需要它。例如,当我进行Bundler设置时:require"bundler/setup" 最佳答案 没有。您可以尝试,但首先您必须用鞋带将自己抬离地面。 关于ruby-我需要将Bundler本身添加到Gemfile中吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/4758609/
我有一个ModularSinatra应用程序,我正在尝试将Bootstrap添加到应用程序中。get'/bootstrap/application.css'doless:"bootstrap/bootstrap"end我在views/bootstrap中有所有less文件,包括bootstrap.less。我收到这个错误:Less::ParseErrorat/bootstrap/application.css'reset.less'wasn'tfound.Bootstrap.less的第一行是://CSSReset@import"reset.less";我尝试了所有不同的路径格式,但它
我正在使用Sequel构建一个愿望list系统。我有一个wishlists和itemstable和一个items_wishlists连接表(该名称是续集选择的名称)。items_wishlists表还有一个用于facebookid的额外列(因此我可以存储opengraph操作),这是一个NOTNULL列。我还有Wishlist和Item具有续集many_to_many关联的模型已建立。Wishlist类也有:selectmany_to_many关联的选项设置为select:[:items.*,:items_wishlists__facebook_action_id].有没有一种方法可以
当谈到运行时自省(introspection)和动态代码生成时,我认为ruby没有任何竞争对手,可能除了一些lisp方言。前几天,我正在做一些代码练习来探索ruby的动态功能,我开始想知道如何向现有对象添加方法。以下是我能想到的3种方法:obj=Object.new#addamethoddirectlydefobj.new_method...end#addamethodindirectlywiththesingletonclassclass这只是冰山一角,因为我还没有探索instance_eval、module_eval和define_method的各种组合。是否有在线/离线资
我注意到类定义,如果我打开classMyClass,并在不覆盖的情况下添加一些东西我仍然得到了之前定义的原始方法。添加的新语句扩充了现有语句。但是对于方法定义,我仍然想要与类定义相同的行为,但是当我打开defmy_method时似乎,def中的现有语句和end被覆盖了,我需要重写一遍。那么有什么方法可以使方法定义的行为与定义相同,类似于super,但不一定是子类? 最佳答案 我想您正在寻找alias_method:classAalias_method:old_func,:funcdeffuncold_func#similartoca
我有带有Logo图像的公司模型has_attached_file:logo我用他们的Logo创建了许多公司。现在,我需要添加新样式has_attached_file:logo,:styles=>{:small=>"30x15>",:medium=>"155x85>"}我是否应该重新上传所有旧数据以重新生成新样式?我不这么认为……或者有什么rake任务可以重新生成样式吗? 最佳答案 参见Thumbnail-Generation.如果rake任务不适合你,你应该能够在控制台中使用一个片段来调用重新处理!关于相关公司
我正在尝试使用Curbgem执行以下POST以解析云curl-XPOST\-H"X-Parse-Application-Id:PARSE_APP_ID"\-H"X-Parse-REST-API-Key:PARSE_API_KEY"\-H"Content-Type:image/jpeg"\--data-binary'@myPicture.jpg'\https://api.parse.com/1/files/pic.jpg用这个:curl=Curl::Easy.new("https://api.parse.com/1/files/lion.jpg")curl.multipart_form_
HashMap中为什么引入红黑树,而不是AVL树呢1.概述开始学习这个知识点之前我们需要知道,在JDK1.8以及之前,针对HashMap有什么不同。JDK1.7的时候,HashMap的底层实现是数组+链表JDK1.8的时候,HashMap的底层实现是数组+链表+红黑树我们要思考一个问题,为什么要从链表转为红黑树呢。首先先让我们了解下链表有什么不好???2.链表上述的截图其实就是链表的结构,我们来看下链表的增删改查的时间复杂度增:因为链表不是线性结构,所以每次添加的时候,只需要移动一个节点,所以可以理解为复杂度是N(1)删:算法时间复杂度跟增保持一致查:既然是非线性结构,所以查询某一个节点的时候
我正在开发一个创建网络博客的RubyonRails项目。我希望将一个名为featured的boolean数据库字段添加到Post模型中。该字段应该可以通过我添加的事件管理界面进行编辑。我使用了以下代码,但我什至没有在网站上显示另一列。$railsgeneratemigrationaddFeaturedfeatured:boolean$rakedb:migrate我是RubyonRails的新手,非常感谢任何帮助。我的index.html.erb文件中的相关代码(views):FeaturedPost架构.rb:ActiveRecord::Schema.define(:version=>
假设我有一个这样的单例类:classSettingsincludeSingletondeftimeout#lazy-loadtimeoutfromconfigfile,orwhateverendend现在,如果我想知道使用什么超时,我需要编写如下内容:Settings.instance.timeout但我宁愿将其缩短为Settings.timeout使这项工作有效的一个明显方法是将设置的实现修改为:classSettingsincludeSingletondefself.timeoutinstance.timeoutenddeftimeout#lazy-loadtimeoutfromc