programming-examples/ruby/Algorithms/btree.rb

31 lines
579 B
Ruby
Raw Normal View History

2019-11-15 12:59:38 +01:00
# Checks if a binary tree is balanced in O(n) time and O(height) space,
# see examples in test/btree_test.rb
class BTree
Node = Struct.new(:left, :right)
def initialize(node)
@height = height(node)
end
def balanced?
@height != -1
end
private
def height(node)
return 0 if node.nil?
left_height = height(node.left)
return -1 if left_height == -1
right_height = height(node.right)
return -1 if right_height == -1
diff = left_height - right_height
return -1 if diff.abs > 1
[left_height, right_height].max + 1
end
end