Skip to content

Commit 34c808b

Browse files
cclausspoyea
authored andcommitted
actions/checkout@v2 (TheAlgorithms#1643)
* actions/checkout@v2 https://github.com/actions/checkout/releases * fixup! Format Python code with psf/black push
1 parent 1b39858 commit 34c808b

File tree

7 files changed

+128
-117
lines changed

7 files changed

+128
-117
lines changed

.github/workflows/directory_writer.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ jobs:
66
build:
77
runs-on: ubuntu-latest
88
steps:
9-
- uses: actions/checkout@v1
9+
- uses: actions/checkout@v2
1010
- uses: actions/setup-python@v1
1111
with:
1212
python-version: 3.x

arithmetic_analysis/newton_raphson.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from sympy import diff
99

1010

11-
def newton_raphson(func: str, a: int, precision: int=10 ** -10) -> float:
11+
def newton_raphson(func: str, a: int, precision: int = 10 ** -10) -> float:
1212
""" Finds root from the point 'a' onwards by Newton-Raphson method
1313
>>> newton_raphson("sin(x)", 2)
1414
3.1415926536808043

ciphers/affine_cipher.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33

44
import cryptomath_module as cryptomath
55

6-
SYMBOLS = (r""" !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`"""
7-
r"""abcdefghijklmnopqrstuvwxyz{|}~""")
6+
SYMBOLS = (
7+
r""" !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`"""
8+
r"""abcdefghijklmnopqrstuvwxyz{|}~"""
9+
)
810

911

1012
def main():

data_structures/binary_tree/binary_search_tree.py

+43-38
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
'''
1+
"""
22
A binary search Tree
3-
'''
3+
"""
4+
5+
46
class Node:
57
def __init__(self, value, parent):
68
self.value = value
@@ -13,16 +15,11 @@ def __repr__(self):
1315

1416
if self.left is None and self.right is None:
1517
return str(self.value)
16-
return pformat(
17-
{
18-
"%s"
19-
% (self.value): (self.left, self.right)
20-
},
21-
indent=1,
22-
)
18+
return pformat({"%s" % (self.value): (self.left, self.right)}, indent=1,)
19+
2320

2421
class BinarySearchTree:
25-
def __init__(self, root = None):
22+
def __init__(self, root=None):
2623
self.root = root
2724

2825
def __str__(self):
@@ -32,10 +29,10 @@ def __str__(self):
3229
return str(self.root)
3330

3431
def __reassign_nodes(self, node, newChildren):
35-
if(newChildren is not None): # reset its kids
32+
if newChildren is not None: # reset its kids
3633
newChildren.parent = node.parent
37-
if(node.parent is not None): # reset its parent
38-
if(self.is_right(node)): # If it is the right children
34+
if node.parent is not None: # reset its parent
35+
if self.is_right(node): # If it is the right children
3936
node.parent.right = newChildren
4037
else:
4138
node.parent.left = newChildren
@@ -55,10 +52,10 @@ def __insert(self, value):
5552
new_node = Node(value, None) # create a new Node
5653
if self.empty(): # if Tree is empty
5754
self.root = new_node # set its root
58-
else: # Tree is not empty
59-
parent_node = self.root # from root
55+
else: # Tree is not empty
56+
parent_node = self.root # from root
6057
while True: # While we don't get to a leaf
61-
if value < parent_node.value: # We go left
58+
if value < parent_node.value: # We go left
6259
if parent_node.left == None:
6360
parent_node.left = new_node # We insert the new node in a leaf
6461
break
@@ -87,60 +84,65 @@ def search(self, value):
8784
node = node.left if value < node.value else node.right
8885
return node
8986

90-
def get_max(self, node = None):
87+
def get_max(self, node=None):
9188
"""
9289
We go deep on the right branch
9390
"""
9491
if node is None:
9592
node = self.root
9693
if not self.empty():
97-
while(node.right is not None):
94+
while node.right is not None:
9895
node = node.right
9996
return node
10097

101-
def get_min(self, node = None):
98+
def get_min(self, node=None):
10299
"""
103100
We go deep on the left branch
104101
"""
105-
if(node is None):
102+
if node is None:
106103
node = self.root
107-
if(not self.empty()):
104+
if not self.empty():
108105
node = self.root
109-
while(node.left is not None):
106+
while node.left is not None:
110107
node = node.left
111108
return node
112109

113110
def remove(self, value):
114-
node = self.search(value) # Look for the node with that label
115-
if(node is not None):
116-
if(node.left is None and node.right is None): # If it has no children
111+
node = self.search(value) # Look for the node with that label
112+
if node is not None:
113+
if node.left is None and node.right is None: # If it has no children
117114
self.__reassign_nodes(node, None)
118115
node = None
119-
elif(node.left is None): # Has only right children
116+
elif node.left is None: # Has only right children
120117
self.__reassign_nodes(node, node.right)
121-
elif(node.right is None): # Has only left children
118+
elif node.right is None: # Has only left children
122119
self.__reassign_nodes(node, node.left)
123120
else:
124-
tmpNode = self.get_max(node.left) # Gets the max value of the left branch
121+
tmpNode = self.get_max(
122+
node.left
123+
) # Gets the max value of the left branch
125124
self.remove(tmpNode.value)
126-
node.value = tmpNode.value # Assigns the value to the node to delete and keesp tree structure
127-
125+
node.value = (
126+
tmpNode.value
127+
) # Assigns the value to the node to delete and keesp tree structure
128+
128129
def preorder_traverse(self, node):
129130
if node is not None:
130131
yield node # Preorder Traversal
131132
yield from self.preorder_traverse(node.left)
132133
yield from self.preorder_traverse(node.right)
133134

134-
def traversal_tree(self, traversalFunction = None):
135+
def traversal_tree(self, traversalFunction=None):
135136
"""
136137
This function traversal the tree.
137138
You can pass a function to traversal the tree as needed by client code
138139
"""
139-
if(traversalFunction is None):
140+
if traversalFunction is None:
140141
return self.preorder_traverse(self.root)
141142
else:
142143
return traversalFunction(self.root)
143144

145+
144146
def postorder(curr_node):
145147
"""
146148
postOrder (left, right, self)
@@ -150,8 +152,9 @@ def postorder(curr_node):
150152
nodeList = postorder(curr_node.left) + postorder(curr_node.right) + [curr_node]
151153
return nodeList
152154

155+
153156
def binary_search_tree():
154-
r'''
157+
r"""
155158
Example
156159
8
157160
/ \
@@ -170,36 +173,38 @@ def binary_search_tree():
170173
Traceback (most recent call last):
171174
...
172175
IndexError: Warning: Tree is empty! please use another.
173-
'''
176+
"""
174177
testlist = (8, 3, 6, 1, 10, 14, 13, 4, 7)
175178
t = BinarySearchTree()
176179
for i in testlist:
177180
t.insert(i)
178181

179182
# Prints all the elements of the list in order traversal
180183
print(t)
181-
182-
if(t.search(6) is not None):
184+
185+
if t.search(6) is not None:
183186
print("The value 6 exists")
184187
else:
185188
print("The value 6 doesn't exist")
186189

187-
if(t.search(-1) is not None):
190+
if t.search(-1) is not None:
188191
print("The value -1 exists")
189192
else:
190193
print("The value -1 doesn't exist")
191194

192-
if(not t.empty()):
195+
if not t.empty():
193196
print("Max Value: ", t.get_max().value)
194197
print("Min Value: ", t.get_min().value)
195198

196199
for i in testlist:
197200
t.remove(i)
198201
print(t)
199202

203+
200204
二叉搜索树 = binary_search_tree
201205

202206
if __name__ == "__main__":
203207
import doctest
208+
204209
doctest.testmod()
205210
binary_search_tree()

0 commit comments

Comments
 (0)