mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-23 21:11:08 +00:00
actions/checkout@v2 (#1643)
* actions/checkout@v2 https://github.com/actions/checkout/releases * fixup! Format Python code with psf/black push
This commit is contained in:
parent
1b3985837f
commit
34c808b375
2
.github/workflows/directory_writer.yml
vendored
2
.github/workflows/directory_writer.yml
vendored
|
@ -6,7 +6,7 @@ jobs:
|
||||||
build:
|
build:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v1
|
- uses: actions/checkout@v2
|
||||||
- uses: actions/setup-python@v1
|
- uses: actions/setup-python@v1
|
||||||
with:
|
with:
|
||||||
python-version: 3.x
|
python-version: 3.x
|
||||||
|
|
|
@ -3,8 +3,10 @@ import sys
|
||||||
|
|
||||||
import cryptomath_module as cryptomath
|
import cryptomath_module as cryptomath
|
||||||
|
|
||||||
SYMBOLS = (r""" !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`"""
|
SYMBOLS = (
|
||||||
r"""abcdefghijklmnopqrstuvwxyz{|}~""")
|
r""" !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`"""
|
||||||
|
r"""abcdefghijklmnopqrstuvwxyz{|}~"""
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
'''
|
"""
|
||||||
A binary search Tree
|
A binary search Tree
|
||||||
'''
|
"""
|
||||||
|
|
||||||
|
|
||||||
class Node:
|
class Node:
|
||||||
def __init__(self, value, parent):
|
def __init__(self, value, parent):
|
||||||
self.value = value
|
self.value = value
|
||||||
|
@ -13,13 +15,8 @@ class Node:
|
||||||
|
|
||||||
if self.left is None and self.right is None:
|
if self.left is None and self.right is None:
|
||||||
return str(self.value)
|
return str(self.value)
|
||||||
return pformat(
|
return pformat({"%s" % (self.value): (self.left, self.right)}, indent=1,)
|
||||||
{
|
|
||||||
"%s"
|
|
||||||
% (self.value): (self.left, self.right)
|
|
||||||
},
|
|
||||||
indent=1,
|
|
||||||
)
|
|
||||||
|
|
||||||
class BinarySearchTree:
|
class BinarySearchTree:
|
||||||
def __init__(self, root=None):
|
def __init__(self, root=None):
|
||||||
|
@ -32,10 +29,10 @@ class BinarySearchTree:
|
||||||
return str(self.root)
|
return str(self.root)
|
||||||
|
|
||||||
def __reassign_nodes(self, node, newChildren):
|
def __reassign_nodes(self, node, newChildren):
|
||||||
if(newChildren is not None): # reset its kids
|
if newChildren is not None: # reset its kids
|
||||||
newChildren.parent = node.parent
|
newChildren.parent = node.parent
|
||||||
if(node.parent is not None): # reset its parent
|
if node.parent is not None: # reset its parent
|
||||||
if(self.is_right(node)): # If it is the right children
|
if self.is_right(node): # If it is the right children
|
||||||
node.parent.right = newChildren
|
node.parent.right = newChildren
|
||||||
else:
|
else:
|
||||||
node.parent.left = newChildren
|
node.parent.left = newChildren
|
||||||
|
@ -94,7 +91,7 @@ class BinarySearchTree:
|
||||||
if node is None:
|
if node is None:
|
||||||
node = self.root
|
node = self.root
|
||||||
if not self.empty():
|
if not self.empty():
|
||||||
while(node.right is not None):
|
while node.right is not None:
|
||||||
node = node.right
|
node = node.right
|
||||||
return node
|
return node
|
||||||
|
|
||||||
|
@ -102,28 +99,32 @@ class BinarySearchTree:
|
||||||
"""
|
"""
|
||||||
We go deep on the left branch
|
We go deep on the left branch
|
||||||
"""
|
"""
|
||||||
if(node is None):
|
if node is None:
|
||||||
node = self.root
|
node = self.root
|
||||||
if(not self.empty()):
|
if not self.empty():
|
||||||
node = self.root
|
node = self.root
|
||||||
while(node.left is not None):
|
while node.left is not None:
|
||||||
node = node.left
|
node = node.left
|
||||||
return node
|
return node
|
||||||
|
|
||||||
def remove(self, value):
|
def remove(self, value):
|
||||||
node = self.search(value) # Look for the node with that label
|
node = self.search(value) # Look for the node with that label
|
||||||
if(node is not None):
|
if node is not None:
|
||||||
if(node.left is None and node.right is None): # If it has no children
|
if node.left is None and node.right is None: # If it has no children
|
||||||
self.__reassign_nodes(node, None)
|
self.__reassign_nodes(node, None)
|
||||||
node = None
|
node = None
|
||||||
elif(node.left is None): # Has only right children
|
elif node.left is None: # Has only right children
|
||||||
self.__reassign_nodes(node, node.right)
|
self.__reassign_nodes(node, node.right)
|
||||||
elif(node.right is None): # Has only left children
|
elif node.right is None: # Has only left children
|
||||||
self.__reassign_nodes(node, node.left)
|
self.__reassign_nodes(node, node.left)
|
||||||
else:
|
else:
|
||||||
tmpNode = self.get_max(node.left) # Gets the max value of the left branch
|
tmpNode = self.get_max(
|
||||||
|
node.left
|
||||||
|
) # Gets the max value of the left branch
|
||||||
self.remove(tmpNode.value)
|
self.remove(tmpNode.value)
|
||||||
node.value = tmpNode.value # Assigns the value to the node to delete and keesp tree structure
|
node.value = (
|
||||||
|
tmpNode.value
|
||||||
|
) # Assigns the value to the node to delete and keesp tree structure
|
||||||
|
|
||||||
def preorder_traverse(self, node):
|
def preorder_traverse(self, node):
|
||||||
if node is not None:
|
if node is not None:
|
||||||
|
@ -136,11 +137,12 @@ class BinarySearchTree:
|
||||||
This function traversal the tree.
|
This function traversal the tree.
|
||||||
You can pass a function to traversal the tree as needed by client code
|
You can pass a function to traversal the tree as needed by client code
|
||||||
"""
|
"""
|
||||||
if(traversalFunction is None):
|
if traversalFunction is None:
|
||||||
return self.preorder_traverse(self.root)
|
return self.preorder_traverse(self.root)
|
||||||
else:
|
else:
|
||||||
return traversalFunction(self.root)
|
return traversalFunction(self.root)
|
||||||
|
|
||||||
|
|
||||||
def postorder(curr_node):
|
def postorder(curr_node):
|
||||||
"""
|
"""
|
||||||
postOrder (left, right, self)
|
postOrder (left, right, self)
|
||||||
|
@ -150,8 +152,9 @@ def postorder(curr_node):
|
||||||
nodeList = postorder(curr_node.left) + postorder(curr_node.right) + [curr_node]
|
nodeList = postorder(curr_node.left) + postorder(curr_node.right) + [curr_node]
|
||||||
return nodeList
|
return nodeList
|
||||||
|
|
||||||
|
|
||||||
def binary_search_tree():
|
def binary_search_tree():
|
||||||
r'''
|
r"""
|
||||||
Example
|
Example
|
||||||
8
|
8
|
||||||
/ \
|
/ \
|
||||||
|
@ -170,7 +173,7 @@ def binary_search_tree():
|
||||||
Traceback (most recent call last):
|
Traceback (most recent call last):
|
||||||
...
|
...
|
||||||
IndexError: Warning: Tree is empty! please use another.
|
IndexError: Warning: Tree is empty! please use another.
|
||||||
'''
|
"""
|
||||||
testlist = (8, 3, 6, 1, 10, 14, 13, 4, 7)
|
testlist = (8, 3, 6, 1, 10, 14, 13, 4, 7)
|
||||||
t = BinarySearchTree()
|
t = BinarySearchTree()
|
||||||
for i in testlist:
|
for i in testlist:
|
||||||
|
@ -179,17 +182,17 @@ def binary_search_tree():
|
||||||
# Prints all the elements of the list in order traversal
|
# Prints all the elements of the list in order traversal
|
||||||
print(t)
|
print(t)
|
||||||
|
|
||||||
if(t.search(6) is not None):
|
if t.search(6) is not None:
|
||||||
print("The value 6 exists")
|
print("The value 6 exists")
|
||||||
else:
|
else:
|
||||||
print("The value 6 doesn't exist")
|
print("The value 6 doesn't exist")
|
||||||
|
|
||||||
if(t.search(-1) is not None):
|
if t.search(-1) is not None:
|
||||||
print("The value -1 exists")
|
print("The value -1 exists")
|
||||||
else:
|
else:
|
||||||
print("The value -1 doesn't exist")
|
print("The value -1 doesn't exist")
|
||||||
|
|
||||||
if(not t.empty()):
|
if not t.empty():
|
||||||
print("Max Value: ", t.get_max().value)
|
print("Max Value: ", t.get_max().value)
|
||||||
print("Min Value: ", t.get_min().value)
|
print("Min Value: ", t.get_min().value)
|
||||||
|
|
||||||
|
@ -197,9 +200,11 @@ def binary_search_tree():
|
||||||
t.remove(i)
|
t.remove(i)
|
||||||
print(t)
|
print(t)
|
||||||
|
|
||||||
|
|
||||||
二叉搜索树 = binary_search_tree
|
二叉搜索树 = binary_search_tree
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
import doctest
|
import doctest
|
||||||
|
|
||||||
doctest.testmod()
|
doctest.testmod()
|
||||||
binary_search_tree()
|
binary_search_tree()
|
||||||
|
|
|
@ -1,12 +1,14 @@
|
||||||
# A complete working Python program to demonstrate all
|
# A complete working Python program to demonstrate all
|
||||||
# stack operations using a doubly linked list
|
# stack operations using a doubly linked list
|
||||||
|
|
||||||
|
|
||||||
class Node:
|
class Node:
|
||||||
def __init__(self, data):
|
def __init__(self, data):
|
||||||
self.data = data # Assign data
|
self.data = data # Assign data
|
||||||
self.next = None # Initialize next as null
|
self.next = None # Initialize next as null
|
||||||
self.prev = None # Initialize prev as null
|
self.prev = None # Initialize prev as null
|
||||||
|
|
||||||
|
|
||||||
class Stack:
|
class Stack:
|
||||||
"""
|
"""
|
||||||
>>> stack = Stack()
|
>>> stack = Stack()
|
||||||
|
@ -32,6 +34,7 @@ class Stack:
|
||||||
stack elements are:
|
stack elements are:
|
||||||
2->1->0->
|
2->1->0->
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.head = None
|
self.head = None
|
||||||
|
|
||||||
|
@ -80,7 +83,7 @@ class Stack:
|
||||||
|
|
||||||
|
|
||||||
# Code execution starts here
|
# Code execution starts here
|
||||||
if __name__=='__main__':
|
if __name__ == "__main__":
|
||||||
|
|
||||||
# Start with the empty stack
|
# Start with the empty stack
|
||||||
stack = Stack()
|
stack = Stack()
|
||||||
|
|
|
@ -36,6 +36,7 @@ def dp_count(S, m, n):
|
||||||
|
|
||||||
return table[n]
|
return table[n]
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
import doctest
|
import doctest
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user