class BinaryTreeNode
Attributes
parent[RW]
Public Class Methods
new(value)
click to toggle source
Calls superclass method
GraphNode::new
# File lib/adt_utilit/binary_tree_node.rb, line 11 def initialize(value) super(value) end
Public Instance Methods
_trav_right_first(search_value = nil, &prc)
click to toggle source
# File lib/adt_utilit/binary_tree_node.rb, line 109 def _trav_right_first(search_value = nil, &prc) if (search_value.nil? && !block_given?) || (search_value && block_given?) raise "Wrong number of argument" end prc = Proc.new{ |val| val == search_value } if search_value result = right_child ? right_child._trav_right_first(nil, &prc) : nil return result unless result.nil? result = left_child ? left_child._trav_right_first(nil, &prc) : nil return result unless result.nil? result = prc.call(self) ? self : nil return result unless result.nil? nil end
connect(node)
click to toggle source
Calls superclass method
GraphNode#connect
# File lib/adt_utilit/binary_tree_node.rb, line 23 def connect(node) if @children.length >= 2 raise "BinaryTreeNode can have only two children" end if node.children.include?(self) raise "Undirected edges are not allowed in binary tree" end super(node) end
filled?()
click to toggle source
def switch(node)
unless self.children.include?(node) raise "No parent-child relationship exists" end replace_idx = self.children.find_index(node) self.children, node.children = node.children, self.children node.children[replace_idx] = self if self.parent.nil? node.parent = nil else replace_idx = self.parent.children.find_index(self) self.parent, node.parent = node, self.parent node.parent.children[replace_idx] = node end node.children.each{|child| child.parent = node} # debugger
end
# File lib/adt_utilit/binary_tree_node.rb, line 52 def filled? !!(left_child && right_child) end
left_child()
click to toggle source
# File lib/adt_utilit/binary_tree_node.rb, line 15 def left_child @children[0] end
right_child()
click to toggle source
# File lib/adt_utilit/binary_tree_node.rb, line 19 def right_child @children[1] end
trav_in_order(search_value = nil, &prc)
click to toggle source
# File lib/adt_utilit/binary_tree_node.rb, line 56 def trav_in_order(search_value = nil, &prc) if (search_value.nil? && !block_given?) || (search_value && block_given?) raise "Wrong number of argument" end prc = Proc.new{ |val| val == search_value } if search_value result = left_child ? left_child.trav_in_order(nil, &prc) : nil return result unless result.nil? result = prc.call(self.value) ? self : nil return result unless result.nil? result = right_child ? right_child.trav_in_order(nil, &prc) : nil return result unless result.nil? nil end
trav_post_order(search_value = nil, &prc)
click to toggle source
# File lib/adt_utilit/binary_tree_node.rb, line 92 def trav_post_order(search_value = nil, &prc) if (search_value.nil? && !block_given?) || (search_value && block_given?) raise "Wrong number of argument" end prc = Proc.new{ |val| val == search_value } if search_value result = left_child ? left_child.trav_post_order(nil, &prc) : nil return result unless result.nil? result = right_child ? right_child.trav_post_order(nil, &prc) : nil return result unless result.nil? result = prc.call(self.value) ? self : nil return result unless result.nil? nil end
trav_pre_order(search_value = nil, &prc)
click to toggle source
# File lib/adt_utilit/binary_tree_node.rb, line 74 def trav_pre_order(search_value = nil, &prc) if (search_value.nil? && !block_given?) || (search_value && block_given?) raise "Wrong number of argument" end prc = Proc.new{ |val| val == search_value } if search_value result = prc.call(self.value) ? self : nil return result unless result.nil? result = left_child ? left_child.trav_pre_order(nil, &prc) : nil return result unless result.nil? result = right_child ? right_child.trav_pre_order(nil, &prc) : nil return result unless result.nil? nil end