用Julia语言插入RED-BLACK TREE



我是julia的新员工,我有以下问题:

如何在由字符串和其他结构组成的RED_BLACK树中插入节点?例如:为RED-BLACK类型的树编写插入和删除图1中所示类型的节点所需的函数。用以下名称演示它在插入中的工作原理:

  • 詹姆斯·詹姆斯
  • Livia Livia显示操作后的内存内容

提到的类型(图1中的"(:

"""
structure that represents Full names
"""
mutable struct Nomes
key::Int
nome::String
end
"""
Nomes( nome::String )
Register constructor for names with no accents
"""
function Nomes( nome::String )
key = sum( [ Int( c ) for c in nome ] )
Nomes( key, nome )
end

DataStructures具有红-黑树。如果要将Nomes直接存储在树中,则必须为数据结构定义isless(如果它们是唯一的,则可以将它们的键存储为整数(。

using DataStructures
Base.isless(n1::Nomes, n2::Nomes) = n1.key < n2.key
rbtree = RBTree{Nomes}()
for nom in ["Sue", "John"]
insert!(rbtree, Nomes(nom))
end
@show rbtree
rbtree = RBTree{Nomes}(DataStructures.RBTreeNode{Nomes}(false, Nomes(301, 
"Sue"), DataStructures.RBTreeNode{Nomes}(false, nothing, nothing, 
nothing, nothing), DataStructures.RBTreeNode{Nomes}(true, Nomes(399, 
"John"), DataStructures.RBTreeNode{Nomes}(false, nothing, nothing, 
nothing, nothing), DataStructures.RBTreeNode{Nomes}(false, nothing, 
nothing, nothing, nothing), DataStructures.RBTreeNode{Nomes}(#= circular 
reference @-2 =#)), nothing), DataStructures.RBTreeNode{Nomes}(false, 
nothing, nothing, nothing, nothing), 2)

相关内容

最新更新