Class: BinaryNode

Inherits:
Object
  • Object
show all
Defined in:
lib/binary_search_tree/binary_node.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/binary_node.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/binary_node.rb', line 2

def height
  @height
end

#keyObject

Returns the value of attribute key.



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

def key
  @key
end

#leftObject

Returns the value of attribute left.



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

def left
  @left
end

#parentObject

Returns the value of attribute parent.



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

def parent
  @parent
end

#rightObject

Returns the value of attribute right.



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

def right
  @right
end

#valueObject

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_factorObject



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

Returns:

  • (Boolean)


11
12
13
# File 'lib/binary_search_tree/binary_node.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/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