Red black tree sort/Julia: Difference between revisions

m
link
(julia example)
 
m (link)
 
(3 intermediate revisions by 2 users not shown)
Line 1:
See [[Red_black_tree_sort]]
=={{header|Julia}}==
 
Code originally by Koustav Chowdhury.
<langsyntaxhighlight rubylang="julia">""" Modified from JuliaCollections/DataStructures.jl's file red_black_tree.jl """
 
# leftChild has keys which are less than the node
Line 51 ⟶ 52:
Returns the last visited node, while traversing through in binary-search-tree fashion looking for `key`.
"""
search_node(tree, key)
 
function search_node(tree::RBTree{K}, d::K) where K
node = tree.root
Line 406 ⟶ 405:
right = node.rightChild === tree.nil ? "empty" : string(node.rightChild.data)
parent = node.parent isa Nothing ? "empty" : string(node.parent.data)
println(io, "(left: $left, color: $color, current: $(node.data), right: $right, parent: $parent))")
end
end
Line 427 ⟶ 426:
 
RBTsort()
</langsyntaxhighlight>{{out}}
<pre>
After adding 30 nodes, Red-Black tree node list is:
(left: empty, color: red, current: 12, right: empty, parent: 18))
(left: 12, color: black, current: 18, right: 19, parent: 58))
(left: empty, color: red, current: 19, right: empty, parent: 18))
(left: 18, color: red, current: 58, right: 67, parent: 94))
(left: empty, color: black, current: 67, right: 78, parent: 58))
(left: empty, color: red, current: 78, right: empty, parent: 67))
(left: 58, color: black, current: 94, right: 155, parent: 207))
(left: empty, color: black, current: 112, right: 153, parent: 155))
(left: empty, color: red, current: 153, right: empty, parent: 112))
(left: 112, color: red, current: 155, right: 187, parent: 94))
(left: empty, color: red, current: 181, right: empty, parent: 187))
(left: 181, color: black, current: 187, right: empty, parent: 155))
(left: 94, color: black, current: 207, right: 488, parent: empty))
(left: empty, color: black, current: 215, right: empty, parent: 253))
(left: 215, color: black, current: 253, right: 401, parent: 488))
(left: empty, color: red, current: 305, right: empty, parent: 319))
(left: 305, color: black, current: 319, right: 390, parent: 401))
(left: empty, color: red, current: 390, right: empty, parent: 319))
(left: 319, color: red, current: 401, right: 408, parent: 253))
(left: empty, color: black, current: 408, right: 445, parent: 401))
(left: empty, color: red, current: 445, right: empty, parent: 408))
(left: 253, color: red, current: 488, right: 862, parent: 207))
(left: empty, color: red, current: 606, right: empty, parent: 653))
(left: 606, color: black, current: 653, right: 731, parent: 754))
(left: empty, color: red, current: 731, right: empty, parent: 653))
(left: 653, color: red, current: 754, right: 842, parent: 862))
(left: empty, color: black, current: 842, right: empty, parent: 754))
(left: 754, color: black, current: 862, right: 1000, parent: 488))
(left: empty, color: red, current: 948, right: empty, parent: 1000))
(left: 948, color: black, current: 1000, right: empty, parent: 862))
 
After deleting 15 of the nodes, Red-Black tree node list is:
(left: empty, color: red, current: 12, right: empty, parent: 18))
(left: 12, color: black, current: 18, right: 19, parent: 58))
(left: empty, color: red, current: 19, right: empty, parent: 18))
(left: 18, color: red, current: 58, right: 78, parent: 153))
(left: empty, color: black, current: 78, right: empty, parent: 58))
(left: 58, color: black, current: 153, right: 181, parent: 305))
(left: empty, color: black, current: 181, right: empty, parent: 153))
(left: 153, color: black, current: 305, right: 606, parent: empty))
(left: empty, color: black, current: 319, right: empty, parent: 401))
(left: 319, color: black, current: 401, right: 445, parent: 606))
(left: empty, color: black, current: 445, right: empty, parent: 401))
(left: 401, color: red, current: 606, right: 754, parent: 305))
(left: empty, color: black, current: 731, right: empty, parent: 754))
(left: 731, color: black, current: 754, right: 948, parent: 606))
(left: empty, color: black, current: 948, right: empty, parent: 754))
</pre>
4,103

edits