Properties of a Red-Black tree map:
Operations:
trees :: RBTreeMap :: defaultinit
serialization :: Serializable :: accept_json_serializer
Refinable service to customize the serialization of this class to JSONserialization :: Serializable :: accept_msgpack_attribute_counter
Hook to customize the behavior of theAttributeCounter
serialization :: Serializable :: accept_msgpack_serializer
Hook to customize the serialization of this class to MessagePackserialization :: Serializable :: add_to_bundle
Called by[]=
to dynamically choose the appropriate method according
core :: Object :: class_factory
Implementation used byget_class
to create the specific class.
serialization :: Serializable :: core_serialize_to
Actual serialization ofself
to serializer
trees :: RBTreeMap :: defaultinit
core :: Object :: defaultinit
core :: Map :: defaultinit
trees :: BinTreeMap :: defaultinit
core :: MapRead :: defaultinit
trees :: TreeMap :: defaultinit
trees :: BinTreeMap :: dot_down
Translate the tree in dot format starting fromnode
.
core :: MapRead :: filter_keys
Return all elements ofkeys
that have a value.
serialization :: Serializable :: from_deserializer
Create an instance of this class from thedeserializer
core :: MapRead :: get_or_default
Get the item atkey
or return default
if not in map
core :: MapRead :: get_or_null
Get the item atkey
or null if key
is not in the map.
core :: Object :: is_same_instance
Return true ifself
and other
are the same instance (i.e. same identity).
core :: Object :: is_same_serialized
Isself
the same as other
in a serialization context?
core :: Object :: is_same_type
Return true ifself
and other
have the same dynamic type.
core :: MapRead :: keys_sorted_by_values
Return an array of all keys sorted with their values usingcomparator
.
core :: MapRead :: lookup_all_values
Search all the values inpe.greaters
.
core :: MapRead :: lookup_values
Combine the values inpe.greaters
from the most smaller elements that have a value.
serialization :: Serializable :: msgpack_extra_array_items
Hook to request a larger than usual metadata arraycore :: Object :: output_class_name
Display class name on stdout (debug only).trees :: BinTreeMap :: print_tree
Print the tree starting fromnode
.
core :: MapRead :: provide_default_value
Called by the underling implementation of[]
to provide a default value when a key
has no value
trees :: BinTreeMap :: rotate_right
Perform right rotation onnode
trees :: BinTreeMap :: search_down
Searchkey
in from
and its children nodes.
serialization :: Serializable :: serialize_msgpack
Serializeself
to MessagePack bytes
serialization :: Serializable :: serialize_to
Serializeself
to serializer
serialization :: Serializable :: serialize_to_json
Serializeself
to JSON
trees :: BinTreeMap :: shift_down
Push down thenode
in tree from a specified from
index
core :: MapRead :: to_map_comparator
A comparator that compares things with their values in self.serialization :: Serializable :: to_pretty_json
Serializeself
to plain pretty JSON
trees :: BinTreeMap :: transplant
Swap anode
with the other
in this Tree
core :: MapRead :: values_sorted_by_key
Return an array of all values sorted with their keys usingcomparator
.
Serializer::serialize
# Red-Black Tree Map
# Properties of a Red-Black tree map:
# * every node is either red or black
# * root is black
# * every leaf (null) is black
# * if a node is red, then both its children are black
# * for each node, all simple path from the node to descendant
# leaves contain the same number of black nodes
#
# Operations:
# * search average O(lg n) worst O(lg n)
# * insert average O(lg n) worst O(lg n)
# * delete average O(lg n) worst O(lg n)
class RBTreeMap[K: Comparable, E]
super BinTreeMap[K, E]
redef type N: RBTreeNode[K, E]
redef fun []=(key, item) do
insert_node(new RBTreeNode[K, E](key, item))
end
redef fun insert_node(node) do
super
insert_fixup_case1(node)
end
# Case 1: node is root
# color node as black
# it adds a black node on every path so we do nothing else
private fun insert_fixup_case1(node: N) do
if node.parent == null then
node.is_red = false
else
insert_fixup_case2(node)
end
end
# Case 2: parent is black
# it do not add black node so we do nothing else
private fun insert_fixup_case2(node: N) do
if node.parent.is_red then insert_fixup_case3(node)
end
# Case 3: node, parent and uncle are red
# this is a LLr or RRr conflict
# we recolor recursively the tree to the root
private fun insert_fixup_case3(node: N) do
if node.uncle != null and node.uncle.is_red then
node.parent.is_red = false
node.uncle.is_red = false
node.grandparent.is_red = true
insert_fixup_case1(node.grandparent.as(not null))
else
insert_fixup_case4(node)
end
end
# Case 4: node and parent are red, uncle is black
# this is a LRb or RLb conflict
# we rotate the tree to balance it
private fun insert_fixup_case4(node: N) do
if node == node.parent.right and node.parent == node.grandparent.left then
rotate_left(node.parent.as(not null))
node = node.left.as(not null)
else if node == node.parent.left and node.parent == node.grandparent.right then
rotate_right(node.parent.as(not null))
node = node.right.as(not null)
end
insert_fixup_case5(node)
end
# Case 5: node and parent are red, uncle is black
# this is a LLb or RRb conflict
# we rotate the tree to balance it
private fun insert_fixup_case5(node: N) do
node.parent.is_red = false
node.grandparent.is_red = true
if node == node.parent.left then
rotate_right(node.grandparent.as(not null))
else
rotate_left(node.grandparent.as(not null))
end
end
# TODO implement RBTree::delete
redef fun delete(key) is abstract
redef fun dot_down(node, f) do
if node.left != null then dot_down(node.left.as(not null), f)
f.write node.to_dot
if node.parent != null then f.write "\"{node.parent.as(not null)}\" -> \"{node}\"[dir=back];\n"
if node.right != null then dot_down(node.right.as(not null), f)
end
end
lib/trees/rbtree.nit:33,1--127,3