The following program
mutable struct Node{T}
value::T
left::Union{Nothing, Node{T}}
right::Union{Nothing, Node{T}}
end
const Tree{T} = Union{Nothing, Node{T}}
function rotateright1(tree::Node{T}) where T
save1 = tree.left.right
tree.left.right = tree
save2 = tree.left
tree.left = save1
tree = save2
tree
end
function rotateright2(tree::Node{T}) where T
tree, tree.left, tree.left.right =
tree.left, tree.left.right, tree
tree
end
tree = Node("Q", Node("P", Node("A", nothing, nothing), Node("B", nothing, nothing)),
Node("C", nothing, nothing))
println(rotateright1(tree))
println(rotateright2(tree))
prints
Node{String}("P", Node{String}("A", nothing, nothing), Node{String}("Q", Node{String}("B", nothing, nothing), Node{String}("C", nothing, nothing)))
ERROR: LoadError: type Nothing has no field right
I’d expect rotateright1
and rotateright2
to behave identical but must be missing something?