forked from hoanhan101/algo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
balanced_binary_tree_test.go
109 lines (89 loc) · 2.19 KB
/
balanced_binary_tree_test.go
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
103
104
105
106
107
108
109
/*
Problem:
- Determine if a binary tree is height-balanced.
Example:
- Input:
1
2 3
4
Output: true
- Input:
1
2 3
4
5
Output: false
Approach:
A binary is balanced if
- its left subtree if balanced
- its right subtree if balanced
- the difference between the heights of left subtree and right subtree is
not more than 1.
*/
package lab
import (
"testing"
"github.com/hoanhan101/algo/common"
)
func TestIsBalanced(t *testing.T) {
t1 := &BinaryTree{nil, 1, nil}
t2 := &BinaryTree{nil, 1, nil}
t2.left = &BinaryTree{nil, 2, nil}
t3 := &BinaryTree{nil, 1, nil}
t3.left = &BinaryTree{nil, 2, nil}
t3.right = &BinaryTree{nil, 3, nil}
t3.right.right = &BinaryTree{nil, 4, nil}
t4 := &BinaryTree{nil, 1, nil}
t4.right = &BinaryTree{nil, 2, nil}
t4.right.right = &BinaryTree{nil, 3, nil}
t4.right.right.right = &BinaryTree{nil, 4, nil}
t5 := &BinaryTree{nil, 1, nil}
t5.left = &BinaryTree{nil, 2, nil}
t5.right = &BinaryTree{nil, 3, nil}
t5.left.left = &BinaryTree{nil, 4, nil}
t6 := &BinaryTree{nil, 1, nil}
t6.left = &BinaryTree{nil, 2, nil}
t6.right = &BinaryTree{nil, 3, nil}
t6.left.left = &BinaryTree{nil, 4, nil}
t6.left.left.right = &BinaryTree{nil, 5, nil}
tests := []struct {
in *BinaryTree
height int
balanced bool
}{
{t1, 1, true},
{t2, 2, true},
{t3, 3, true},
{t4, 4, false},
{t5, 3, true},
{t6, 4, false},
}
for _, tt := range tests {
h := height(tt.in)
b := isBalanced(tt.in)
common.Equal(t, tt.height, h)
common.Equal(t, tt.balanced, b)
}
}
func isBalanced(t *BinaryTree) bool {
if t == nil {
return true
}
// calculate the left and right subtrees' heights.
leftHeight := height(t.left)
rightHeight := height(t.right)
// if the difference between left and right subtrees' heights is less than
// 1 apart while the left and right subtree are balanced then the tree must
// be balanced.
if common.IsLessThan1Apart(leftHeight, rightHeight) && isBalanced(t.left) && isBalanced(t.right) {
return true
}
return false
}
// height returns the height of the binary tree.
func height(t *BinaryTree) int {
if t == nil {
return 0
}
return common.Max(height(t.left), height(t.right)) + 1
}