Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit f5ce7be

Browse files
Ernestkeon
authored andcommittedFeb 15, 2019
Fix some bug (keon#366)
* Fix bug: explicity compare with None. * Unify node class * Hide slower code. * Update * Only test when file is main * Remove unnecessary code. * Rename and hide implementations. * Fix flake8 * Fix error. * Fix error. * Create __init__.py * Fix error.
1 parent d31ec0a commit f5ce7be

21 files changed

+143
-158
lines changed
 

‎algorithms/matrix/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

‎algorithms/tree/bin_tree_to_list.py

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
class Node():
2-
def __init__(self, val = 0):
3-
self.val = val
4-
self.left = None
5-
self.right = None
1+
from tree.tree import TreeNode
2+
63

74
def bin_tree_to_list(root):
85
"""
@@ -15,6 +12,7 @@ def bin_tree_to_list(root):
1512
root = root.left
1613
return root
1714

15+
1816
def bin_tree_to_list_util(root):
1917
if not root:
2018
return root
@@ -32,18 +30,8 @@ def bin_tree_to_list_util(root):
3230
root.right = right
3331
return root
3432

33+
3534
def print_tree(root):
3635
while root:
3736
print(root.val)
3837
root = root.right
39-
40-
tree = Node(10)
41-
tree.left = Node(12)
42-
tree.right = Node(15)
43-
tree.left.left = Node(25)
44-
tree.left.left.right = Node(100)
45-
tree.left.right = Node(30)
46-
tree.right.left = Node(36)
47-
48-
head = bin_tree_to_list(tree)
49-
print_tree(head)

‎algorithms/tree/binary_tree_paths.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
def binary_tree_paths(root):
22
res = []
3-
if not root:
3+
if root is None:
44
return res
55
dfs(res, root, str(root.val))
66
return res
77

8+
89
def dfs(res, root, cur):
9-
if not root.left and not root.right:
10+
if root.left is None and root.right is None:
1011
res.append(cur)
1112
if root.left:
1213
dfs(res, root.left, cur+'->'+str(root.left.val))

‎algorithms/tree/deepest_left.py

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,15 @@
1212
# 7
1313
# should return 4.
1414

15+
from tree.tree import TreeNode
1516

16-
class Node:
17-
def __init__(self, val = None):
18-
self.left = None
19-
self.right = None
20-
self.val = val
2117

2218
class DeepestLeft:
2319
def __init__(self):
2420
self.depth = 0
2521
self.Node = None
2622

23+
2724
def find_deepest_left(root, is_left, depth, res):
2825
if not root:
2926
return
@@ -33,15 +30,17 @@ def find_deepest_left(root, is_left, depth, res):
3330
find_deepest_left(root.left, True, depth + 1, res)
3431
find_deepest_left(root.right, False, depth + 1, res)
3532

36-
root = Node(1)
37-
root.left = Node(2)
38-
root.right = Node(3)
39-
root.left.left = Node(4)
40-
root.left.right = Node(5)
41-
root.right.right = Node(6)
42-
root.right.right.right = Node(7)
43-
44-
res = DeepestLeft()
45-
find_deepest_left(root, True, 1, res)
46-
if res.Node:
47-
print(res.Node.val)
33+
34+
if __name__ == '__main__':
35+
root = TreeNode(1)
36+
root.left = TreeNode(2)
37+
root.right = TreeNode(3)
38+
root.left.left = TreeNode(4)
39+
root.left.right = TreeNode(5)
40+
root.right.right = TreeNode(6)
41+
root.right.right.right = TreeNode(7)
42+
43+
res = DeepestLeft()
44+
find_deepest_left(root, True, 1, res)
45+
if res.Node:
46+
print(res.Node.val)

‎algorithms/tree/invert_tree.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# invert a binary tree
22

33
def reverse(root):
4-
if not root:
4+
if root is None:
55
return
66
root.left, root.right = root.right, root.left
77
if root.left:

‎algorithms/tree/is_balanced.py

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,36 @@
1-
21
def is_balanced(root):
2+
return __is_balanced_recursive(root)
3+
4+
5+
def __is_balanced_recursive(root):
36
"""
47
O(N) solution
58
"""
6-
return -1 != get_depth(root)
9+
return -1 != __get_depth(root)
10+
711

8-
def get_depth(root):
12+
def __get_depth(root):
913
"""
1014
return 0 if unbalanced else depth + 1
1115
"""
12-
if not root:
16+
if root is None:
1317
return 0
14-
left = get_depth(root.left)
15-
right = get_depth(root.right)
16-
if abs(left-right) > 1 or left == -1 or right == -1:
18+
left = __get_depth(root.left)
19+
right = __get_depth(root.right)
20+
if abs(left-right) > 1 or -1 in [left, right]:
1721
return -1
1822
return 1 + max(left, right)
1923

20-
################################
2124

22-
def is_balanced(root):
23-
"""
24-
O(N^2) solution
25-
"""
26-
left = max_height(root.left)
27-
right = max_height(root.right)
28-
return abs(left-right) <= 1 and is_balanced(root.left) and is_balanced(root.right)
25+
# def is_balanced(root):
26+
# """
27+
# O(N^2) solution
28+
# """
29+
# left = max_height(root.left)
30+
# right = max_height(root.right)
31+
# return abs(left-right) <= 1 and is_balanced(root.left) and is_balanced(root.right)
2932

30-
def max_height(root):
31-
if not root:
32-
return 0
33-
return max(max_height(root.left), max_height(root.right)) + 1
33+
# def max_height(root):
34+
# if root is None:
35+
# return 0
36+
# return max(max_height(root.left), max_height(root.right)) + 1

‎algorithms/tree/is_subtree.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,8 @@ def is_subtree(big, small):
6464

6565

6666
def comp(p, q):
67-
if not p and not q:
67+
if p is None and q is None:
6868
return True
69-
if p and q:
69+
if p is not None and q is not None:
7070
return p.val == q.val and comp(p.left,q.left) and comp(p.right, q.right)
7171
return False
72-
73-

‎algorithms/tree/is_symmetric.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,28 +21,28 @@
2121

2222
# TC: O(b) SC: O(log n)
2323
def is_symmetric(root):
24-
if not root:
24+
if root is None:
2525
return True
2626
return helper(root.left, root.right)
2727

2828

2929
def helper(p, q):
30-
if not p and not q:
30+
if p is None and q is None:
3131
return True
32-
if not p or not q or q.val != p.val:
32+
if p is not None or q is not None or q.val != p.val:
3333
return False
3434
return helper(p.left, q.right) and helper(p.right, q.left)
3535

3636

3737
def is_symmetric_iterative(root):
38-
if not root:
38+
if root is None:
3939
return True
4040
stack = [[root.left, root.right]]
4141
while stack:
4242
left, right = stack.pop() # popleft
43-
if not left and not right:
43+
if left is None and right is None:
4444
continue
45-
if not left or not right:
45+
if left is None or right is None:
4646
return False
4747
if left.val == right.val:
4848
stack.append([left.left, right.right])

‎algorithms/tree/longest_consecutive.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,15 @@ def longest_consecutive(root):
3030
:type root: TreeNode
3131
:rtype: int
3232
"""
33-
if not root:
33+
if root is None:
3434
return 0
3535
max_len = 0
3636
dfs(root, 0, root.val, max_len)
3737
return max_len
3838

3939

4040
def dfs(root, cur, target, max_len):
41-
if not root:
41+
if root is None:
4242
return
4343
if root.val == target:
4444
cur += 1

‎algorithms/tree/lowest_common_ancestor.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ def lca(root, p, q):
2828
:type q: TreeNode
2929
:rtype: TreeNode
3030
"""
31-
if not root or root is p or root is q:
31+
if root is None or root is p or root is q:
3232
return root
3333
left = lca(root.left, p, q)
3434
right = lca(root.right, p, q)
35-
if left and right:
35+
if left is not None and right is not None:
3636
return root
3737
return left if left else right

‎algorithms/tree/max_height.py

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,18 @@
55
longest path from the root node down to the farthest leaf node.
66
"""
77

8-
9-
class Node():
10-
def __init__(self, val = 0):
11-
self.val = val
12-
self.left = None
13-
self.right = None
14-
158
# def max_height(root):
16-
# if not root:
17-
# return 0
18-
# return max(maxDepth(root.left), maxDepth(root.right)) + 1
9+
# if not root:
10+
# return 0
11+
# return max(maxDepth(root.left), maxDepth(root.right)) + 1
1912

2013
# iterative
14+
15+
from tree.tree import TreeNode
16+
17+
2118
def max_height(root):
22-
if not root:
19+
if root is None:
2320
return 0
2421
height = 0
2522
queue = [root]
@@ -28,27 +25,30 @@ def max_height(root):
2825
level = []
2926
while queue:
3027
node = queue.pop(0)
31-
if node.left:
28+
if node.left is not None:
3229
level.append(node.left)
33-
if node.right:
30+
if node.right is not None:
3431
level.append(node.right)
3532
queue = level
3633
return height
3734

35+
3836
def print_tree(root):
39-
if root:
37+
if root is not None:
4038
print(root.val)
4139
print_tree(root.left)
4240
print_tree(root.right)
4341

44-
tree = Node(10)
45-
tree.left = Node(12)
46-
tree.right = Node(15)
47-
tree.left.left = Node(25)
48-
tree.left.left.right = Node(100)
49-
tree.left.right = Node(30)
50-
tree.right.left = Node(36)
51-
52-
height = max_height(tree)
53-
print_tree(tree)
54-
print("height:", height)
42+
43+
if __name__ == '__main__':
44+
tree = TreeNode(10)
45+
tree.left = TreeNode(12)
46+
tree.right = TreeNode(15)
47+
tree.left.left = TreeNode(25)
48+
tree.left.left.right = TreeNode(100)
49+
tree.left.right = TreeNode(30)
50+
tree.right.left = TreeNode(36)
51+
52+
height = max_height(tree)
53+
print_tree(tree)
54+
print("height:", height)

‎algorithms/tree/max_path_sum.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
2-
31
def max_path_sum(root):
42
maximum = float("-inf")
53
helper(root, maximum)
64
return maximum
75

86

97
def helper(root, maximum):
10-
if not root:
8+
if root is None:
119
return 0
1210
left = helper(root.left, maximum)
1311
right = helper(root.right, maximum)

‎algorithms/tree/min_height.py

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,54 @@
1-
class Node():
2-
def __init__(self, val = 0):
3-
self.val = val
4-
self.left = None
5-
self.right = None
1+
from tree.tree import TreeNode
62

73

84
def min_depth(self, root):
95
"""
106
:type root: TreeNode
117
:rtype: int
128
"""
13-
if not root:
9+
if root is None:
1410
return 0
15-
if not root.left or not root.right:
11+
if root.left is not None or root.right is not None:
1612
return max(self.minDepth(root.left), self.minDepth(root.right))+1
1713
return min(self.minDepth(root.left), self.minDepth(root.right)) + 1
1814

1915

2016
# iterative
2117
def min_height(root):
22-
if not root:
18+
if root is None:
2319
return 0
2420
height = 0
2521
level = [root]
2622
while level:
2723
height += 1
2824
new_level = []
2925
for node in level:
30-
if not node.left and not node.right:
26+
if node.left is None and node.right is None:
3127
return height
32-
if node.left:
28+
if node.left is not None:
3329
new_level.append(node.left)
34-
if node.right:
30+
if node.right is not None:
3531
new_level.append(node.right)
3632
level = new_level
3733
return height
3834

3935

4036
def print_tree(root):
41-
if root:
37+
if root is not None:
4238
print(root.val)
4339
print_tree(root.left)
4440
print_tree(root.right)
4541

46-
tree = Node(10)
47-
tree.left = Node(12)
48-
tree.right = Node(15)
49-
tree.left.left = Node(25)
50-
tree.left.left.right = Node(100)
51-
tree.left.right = Node(30)
52-
tree.right.left = Node(36)
53-
54-
height = min_height(tree)
55-
print_tree(tree)
56-
print("height:", height)
42+
43+
if __name__ == '__main__':
44+
tree = TreeNode(10)
45+
tree.left = TreeNode(12)
46+
tree.right = TreeNode(15)
47+
tree.left.left = TreeNode(25)
48+
tree.left.left.right = TreeNode(100)
49+
tree.left.right = TreeNode(30)
50+
tree.right.left = TreeNode(36)
51+
52+
height = min_height(tree)
53+
print_tree(tree)
54+
print("height:", height)

‎algorithms/tree/path_sum.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,43 +21,43 @@ def has_path_sum(root, sum):
2121
:type sum: int
2222
:rtype: bool
2323
"""
24-
if not root:
24+
if root is None:
2525
return False
26-
if not root.left and not root.right and root.val == sum:
26+
if root.left is None and root.right is None and root.val == sum:
2727
return True
2828
sum -= root.val
2929
return has_path_sum(root.left, sum) or has_path_sum(root.right, sum)
3030

3131

3232
# DFS with stack
3333
def has_path_sum2(root, sum):
34-
if not root:
34+
if root is None:
3535
return False
3636
stack = [(root, root.val)]
3737
while stack:
3838
node, val = stack.pop()
39-
if not node.left and not node.right:
39+
if node.left is None and node.right is None:
4040
if val == sum:
4141
return True
42-
if node.left:
42+
if node.left is not None:
4343
stack.append((node.left, val+node.left.val))
44-
if node.right:
44+
if node.right is not None:
4545
stack.append((node.right, val+node.right.val))
4646
return False
4747

4848

4949
# BFS with queue
5050
def has_path_sum3(root, sum):
51-
if not root:
51+
if root is None:
5252
return False
5353
queue = [(root, sum-root.val)]
5454
while queue:
5555
node, val = queue.pop(0) # popleft
56-
if not node.left and not node.right:
56+
if node.left is None and node.right is None:
5757
if val == 0:
5858
return True
59-
if node.left:
59+
if node.left is not None:
6060
queue.append((node.left, val-node.left.val))
61-
if node.right:
61+
if node.right is not None:
6262
queue.append((node.right, val-node.right.val))
6363
return False

‎algorithms/tree/path_sum2.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,51 +20,52 @@
2020

2121

2222
def path_sum(root, sum):
23-
if not root:
23+
if root is None:
2424
return []
2525
res = []
2626
dfs(root, sum, [], res)
2727
return res
2828

29+
2930
def dfs(root, sum, ls, res):
30-
if not root.left and not root.right and root.val == sum:
31+
if root.left is None and root.right is None and root.val == sum:
3132
ls.append(root.val)
3233
res.append(ls)
33-
if root.left:
34+
if root.left is not None:
3435
dfs(root.left, sum-root.val, ls+[root.val], res)
35-
if root.right:
36+
if root.right is not None:
3637
dfs(root.right, sum-root.val, ls+[root.val], res)
3738

3839

3940
# DFS with stack
4041
def path_sum2(root, s):
41-
if not root:
42+
if root is None:
4243
return []
4344
res = []
4445
stack = [(root, [root.val])]
4546
while stack:
4647
node, ls = stack.pop()
47-
if not node.left and not node.right and sum(ls) == s:
48+
if node.left is None and node.right is None and sum(ls) == s:
4849
res.append(ls)
49-
if node.left:
50+
if node.left is not None:
5051
stack.append((node.left, ls+[node.left.val]))
51-
if node.right:
52+
if node.right is not None:
5253
stack.append((node.right, ls+[node.right.val]))
5354
return res
5455

5556

5657
# BFS with queue
5758
def path_sum3(root, sum):
58-
if not root:
59+
if root is None:
5960
return []
6061
res = []
6162
queue = [(root, root.val, [root.val])]
6263
while queue:
6364
node, val, ls = queue.pop(0) # popleft
64-
if not node.left and not node.right and val == sum:
65+
if node.left is None and node.right is None and val == sum:
6566
res.append(ls)
66-
if node.left:
67+
if node.left is not None:
6768
queue.append((node.left, val+node.left.val, ls+[node.left.val]))
68-
if node.right:
69+
if node.right is not None:
6970
queue.append((node.right, val+node.right.val, ls+[node.right.val]))
7071
return res

‎algorithms/tree/pretty_print.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
# e -> Quin -> Book -> 5
99
# -> TV -> 2
1010
# f -> Adam -> Computer -> 7
11+
1112
from __future__ import print_function
1213

1314

‎algorithms/tree/same_tree.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88

99

1010
def is_same_tree(p, q):
11-
if not p and not q:
11+
if p is None and q is None:
1212
return True
13-
if p and q and p.val == q.val:
13+
if p is not None and q is not None and p.val == q.val:
1414
return is_same_tree(p.left, q.left) and is_same_tree(p.right, q.right)
1515
return False
1616

‎algorithms/tree/tree.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,3 @@ def __init__(self, val=0):
33
self.val = val
44
self.left = None
55
self.right = None
6-
7-
8-

‎tests/test_dp.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ class TestHosoyaTriangle(unittest.TestCase):
2121
"""
2222

2323
def test_hosoya(self):
24-
self.assertEqual([1], hosoya_testing(1))
24+
self.assertEqual([1], hosoya_triangle.hosoya_testing(1))
2525
self.assertEqual([1,
2626
1, 1,
2727
2, 1, 2,
2828
3, 2, 2, 3,
2929
5, 3, 4, 3, 5,
3030
8, 5, 6, 6, 5, 8],
31-
hosoya_testing(6))
31+
hosoya_triangle.hosoya_testing(6))
3232
self.assertEqual([1,
3333
1, 1,
3434
2, 1, 2,
@@ -39,7 +39,7 @@ def test_hosoya(self):
3939
21, 13, 16, 15, 15, 16, 13, 21,
4040
34, 21, 26, 24, 25, 24, 26, 21, 34,
4141
55, 34, 42, 39, 40, 40, 39, 42, 34, 55],
42-
hosoya_testing(10))
42+
hosoya_triangle.hosoya_testing(10))
4343

4444

4545
if __name__ == '__main__':

‎tests/test_matrix.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
class TestCroutMatrixDecomposition(unittest.TestCase):
1010
"""[summary]
11-
Test for the file crout_matrix_decomposition.py
11+
Test for the file crout_matrix_decomposition.crout_matrix_decomposition.py
1212
1313
Arguments:
1414
unittest {[type]} -- [description]
@@ -17,15 +17,17 @@ class TestCroutMatrixDecomposition(unittest.TestCase):
1717
def test_crout_matrix_decomposition(self):
1818
self.assertEqual(([[9.0, 0.0], [7.0, 0.0]],
1919
[[1.0, 1.0], [0.0, 1.0]]),
20-
crout_matrix_decomposition([[9,9], [7,7]]))
20+
crout_matrix_decomposition.crout_matrix_decomposition(
21+
[[9,9], [7,7]]))
2122

2223
self.assertEqual(([[1.0, 0.0, 0.0],
2324
[3.0, -2.0, 0.0],
2425
[6.0, -5.0, 0.0]],
2526
[[1.0, 2.0, 3.0],
2627
[0.0, 1.0, 2.0],
2728
[0.0, 0.0, 1.0]]),
28-
crout_matrix_decomposition([[1,2,3],[3,4,5],[6,7,8]]))
29+
crout_matrix_decomposition.crout_matrix_decomposition(
30+
[[1,2,3],[3,4,5],[6,7,8]]))
2931

3032
self.assertEqual(([[2.0, 0, 0, 0],
3133
[4.0, -1.0, 0, 0],
@@ -35,10 +37,8 @@ def test_crout_matrix_decomposition(self):
3537
[0, 1.0, 2.0, 1.0],
3638
[0, 0, 1.0, 0.0],
3739
[0, 0, 0, 1.0]]),
38-
crout_matrix_decomposition([[2,1,3,1],
39-
[4,1,4,1],
40-
[6,1,7,1],
41-
[8,1,9,1]]))
40+
crout_matrix_decomposition.crout_matrix_decomposition(
41+
[[2,1,3,1], [4,1,4,1], [6,1,7,1], [8,1,9,1]]))
4242

4343

4444

‎tests/test_strings.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
rotate,
3838
first_unique_char,
3939
repeat_substring,
40-
atbash_cipher
40+
atbash
4141
)
4242

4343
import unittest

0 commit comments

Comments
 (0)
Please sign in to comment.