Class: BinaryNode

Inherits:
Object
  • Object
show all
Defined in:
lib/binary_search_tree.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, value, parent) ⇒ BinaryNode

Returns a new instance of BinaryNode.



4
5
6
7
8
9
# File 'lib/binary_search_tree.rb', line 4

def initialize key, value, parent
  @key = key
  @value = value
  @parent = parent
  @height = 0
end

Instance Attribute Details

#heightObject

Returns the value of attribute height.



2
3
4
# File 'lib/binary_search_tree.rb', line 2

def height
  @height
end

#keyObject

Returns the value of attribute key.



2
3
4
# File 'lib/binary_search_tree.rb', line 2

def key
  @key
end

#leftObject

Returns the value of attribute left.



2
3
4
# File 'lib/binary_search_tree.rb', line 2

def left
  @left
end

#parentObject

Returns the value of attribute parent.



2
3
4
# File 'lib/binary_search_tree.rb', line 2

def parent
  @parent
end

#rightObject

Returns the value of attribute right.



2
3
4
# File 'lib/binary_search_tree.rb', line 2

def right
  @right
end

#valueObject

Returns the value of attribute value.



2
3
4
# File 'lib/binary_search_tree.rb', line 2

def value
  @value
end

Instance Method Details

#balance_factorObject



27
28
29
# File 'lib/binary_search_tree.rb', line 27

def balance_factor
  (left.height rescue -1) - (right.height rescue -1)
end

#is_leaf?Boolean

Returns:

  • (Boolean)


11
12
13
# File 'lib/binary_search_tree.rb', line 11

def is_leaf?
  height.zero?
end

#max_children_heightObject



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/binary_search_tree.rb', line 15

def max_children_height
  if left.present? && right.present?
    [left.height, right.height].max
  elsif left.present?
    left.height
  elsif right.present?
    right.height
  else
    -1
  end
end