-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBinary_Trees.rb
102 lines (77 loc) · 2.43 KB
/
Binary_Trees.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
require 'benchmark/ips'
class Node
attr_reader :data
attr_accessor :left, :right
def initialize(data)
@data = data
end
end
def array_to_tree(array, i)
return nil if i >= array.length || array[i] == 0
node = Node.new(array[i])
node.left = array_to_tree(array, 2*i+1)
node.right = array_to_tree(array, 2*i+2)
node
end
def pre_order(node)
return '' if node.nil?
result = "#{node.data} "
result += pre_order(node.left)
result += pre_order(node.right)
end
def post_order(node)
# your code here
return '' if node.nil?
result = post_order(node.left)
result += post_order(node.right)
result += "#{node.data} "
end
# Binary Tree Height
def binary_tree_height(node)
return 0 if node.nil?
left = binary_tree_height(node.left)
right = binary_tree_height(node.right)
[left, right].max + 1
end
def isBalanced?(node)
return true if node.nil?
left_height = binary_tree_height(node.left)
right_height = binary_tree_height(node.right)
return true if (left_height - right_height).abs <=1 && isBalanced?(node.left) && isBalanced?(node.right)
return false;
end
def is_balanced_optimized?(node, is_balanced)
# base case empty tree or tree is not balaced
return 0 if node.nil? || !$is_balanced
left_height = is_balanced_optimized?(node.left, $is_balanced)
right_height = is_balanced_optimized?(node.right, $is_balanced)
$is_balanced = false if (left_height - right_height).abs > 1
[left_height, right_height].max + 1
end
def balanced_tree_optimized?(array_tree)
# write your code here
$is_balanced = true
tree = array_to_tree(array_tree, 0)
is_balanced_optimized?(tree, $is_balanced)
$is_balanced
end
def balanced_tree?(array_tree)
# write your code here
tree = array_to_tree(array_tree, 0)
isBalanced?(tree)
end
# Benchmark.ips do |x|
# x.report("bal: "){balanced_tree?([1, 2, 0, 3, 4, 0, 0]) }
# x.report("bal_optimized: "){balanced_tree_optimized?([1, 2, 0, 3, 4, 0, 0]) }
# x.compare!
# end
tree = array_to_tree([1, 7, 5, 2, 6, 0, 9, 3, 7, 5, 11, 0, 0, 4, 0], 0)
# tree = array_to_tree([1, 2, 0, 3, 4, 0, 0], 0)
# puts post_order(tree)
#=> 3 4 1 5 6 2 10
puts binary_tree_height(tree)
# puts balanced_tree?([1, 2, 0, 3, 4, 0, 0])
# => 3
# [1, 7, 5, 2, 6, 0, 9, 3, 7, 5, 11, 0, 0, 4, 0] =>4
# [5, 3, 2, 9, 0, 0, 7, 0, 0, 0, 0, 0, 0, 5, 0] => 4
# [1, 2, 3, 4, 0, 5, 6, 7, 8, 0, 0, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 0, 0, 0, 0, 0, 0]=>5