module Sanction::Tree

Public Instance Methods

ancestors() click to toggle source

Get all of the ancestors for this node.

# File lib/sanction/tree.rb, line 53
def ancestors
  ancestors = []
  if parent
    ancestors << parent
    parent.ancestors.each {|ancestor| ancestors << ancestor }
  end
  ancestors
end
children() click to toggle source

Get the children of this node.

# File lib/sanction/tree.rb, line 29
def children
  @children ||= []
end
depth() click to toggle source

An integer representation of how deep in the tree this node is. The root node has a depth of 1, its children have a depth of 2, etc.

# File lib/sanction/tree.rb, line 76
def depth
  ancestors.size + 1
end
descendants() click to toggle source

Get all of the descendants of this node. All of its children, and its childrens’ children, and its childrens’ childrens’ children…

# File lib/sanction/tree.rb, line 64
def descendants
  descendants = []
  if !children.empty?
    (descendants << children).flatten!
    children.each {|descendant| descendants << descendant.descendants }
    descendants.flatten!   
  end
  descendants
end
graft(node) click to toggle source

Append a node to this node’s children, and return the node.

# File lib/sanction/tree.rb, line 108
def graft(node)
  node.instance_variable_set(:@parent, self)
  children << node
  node
end
has_children?() click to toggle source

Does this node have children? Is it not a leaf node?

# File lib/sanction/tree.rb, line 48
def has_children?
  !leaf?
end
height() click to toggle source

From Wikipedia: The height of a node is the length of the longest downward path to a leaf from that node. In other words, the length of this node to its furthest descendant.

# File lib/sanction/tree.rb, line 83
def height
  if !leaf?
    descendants.collect {|child| child.depth }.uniq.size + 1
  else
    1
  end
end
leaf?() click to toggle source

Is this node a leaf node? Is this node childless?

# File lib/sanction/tree.rb, line 15
def leaf?
  children.nil? || children.empty?
end
only_child?() click to toggle source

Is this node the only child of its parent. Does it have any siblings?

# File lib/sanction/tree.rb, line 41
def only_child?
  if parent
    siblings.empty?
  end
end
parent() click to toggle source

This node’s parent.

# File lib/sanction/tree.rb, line 5
def parent
  @parent
end
prune() click to toggle source

Abandon all of this node’s children.

# File lib/sanction/tree.rb, line 101
def prune
  if children
    children.each {|child| child.unlink }
  end
end
root() click to toggle source

Get the root node in this tree.

# File lib/sanction/tree.rb, line 20
def root
  if root?
    self
  else
    parent.root
  end
end
root?() click to toggle source

Is this node the root of the tree?

# File lib/sanction/tree.rb, line 10
def root?
  parent.nil?
end
siblings() click to toggle source

Get the siblings of this node. The other children belonging to this node’s parent.

# File lib/sanction/tree.rb, line 34
def siblings
  if parent
    parent.children.reject {|child| child == self }
  end
end
walk() { |self| ... } click to toggle source

Recursively yield every node in the tree.

# File lib/sanction/tree.rb, line 115
def walk(&block)
  if block_given?
    yield self
    children.each {|child| child.walk(&block) }
  end
end