-
Notifications
You must be signed in to change notification settings - Fork 0
/
binary_tree_test.py
181 lines (145 loc) · 4.56 KB
/
binary_tree_test.py
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import unittest
import io
import sys
import math
class Node:
def __init__(self, data):
self.data = data
self.left = None
self.right = None
def __str__(self):
return str(self.data)
class BinarySearchTree:
def __init__(self):
self.root = None
# Definition from https://www.hackerrank.com/challenges/tree-height-of-a-binary-tree/problem
def create(self, val):
if self.root == None:
self.root = Node(val)
else:
current = self.root
while True:
if val < current.data:
if current.left:
current = current.left
else:
current.left = Node(val)
break
elif val > current.data:
if current.right:
current = current.right
else:
current.right = Node(val)
break
else:
break
def traverse_inorder(root):
# Left - Parent - Right
if root is None:
return []
return traverse_inorder(root.left) + [root.data] + traverse_inorder(root.right)
def traverse_preorder(root):
# Parent - Left - Right
if root is None:
return []
return [root.data] + traverse_preorder(root.left) + traverse_preorder(root.right)
def traverse_postorder(root):
# Left - Right - Parent
if root is None:
return []
return traverse_postorder(root.left) + traverse_postorder(root.right) + [root.data]
# https://www.hackerrank.com/challenges/tree-height-of-a-binary-tree/problem
def height(root):
if root is None:
return 0
if root.left is None and root.right is None:
return 0
else:
max_height_left = height(root.left)
max_height_right = height(root.right)
max_height = max(max_height_left, max_height_right)
return max_height + 1
def get_root(arr):
tree = BinarySearchTree()
for i in arr:
tree.create(i)
return tree.root
def is_isometric(a, b):
if a is None and b is None:
return True
if a is None or b is None:
return False
return (
is_isometric(a.left, b.left)
and a.data == b.data
and is_isometric(a.right, b.right)
)
# https://youtu.be/i_Q0v_Ct5lY
def checkBST(root, min=-math.inf, max=math.inf):
if root is None:
return True
if root.data < min or root.data > max:
return False
# Example: if root is 10,
# then on the left it can be upto 9 at most
# and on the right it has to be 11 at least
return checkBST(root.left, min, root.data - 1) and checkBST(
root.right, root.data + 1, max
)
class MyTest(unittest.TestCase):
def setUp(self):
self.capturedOutput = io.StringIO()
sys.stdout = self.capturedOutput
def tearDown(self):
sys.stdout = sys.__stdout__
def test_height_1(self):
arr = [3, 5, 2, 1, 4, 6, 7]
root = get_root(arr)
received = height(root)
expected = 3
self.assertEqual(received, expected)
def test_height_2(self):
arr = [15]
root = get_root(arr)
received = height(root)
expected = 0
self.assertEqual(received, expected)
def test_height_3(self):
arr = [3, 1, 7, 5, 4]
root = get_root(arr)
received = height(root)
expected = 3
self.assertEqual(received, expected)
def test_traverse_inorder(self):
arr = [2, 1, 3]
root = get_root(arr)
received = traverse_inorder(root)
expected = [1, 2, 3]
self.assertEqual(received, expected)
def test_traverse_preorder(self):
arr = [2, 1, 3]
root = get_root(arr)
received = traverse_preorder(root)
expected = [2, 1, 3]
self.assertEqual(received, expected)
def test_traverse_postorder(self):
arr = [2, 1, 3]
root = get_root(arr)
received = traverse_postorder(root)
expected = [1, 3, 2]
self.assertEqual(received, expected)
def test_is_isometric_true(self):
arr = [2, 1, 3]
a = get_root(arr)
b = get_root(arr)
received = is_isometric(a, b)
expected = True
self.assertEqual(received, expected)
def test_is_isometric_false(self):
a = get_root([2, 1, 3])
b = get_root([1, 2, 3])
received = is_isometric(a, b)
expected = False
self.assertEqual(received, expected)
if __name__ == "__main__":
unittest.main()