Class: Tree::BinaryTreeNode

Inherits:
TreeNode show all
Defined in:
lib/tree/binarytree.rb

Overview

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 TreeNode class.

Author:

Instance Attribute Summary

Attributes inherited from TreeNode

#content, #name, #parent

Instance Method Summary (collapse)

Methods inherited from TreeNode

#<<, #<=>, #[], #breadth, #breadth_each, #children, #depth, #detached_copy, #detached_subtree_copy, #each, #each_leaf, #first_child, #first_sibling, #freeze_tree!, #has_children?, #has_content?, #in_degree, #initialize, #is_first_sibling?, #is_last_sibling?, #is_leaf?, #is_only_child?, #is_root?, json_create, #last_child, #last_sibling, #length, #marshal_dump, #marshal_load, #method_missing, #next_sibling, #node_depth, #node_height, #out_degree, #parentage, #preordered_each, #previous_sibling, #print_tree, #remove!, #remove_all!, #remove_from_parent!, #root, #siblings, #size, #to_json, #to_s

Constructor Details

This class inherits a constructor from Tree::TreeNode

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Tree::TreeNode

Instance Method Details

- (Object) add(child)

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.

Parameters:

Raises:

  • (ArgumentError)

    This exception is raised if two children are already present.



66
67
68
69
70
# File 'lib/tree/binarytree.rb', line 66

def add(child)
  raise ArgumentError, "Already has two child nodes" if @children.size == 2

  super(child)
end

- (Boolean) is_left_child?

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

Returns:

  • (Boolean)

    true if this is the left child of its parent.



138
139
140
141
# File 'lib/tree/binarytree.rb', line 138

def is_left_child?
  return false if is_root?
  self == parent.left_child
end

- (Boolean) is_right_child?

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

Returns:

  • (Boolean)

    true if this is the right child of its parent.



147
148
149
150
# File 'lib/tree/binarytree.rb', line 147

def is_right_child?
  return false if is_root?
  self == parent.right_child
end

- (Tree::BinaryTreeNode) left_child

Returns the left child of the receiver node. Note that left Child == first Child.

Returns:

See Also:



77
78
79
# File 'lib/tree/binarytree.rb', line 77

def left_child
  children.first
end

- (Tree::BinaryTreeNode) left_child=(child)

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

Parameters:

Returns:

See Also:



118
119
120
# File 'lib/tree/binarytree.rb', line 118

def left_child=(child)
  set_child_at child, 0
end

- (Tree::BinaryTreeNode) right_child

Returns the 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.

Returns:

See Also:



88
89
90
# File 'lib/tree/binarytree.rb', line 88

def right_child
  children[1]
end

- (Tree::BinaryTreeNode) right_child=(child)

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

Parameters:

Returns:

See Also:



130
131
132
# File 'lib/tree/binarytree.rb', line 130

def right_child=(child)
  set_child_at child, 1
end

- (Object) swap_children

TODO:

Define the return value.

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



155
156
157
# File 'lib/tree/binarytree.rb', line 155

def swap_children
  self.left_child, self.right_child = self.right_child, self.left_child
end