Class: CompSci::BinaryTree
- Defined in:
- lib/compsci/tree.rb
Direct Known Subclasses
Instance Attribute Summary
Attributes inherited from NaryTree
Attributes inherited from Tree
Instance Method Summary collapse
- #display(width: 80) ⇒ Object (also: #to_s)
-
#initialize(node_class, val) ⇒ BinaryTree
constructor
A new instance of BinaryTree.
Methods inherited from NaryTree
#open_parent, #open_parent?, #open_sibling, power_of?, #push
Methods inherited from Tree
#bf_search, #df_search, #df_search_generic
Constructor Details
#initialize(node_class, val) ⇒ BinaryTree
Returns a new instance of BinaryTree.
109 110 111 |
# File 'lib/compsci/tree.rb', line 109 def initialize(node_class, val) super(node_class, val, child_slots: 2) end |
Instance Method Details
#display(width: 80) ⇒ Object Also known as: to_s
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/compsci/tree.rb', line 113 def display(width: 80) count = 0 str = '' self.bf_search { |node| count += 1 level = Math.log(count, 2).floor block_width = width / (2**level) val = node.to_s str += "\n" if 2**level == count and count > 1 space = [(block_width + val.size) / 2, val.size + 1].max str += val.ljust(space, ' ').rjust(block_width, ' ') false # keep searching to visit every node } str end |