Class: Tree::BinaryTreeNode
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.
Core Attributes collapse
-
#left_child ⇒ Tree::BinaryTreeNode
Left child of the receiver node.
-
#left_child? ⇒ Boolean
(also: #is_left_child?)
true
if the receiver node is the left child of its parent. -
#right_child ⇒ Tree::BinaryTreeNode
Right child of the receiver node.
-
#right_child? ⇒ Boolean
(also: #is_right_child?)
readonly
true
if the receiver node is the right child of its parent.
Attributes inherited from TreeNode
#children?, #content, #content?, #leaf?, #name, #parent, #parentage, #root, #root?
Attributes included from Utils::TreeMetricsHandler
#breadth, #in_degree, #length, #level, #node_depth, #node_height, #out_degree, #size
Structure Modification collapse
-
#add(child) ⇒ Object
Adds the specified child node to the receiver node.
-
#add_from_hash(hashed_subtree) ⇒ Array
Instantiate and insert child nodes from data in a Ruby
Hash
. -
#inordered_each {|node| ... } ⇒ Tree::BinaryTreeNode, Enumerator
Performs in-order traversal (including this node).
-
#swap_children ⇒ Object
Swaps the left and right child nodes of the receiver node with each other.
Methods inherited from TreeNode
#<<, #<=>, #[], #breadth_each, #children, #detached_copy, #detached_subtree_copy, #each, #each_leaf, #each_level, #first_child, #first_sibling, #first_sibling?, #freeze_tree!, #initialize, #last_child, #last_sibling, #last_sibling?, #marshal_dump, #marshal_load, #next_sibling, #only_child?, #postordered_each, #preordered_each, #previous_sibling, #print_tree, #remove!, #remove_all!, #remove_from_parent!, #rename, #rename_child, #replace!, #replace_with, #siblings, #to_s
Methods included from Utils::HashConverter
Methods included from Utils::TreeMergeHandler
Methods included from Utils::JSONConverter
Methods included from Utils::TreePathHandler
#path_as_array, #path_as_string
Constructor Details
This class inherits a constructor from Tree::TreeNode
Instance Attribute Details
#left_child ⇒ Tree::BinaryTreeNode
Left child of the receiver node. Note that left Child == first Child.
60 61 62 |
# File 'lib/tree/binarytree.rb', line 60 def left_child children.first end |
#left_child? ⇒ Boolean Also known as: is_left_child?
true
if the receiver node is the left child of its parent. Always returns false
if it is a root node.
83 84 85 86 87 |
# File 'lib/tree/binarytree.rb', line 83 def left_child? return false if root? self == parent.left_child end |
#right_child ⇒ Tree::BinaryTreeNode
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.
74 75 76 |
# File 'lib/tree/binarytree.rb', line 74 def right_child children[1] end |
#right_child? ⇒ Boolean (readonly) Also known as: is_right_child?
true
if the receiver node is the right child of its parent. Always returns false
if it is a root node.
96 97 98 99 100 |
# File 'lib/tree/binarytree.rb', line 96 def right_child? return false if root? self == parent.right_child end |
Instance Method Details
#add(child) ⇒ Object
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.
119 120 121 122 123 |
# File 'lib/tree/binarytree.rb', line 119 def add(child) raise ArgumentError, 'Already has two child nodes' if @children.size == 2 super(child) end |
#add_from_hash(hashed_subtree) ⇒ Array
Instantiate and insert child nodes from data in a Ruby Hash
This method is used in conjunction with TreeNode.from_hash to provide a convenient way of building and inserting child nodes present in a Ruby hashes.
This method will instantiate a TreeNode instance for each top- level key of the input hash, to be inserted as children of the receiver instance.
Nested hashes are expected and further child nodes will be created and added accordingly. If a hash key is a single value that value will be used as the name for the node. If a hash key is an Array, both node name and content will be populated.
A leaf element of the tree should be represented as a hash key with corresponding value nil or {}.
>
154 155 156 157 158 159 |
# File 'lib/tree/binarytree.rb', line 154 def add_from_hash(hashed_subtree) raise ArgumentError, 'Too many children'\ if hashed_subtree.size + @children.size > 2 super(hashed_subtree) end |
#inordered_each {|node| ... } ⇒ Tree::BinaryTreeNode, Enumerator
Performs in-order traversal (including this node).
noinspection RubyUnusedLocalVariable
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/tree/binarytree.rb', line 174 def inordered_each return to_enum unless block_given? node_stack = [] current_node = self until node_stack.empty? && current_node.nil? if current_node node_stack.push(current_node) current_node = current_node.left_child else current_node = node_stack.pop yield current_node current_node = current_node.right_child end end self if block_given? end |
#swap_children ⇒ Object
Swaps the left and right child nodes of the receiver node with each other.
246 247 248 |
# File 'lib/tree/binarytree.rb', line 246 def swap_children self.left_child, self.right_child = right_child, left_child end |