Class/Module Index [+]

Quicksearch

Tree::BinaryTreeNode

Provides a Binary tree implementation. This node allows only two child nodes (left and right child). It also provides direct access to the left or right child, including assignment to the same.

This inherits from the {Tree::TreeNode} class.

@author Anupam Sengupta

Public Instance Methods

add(child) click to toggle source

Adds the specified child node to the receiver node. The child node's parent is set to be the receiver.

The child nodes are added in the order of addition, i.e., the first child added becomes the left node, and the second child added will be the second node.

If only one child is present, then this will be the left child.

@param [Tree::BinaryTreeNode] child The child to add.

@raise [ArgumentError] This exception is raised if two children are already present.

# File lib/tree/binarytree.rb, line 64
def add(child)
  raise ArgumentError, "Already has two child nodes" if @children.size == 2

  super(child)
end
is_left_child?() click to toggle source

Returns true if the receiver node is the left child of its parent. Always returns false if it is a root node.

@return [Boolean] true if this is the left child of its parent.

# File lib/tree/binarytree.rb, line 138
def is_left_child?
  return false if is_root?
  self == parent.left_child
end
is_right_child?() click to toggle source

Returns true if the receiver node is the right child of its parent. Always returns false if it is a root node.

@return [Boolean] true if this is the right child of its parent.

# File lib/tree/binarytree.rb, line 147
def is_right_child?
  return false if is_root?
  self == parent.right_child
end
left_child() click to toggle source

@!attribute [rw] left_child Left child of the receiver node. Note that left Child == first Child.

@return [Tree::BinaryTreeNode] The left most (or first) child.

@see right_child

# File lib/tree/binarytree.rb, line 76
def left_child
  children.first
end
left_child=(child) click to toggle source

Sets the left child of the receiver node. If a previous child existed, it is replaced.

@param [Tree::BinaryTreeNode] child The child to add as the left-side node.

@return [Tree::BinaryTreeNode] The assigned child node.

@see left_child @see right_child=

# File lib/tree/binarytree.rb, line 118
def left_child=(child)
  set_child_at child, 0
end
right_child() click to toggle source

@!attribute [rw] right_child Right child of the receiver node. Note that right child == last child unless there is only one child.

Returns nil if the right child does not exist.

@return [Tree::BinaryTreeNode] The right child, or nil if the right side child does not exist.

@see left_child

# File lib/tree/binarytree.rb, line 88
def right_child
  children[1]
end
right_child=(child) click to toggle source

Sets the right child of the receiver node. If a previous child existed, it is replaced.

@param [Tree::BinaryTreeNode] child The child to add as the right-side node.

@return [Tree::BinaryTreeNode] The assigned child node.

@see right_child @see left_child=

# File lib/tree/binarytree.rb, line 130
def right_child=(child)
  set_child_at child, 1
end
swap_children() click to toggle source

Swaps the left and right child nodes of the receiver node with each other.

# File lib/tree/binarytree.rb, line 153
def swap_children
  self.left_child, self.right_child = self.right_child, self.left_child
end

Protected Instance Methods

set_child_at(child, at_index) click to toggle source

A protected method to allow insertion of child nodes at the specified location. Note that this method allows insertion of nil nodes.

@param [Tree::BinaryTreeNode] child The child to add at the specified location. @param [Integer] at_index The location to add the entry at (0 or 1).

@return [Tree::BinaryTreeNode] The added child.

@raise [ArgumentError] If the index is out of limits.

# File lib/tree/binarytree.rb, line 101
def set_child_at(child, at_index)
  raise ArgumentError "A binary tree cannot have more than two children." unless (0..1).include? at_index

  @children[at_index]        = child
  @children_hash[child.name] = child if child # Assign the name mapping
  child.parent               = self if child
  child
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.