module Rooted::Mutable

This module contains methods for mutating a tree node.

Public Instance Methods

<<(value = nil)

@see append_child.

Alias for: append_child
append_child(value = nil) click to toggle source

Insert a child after the last one.

@param value [Object] the value of the new sibling. @return [self]

# File lib/rooted/mutable.rb, line 36
def append_child(value = nil)
  push value
end
Also aliased as: <<
append_sibling(value = nil) click to toggle source

Insert a child between this node and the one after it.

@raise [StructureException] if this node has no parent.

@param value [Object] the value of the new sibling. @return [self]

# File lib/rooted/mutable.rb, line 12
def append_sibling(value = nil)
  raise StructureException, 'Root node can not have siblings' if root?

  append value
  self
end
delete() click to toggle source

Removes the node from the tree.

@return [Array<Node>] an array of the children to the deleted node, now

made roots.
# File lib/rooted/mutable.rb, line 65
def delete
  extract.children.to_a.each(&:extract)
end
extract() click to toggle source

Extracts the node and its subtree from the larger structure.

@return [self] the node will now be root.

# File lib/rooted/mutable.rb, line 54
def extract
  return self if root?

  method(:delete).super_method.call
  self
end
prepend_child(value = nil) click to toggle source

Insert a child before the first one.

@param value [Object] the value of the new sibling. @return [self]

# File lib/rooted/mutable.rb, line 47
def prepend_child(value = nil)
  unshift value
end
prepend_sibling(value = nil) click to toggle source

Insert a child between this node and the one before it.

@raise [StructureException] if this node has no parent.

@param value [Object] the value of the new sibling. @return [self]

# File lib/rooted/mutable.rb, line 25
def prepend_sibling(value = nil)
  raise StructureException, 'Root node can not have siblings' if root?

  prepend value
  self
end