Class: BinaryNode
- Inherits:
-
Object
- Object
- BinaryNode
- Defined in:
- lib/binary_search_tree/binary_node.rb
Instance Attribute Summary collapse
-
#height ⇒ Object
Returns the value of attribute height.
-
#key ⇒ Object
Returns the value of attribute key.
-
#left ⇒ Object
Returns the value of attribute left.
-
#parent ⇒ Object
Returns the value of attribute parent.
-
#right ⇒ Object
Returns the value of attribute right.
-
#value ⇒ Object
Returns the value of attribute value.
Instance Method Summary collapse
- #balance_factor ⇒ Object
-
#initialize(key, value, parent) ⇒ BinaryNode
constructor
A new instance of BinaryNode.
- #is_leaf? ⇒ Boolean
- #max_children_height ⇒ Object
Constructor Details
#initialize(key, value, parent) ⇒ BinaryNode
Returns a new instance of BinaryNode.
4 5 6 7 8 9 |
# File 'lib/binary_search_tree/binary_node.rb', line 4 def initialize key, value, parent @key = key @value = value @parent = parent @height = 0 end |
Instance Attribute Details
#height ⇒ Object
Returns the value of attribute height.
2 3 4 |
# File 'lib/binary_search_tree/binary_node.rb', line 2 def height @height end |
#key ⇒ Object
Returns the value of attribute key.
2 3 4 |
# File 'lib/binary_search_tree/binary_node.rb', line 2 def key @key end |
#left ⇒ Object
Returns the value of attribute left.
2 3 4 |
# File 'lib/binary_search_tree/binary_node.rb', line 2 def left @left end |
#parent ⇒ Object
Returns the value of attribute parent.
2 3 4 |
# File 'lib/binary_search_tree/binary_node.rb', line 2 def parent @parent end |
#right ⇒ Object
Returns the value of attribute right.
2 3 4 |
# File 'lib/binary_search_tree/binary_node.rb', line 2 def right @right end |
#value ⇒ Object
Returns the value of attribute value.
2 3 4 |
# File 'lib/binary_search_tree/binary_node.rb', line 2 def value @value end |
Instance Method Details
#balance_factor ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/binary_search_tree/binary_node.rb', line 27 def balance_factor left_height = if left left.height else -1 end right_height = if right right.height else -1 end left_height - right_height end |
#is_leaf? ⇒ Boolean
11 12 13 |
# File 'lib/binary_search_tree/binary_node.rb', line 11 def is_leaf? height.zero? end |
#max_children_height ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/binary_search_tree/binary_node.rb', line 15 def max_children_height if left && right [left.height, right.height].max elsif left left.height elsif right right.height else -1 end end |