diff --git a/__pycache__/test_find_maximum_binary_tree.cpython-39-pytest-5.4.3.pyc b/__pycache__/test_find_maximum_binary_tree.cpython-39-pytest-5.4.3.pyc new file mode 100644 index 0000000..a939b07 Binary files /dev/null and b/__pycache__/test_find_maximum_binary_tree.cpython-39-pytest-5.4.3.pyc differ diff --git a/data_structures_and_algorithms/challenges/array_reverse/__pycache__/array_search.cpython-39.pyc b/data_structures_and_algorithms/challenges/array_reverse/__pycache__/array_search.cpython-39.pyc new file mode 100644 index 0000000..1ead762 Binary files /dev/null and b/data_structures_and_algorithms/challenges/array_reverse/__pycache__/array_search.cpython-39.pyc differ diff --git a/data_structures_and_algorithms/challenges/find-maximum-binary-tree/18_find_maximum_value.jpg b/data_structures_and_algorithms/challenges/find-maximum-binary-tree/18_find_maximum_value.jpg new file mode 100644 index 0000000..9849e98 Binary files /dev/null and b/data_structures_and_algorithms/challenges/find-maximum-binary-tree/18_find_maximum_value.jpg differ diff --git a/data_structures_and_algorithms/challenges/find-maximum-binary-tree/bst.py b/data_structures_and_algorithms/challenges/find-maximum-binary-tree/bst.py new file mode 100644 index 0000000..05b3a14 --- /dev/null +++ b/data_structures_and_algorithms/challenges/find-maximum-binary-tree/bst.py @@ -0,0 +1,104 @@ +class Node: + def __init__(self, val): + self.val = val + self.right = None + self.left = None + + def __repr__(self): + return ''.format(self.val) + + def __str__(self): + return self.val + + +class BST: + def __init__(self, iter=[]): + self.root = None + if not isinstance(iter, (str, tuple, list)): + raise TypeError('It\'s not an iterable type.') + for item in iter: + self.insert(item) + + def __repr__(self): + return ''.format(self.root.val) + + def __str__(self): + val = self.root.val + return str(val) + + def in_order(self, operation): + def _walk(node=None): + if node is None: + return + + if node.left is not None: + _walk(node.left) + + operation(node) + + if node.right is not None: + _walk(node.right) + + _walk(self.root) + + def pre_order(self, operation): + def _walk(node=None): + if node is None: + return + + operation(node) + + if node.left is not None: + _walk(node.left) + + if node.right is not None: + _walk(node.right) + + _walk(self.root) + + def post_order(self, operation): + def _walk(node=None): + if node is None: + return + + if node.left is not None: + _walk(node.left) + + if node.right is not None: + _walk(node.right) + + operation(node) + + _walk(self.root) + + def insert(self, val): + if isinstance(val, Node): + node = val + else: + node = Node(val) + + current = self.root + + if self.root is None: + self.root = node + return node + + while current: + try: + if val >= current.val: + if current.right is not None: + current = current.right + else: + current.right = node + break + + elif val < current.val: + if current.left is not None: + current = current.left + else: + current.left = node + break + except TypeError: + raise TypeError('the value cannot be compared') + + return node \ No newline at end of file diff --git a/data_structures_and_algorithms/challenges/find-maximum-binary-tree/find_maximum_binary_tree.py b/data_structures_and_algorithms/challenges/find-maximum-binary-tree/find_maximum_binary_tree.py new file mode 100644 index 0000000..93c1962 --- /dev/null +++ b/data_structures_and_algorithms/challenges/find-maximum-binary-tree/find_maximum_binary_tree.py @@ -0,0 +1,29 @@ +from .bst import BST + + +def find_maximum_value(bst): + """ function finding max value from bst input \ + it checks if input is of BST type \ + and if root is empty + """ + if not isinstance(bst, BST): + raise TypeError('It\'s not a BST type.') + + if bst.root is None: + raise ValueError('BST is empty.') + + max = 0 + this_level = [bst.root] + + while this_level: + next_level = list() + for n in this_level: + if n.val > max: + max = n.val + if n.left: + next_level.append(n.left) + if n.right: + next_level.append(n.right) + this_level = next_level + + return max \ No newline at end of file diff --git a/data_structures_and_algorithms/challenges/find-maximum-binary-tree/read.md b/data_structures_and_algorithms/challenges/find-maximum-binary-tree/read.md new file mode 100644 index 0000000..c9b1833 --- /dev/null +++ b/data_structures_and_algorithms/challenges/find-maximum-binary-tree/read.md @@ -0,0 +1,2 @@ +![image](https://github.com/MsDiala/data-structures-and-algorithms-python/blob/find-maximum-binary-tree/data_structures_and_algorithms/challenges/find-maximum-binary-tree/18_find_maximum_value.jpg) +I got help from prev student in codefelowes diff --git a/data_structures_and_algorithms/data_structures/BinaryTree/__pycache__/maximum_binary_tree.cpython-38.pyc b/data_structures_and_algorithms/data_structures/BinaryTree/__pycache__/maximum_binary_tree.cpython-38.pyc new file mode 100644 index 0000000..66f4cc6 Binary files /dev/null and b/data_structures_and_algorithms/data_structures/BinaryTree/__pycache__/maximum_binary_tree.cpython-38.pyc differ diff --git a/data_structures_and_algorithms/data_structures/BinaryTree/__pycache__/maximum_binary_tree.cpython-39.pyc b/data_structures_and_algorithms/data_structures/BinaryTree/__pycache__/maximum_binary_tree.cpython-39.pyc new file mode 100644 index 0000000..9fa826c Binary files /dev/null and b/data_structures_and_algorithms/data_structures/BinaryTree/__pycache__/maximum_binary_tree.cpython-39.pyc differ diff --git a/data_structures_and_algorithms/data_structures/BinaryTree/maximum_binary_tree.py b/data_structures_and_algorithms/data_structures/BinaryTree/maximum_binary_tree.py new file mode 100644 index 0000000..5ba7dd7 --- /dev/null +++ b/data_structures_and_algorithms/data_structures/BinaryTree/maximum_binary_tree.py @@ -0,0 +1,20 @@ +from data_structures_and_algorithms.data_structures.tree.tree import Ktree + +def find_maximum_value(bst): + '''Find the maximum balue in a tree''' + + if not isinstance(bst,Ktree): + raise TypeError('argument must be of type cur: + cur = node.val + + bst.pre_order(update) + return cur \ No newline at end of file diff --git a/data_structures_and_algorithms/data_structures/linked_list/__pycache__/linked_list.cpython-39.pyc b/data_structures_and_algorithms/data_structures/linked_list/__pycache__/linked_list.cpython-39.pyc new file mode 100644 index 0000000..eaf66c6 Binary files /dev/null and b/data_structures_and_algorithms/data_structures/linked_list/__pycache__/linked_list.cpython-39.pyc differ diff --git a/data_structures_and_algorithms/data_structures/stacks_and_queues/__pycache__/stacks_and_queues.cpython-38.pyc b/data_structures_and_algorithms/data_structures/stacks_and_queues/__pycache__/stacks_and_queues.cpython-38.pyc index 6ced29c..6f9d976 100644 Binary files a/data_structures_and_algorithms/data_structures/stacks_and_queues/__pycache__/stacks_and_queues.cpython-38.pyc and b/data_structures_and_algorithms/data_structures/stacks_and_queues/__pycache__/stacks_and_queues.cpython-38.pyc differ diff --git a/data_structures_and_algorithms/data_structures/stacks_and_queues/__pycache__/stacks_and_queues.cpython-39.pyc b/data_structures_and_algorithms/data_structures/stacks_and_queues/__pycache__/stacks_and_queues.cpython-39.pyc new file mode 100644 index 0000000..8a47d8b Binary files /dev/null and b/data_structures_and_algorithms/data_structures/stacks_and_queues/__pycache__/stacks_and_queues.cpython-39.pyc differ diff --git a/data_structures_and_algorithms/data_structures/stacks_and_queues/__pycache__/test_stacks_and_queues.cpython-38-pytest-6.1.2.pyc b/data_structures_and_algorithms/data_structures/stacks_and_queues/__pycache__/test_stacks_and_queues.cpython-38-pytest-6.1.2.pyc index 2e3f193..5e33f1b 100644 Binary files a/data_structures_and_algorithms/data_structures/stacks_and_queues/__pycache__/test_stacks_and_queues.cpython-38-pytest-6.1.2.pyc and b/data_structures_and_algorithms/data_structures/stacks_and_queues/__pycache__/test_stacks_and_queues.cpython-38-pytest-6.1.2.pyc differ diff --git a/data_structures_and_algorithms/data_structures/stacks_and_queues/__pycache__/test_stacks_and_queues.cpython-39-pytest-5.4.3.pyc b/data_structures_and_algorithms/data_structures/stacks_and_queues/__pycache__/test_stacks_and_queues.cpython-39-pytest-5.4.3.pyc new file mode 100644 index 0000000..b4f7592 Binary files /dev/null and b/data_structures_and_algorithms/data_structures/stacks_and_queues/__pycache__/test_stacks_and_queues.cpython-39-pytest-5.4.3.pyc differ diff --git a/data_structures_and_algorithms/__init__.py b/data_structures_and_algorithms/data_structures/tree/__init__.py similarity index 100% rename from data_structures_and_algorithms/__init__.py rename to data_structures_and_algorithms/data_structures/tree/__init__.py diff --git a/data_structures_and_algorithms/data_structures/tree/__pycache__/__init__.cpython-38.pyc b/data_structures_and_algorithms/data_structures/tree/__pycache__/__init__.cpython-38.pyc new file mode 100644 index 0000000..f0e5fe9 Binary files /dev/null and b/data_structures_and_algorithms/data_structures/tree/__pycache__/__init__.cpython-38.pyc differ diff --git a/data_structures_and_algorithms/data_structures/tree/__pycache__/__init__.cpython-39.pyc b/data_structures_and_algorithms/data_structures/tree/__pycache__/__init__.cpython-39.pyc new file mode 100644 index 0000000..b5fd4ab Binary files /dev/null and b/data_structures_and_algorithms/data_structures/tree/__pycache__/__init__.cpython-39.pyc differ diff --git a/data_structures_and_algorithms/data_structures/tree/__pycache__/tree.cpython-38.pyc b/data_structures_and_algorithms/data_structures/tree/__pycache__/tree.cpython-38.pyc index 65e5d35..07dc3d5 100644 Binary files a/data_structures_and_algorithms/data_structures/tree/__pycache__/tree.cpython-38.pyc and b/data_structures_and_algorithms/data_structures/tree/__pycache__/tree.cpython-38.pyc differ diff --git a/data_structures_and_algorithms/data_structures/tree/__pycache__/tree.cpython-39.pyc b/data_structures_and_algorithms/data_structures/tree/__pycache__/tree.cpython-39.pyc new file mode 100644 index 0000000..79b84c0 Binary files /dev/null and b/data_structures_and_algorithms/data_structures/tree/__pycache__/tree.cpython-39.pyc differ diff --git a/data_structures_and_algorithms/data_structures/tree/tree.py b/data_structures_and_algorithms/data_structures/tree/tree.py index 47cbe49..8aa08d7 100644 --- a/data_structures_and_algorithms/data_structures/tree/tree.py +++ b/data_structures_and_algorithms/data_structures/tree/tree.py @@ -1,125 +1,105 @@ -class Node: - """Node class definition.""" - def __init__(self, val=None): - """Create an instance of Node object.""" +class Node: + def __init__(self, val): self.val = val - self.children = [] - self._next = next + self.right = None + self.left = None def __repr__(self): - """Node class representation.""" - return ''.format(self.val) + return self.val def __str__(self): - """Node class string printout.""" - return 'Node Val: {}'.format(self.val) + return self.val -class Queue: - def __init__(self, iter=[]): - self.front = None - self.back = None - self._length = 0 - - if not isinstance(iter, (list, dict, tuple)): - """ check for iterable """ - raise TypeError('It is not iterable.') - for i in iter: - self.enqueue(i) - - def enqueue(self, val): - """ add a value, increase size by 1""" - node = Node(val) - if self._length == 0: - self.front = self.back = node - self._length += 1 - return node - self.back.next = node - self.back = node - self._length += 1 - return node - - def dequeue(self): - """ remove node from the front of queue """ - if self._length == 0: - raise IndexError('You cannot dequeue() when front is empty') - - temp = self.front - self.front = temp.next - self._length -= 1 - return temp - -class KTree: - """Ktree class definition.""" - def __init__(self): - """Create an instance of KTree object.""" +class Ktree: + def __init__(self, iter=[]): self.root = None + + for item in iter: + self.insert(item) def __repr__(self): - """Ktree class representation.""" - return ''.format(self.root.val) + return ''.format(self.root.val) def __str__(self): - """Ktree class string printout.""" - return 'KTree Root Val: {}'.format(self.root.val) + return self.root.val - def pre_order(self, operation): - """Ktree pre_order traversal.""" + def in_order(self, operation): + """in_order traversal""" def _walk(node=None): if node is None: return - operation(node) + if node.left is not None: + _walk(node.left) + operation(node.val) - for child in node.children: - _walk(child) + if node.right is not None: + _walk(node.right) - _walk(self.root) + if self.root is None: + return False + else: + _walk(self.root) + + def insert(self, val): + node = Node(val) - def post_order(self, operation): - """Ktree post_order traversal.""" + def _insert(current, node): + """isert node in bst""" + if node.val >= current.val: + if current.right is None: + current.right = node + else: + current = current.right + _insert(current, node) + if node.val < current.val: + if current.left is None: + current.left = node + else: + current = current.left + _insert(current, node) + + if self.root is None: + self.root = node + return node + else: + current = self.root + _insert(current, node) + + def pre_order(self, operation): + """preorder traversal""" def _walk(node=None): if node is None: return + operation(node.val) + + if node.left is not None: + _walk(node.left) + + if node.right is not None: + _walk(node.right) + + if self.root is None: + return False + else: + _walk(self.root) + + def post_order(self, operation): + """postorder traversal""" + def _walk(node=None): + if node.left is not None: + _walk(node.left) - for child in node.children: - _walk(child) - - operation(node) - - _walk(self.root) - - def breadth_first_traversal(self, operation): - """Ktree breadth_first_traversal.""" - queue = Queue() - queue.enqueue(self.root) - while queue._length > 0: - current = queue.dequeue() - operation(current) - for child in current.children: - queue.enqueue(child) - - def insert(self, val, parent=None): - """Insert a value at first instance of given parent.""" - if parent is None: - if self.root is None: - self.root = Node(val) - return self.root - raise ValueError('parent node is none.') - - node = Node(val) - - def _walk(curr=None): - if curr is None: - return + if node.right is not None: + _walk(node.right) + operation(node.val) - if curr.val == parent: - curr.children.append(node) + if node is None: return - - for child in curr.children: - _walk(child) - if node in child.children: - return - _walk(self.root) \ No newline at end of file + + if self.root is None: + return False + else: + _walk(self.root) \ No newline at end of file diff --git a/test_find_maximum_binary_tree.py b/test_find_maximum_binary_tree.py new file mode 100644 index 0000000..46d4948 --- /dev/null +++ b/test_find_maximum_binary_tree.py @@ -0,0 +1,27 @@ +import pytest +from .find_maximum_value_binary_tree import find_maximum_value +from .bst import BST + + +def test_typeerror_true(): + """ test on input type """ + with pytest.raises(TypeError): + find_maximum_value([1, 2, 3]) + + +def test_maximum_five(five_element_bst): + """ test for finding max from bst with five nodes. """ + b = find_maximum_value(five_element_bst) + assert b == 9 + + +def test_maximum_seven(seven_element_bst): + """ test for finding max from bst with seven nodes. """ + b = find_maximum_value(seven_element_bst) + assert b == 30 + + +def test_empty(): + """ test for empty BST input """ + with pytest.raises(ValueError): + find_maximum_value(BST()) \ No newline at end of file diff --git a/tests/__pycache__/__init__.cpython-39.pyc b/tests/__pycache__/__init__.cpython-39.pyc new file mode 100644 index 0000000..719353b Binary files /dev/null and b/tests/__pycache__/__init__.cpython-39.pyc differ diff --git a/tests/__pycache__/test_data_structures_and_algorithms.cpython-39-pytest-5.4.3.pyc b/tests/__pycache__/test_data_structures_and_algorithms.cpython-39-pytest-5.4.3.pyc new file mode 100644 index 0000000..02859ca Binary files /dev/null and b/tests/__pycache__/test_data_structures_and_algorithms.cpython-39-pytest-5.4.3.pyc differ diff --git a/tests/challenges/__pycache__/test_array_search.cpython-39-pytest-5.4.3.pyc b/tests/challenges/__pycache__/test_array_search.cpython-39-pytest-5.4.3.pyc new file mode 100644 index 0000000..15da284 Binary files /dev/null and b/tests/challenges/__pycache__/test_array_search.cpython-39-pytest-5.4.3.pyc differ diff --git a/tests/challenges/__pycache__/test_array_shift.cpython-39-pytest-5.4.3.pyc b/tests/challenges/__pycache__/test_array_shift.cpython-39-pytest-5.4.3.pyc new file mode 100644 index 0000000..d77a7a9 Binary files /dev/null and b/tests/challenges/__pycache__/test_array_shift.cpython-39-pytest-5.4.3.pyc differ diff --git a/tests/data_structures/__pycache__/test_linked_list.cpython-39-pytest-5.4.3.pyc b/tests/data_structures/__pycache__/test_linked_list.cpython-39-pytest-5.4.3.pyc new file mode 100644 index 0000000..f6de1b8 Binary files /dev/null and b/tests/data_structures/__pycache__/test_linked_list.cpython-39-pytest-5.4.3.pyc differ diff --git a/tests/data_structures/__pycache__/test_maximum_binary_tree.cpython-38-pytest-6.1.2.pyc b/tests/data_structures/__pycache__/test_maximum_binary_tree.cpython-38-pytest-6.1.2.pyc new file mode 100644 index 0000000..9b1110c Binary files /dev/null and b/tests/data_structures/__pycache__/test_maximum_binary_tree.cpython-38-pytest-6.1.2.pyc differ diff --git a/tests/data_structures/__pycache__/test_maximum_binary_tree.cpython-39-pytest-5.4.3.pyc b/tests/data_structures/__pycache__/test_maximum_binary_tree.cpython-39-pytest-5.4.3.pyc new file mode 100644 index 0000000..76245fa Binary files /dev/null and b/tests/data_structures/__pycache__/test_maximum_binary_tree.cpython-39-pytest-5.4.3.pyc differ diff --git a/tests/data_structures/__pycache__/test_tree.cpython-38-pytest-6.1.2.pyc b/tests/data_structures/__pycache__/test_tree.cpython-38-pytest-6.1.2.pyc index 36eadd2..b8c3349 100644 Binary files a/tests/data_structures/__pycache__/test_tree.cpython-38-pytest-6.1.2.pyc and b/tests/data_structures/__pycache__/test_tree.cpython-38-pytest-6.1.2.pyc differ diff --git a/tests/data_structures/__pycache__/test_tree.cpython-39-pytest-5.4.3.pyc b/tests/data_structures/__pycache__/test_tree.cpython-39-pytest-5.4.3.pyc new file mode 100644 index 0000000..49b10de Binary files /dev/null and b/tests/data_structures/__pycache__/test_tree.cpython-39-pytest-5.4.3.pyc differ diff --git a/tests/data_structures/test_maximum_binary_tree.py b/tests/data_structures/test_maximum_binary_tree.py new file mode 100644 index 0000000..9d851f8 --- /dev/null +++ b/tests/data_structures/test_maximum_binary_tree.py @@ -0,0 +1,22 @@ +import pytest +from data_structures_and_algorithms.data_structures.tree.tree import Ktree +from data_structures_and_algorithms.data_structures.BinaryTree.maximum_binary_tree import find_maximum_value + +def test_invalid_arg(): + '''Test invalid parameter''' + with pytest.raises(TypeError): + find_maximum_value(4) + +def test_empty_Ktree(): + '''Test an empty Ktree has no max value''' + assert find_maximum_value(Ktree()) is None + +def test_single_Ktree(): + '''Test single valued Ktree''' + assert find_maximum_value(Ktree([1])) == 1 + +def test_multi_Ktree(): + '''Test multi-valued non-balanced Ktree''' + l = [5,2,15,33,1,8,7,4,2,66,5,1,0,-2] + b = Ktree(l) + assert find_maximum_value(b) == 66 \ No newline at end of file diff --git a/tests/data_structures/test_tree.py b/tests/data_structures/test_tree.py index bb66f80..21c5b9c 100644 --- a/tests/data_structures/test_tree.py +++ b/tests/data_structures/test_tree.py @@ -1,51 +1,87 @@ + import pytest -from data_structures_and_algorithms.data_structures.tree.tree import Node +from data_structures_and_algorithms.data_structures.tree.tree import Ktree +from data_structures_and_algorithms.data_structures.tree.tree import Node as nd -def test_insert_small_ktree(small_tree): - """This checks if a value inserted correctly.""" - # import pdb; pdb.set_trace() - small_tree.insert(11, 3) - assert small_tree.root.children[1].children[0].val == 11 +@pytest.fixture +def empty_Ktree(): + """make empty Ktree""" + return Ktree() -def test_insert_small_ktree_no_parent(small_tree): - """This tests if ValueError is raised when passing no parent.""" - with pytest.raises(ValueError): - small_tree.insert(8) +@pytest.fixture +def node(): + """make node""" + return nd(5) + +@pytest.fixture +def small_Ktree(): + return Ktree([4, 3, 3.5, 2, 5, 6, 7]) -def test_pre_order_traverse(small_tree): - """Confirm pre_order traversal.""" - ls = [] - small_tree.pre_order(lambda n: ls.append(n.val)) - assert ls == [5, 9, 3] +def test_constractor(empty_Ktree): + """test constarctor""" + assert empty_Ktree.root is None -def test_post_order_traversal(small_tree): - """Confirm post_order traversal.""" - ls = [] - small_tree.post_order(lambda n: ls.append(n.val)) - assert ls == [9, 3, 5] +def test_node(node): + """test node object""" + assert node.right is None + assert node.left is None -def test_breadth_first_traversal(small_tree): - """Confirm breadth_first_traversal.""" - ls = [] - small_tree.breadth_first_traversal(lambda n: ls.append(n.val)) - assert ls == [5, 9, 3] +def test_value_of_node(node): + """test repr and str of node""" + assert node.val == 5 -def test_ktree_node(): - """Test if K-tree node is made properly.""" - nd = Node(34) - assert nd.val == 34 - assert nd.children == [] - assert repr(nd) == '' - assert str(nd) == 'Node Val: 34' +def test_insert_empty(empty_Ktree): + """insert in empty Ktree""" + empty_Ktree.insert(5) + assert empty_Ktree.root.val == 5 -def test_ktree(small_tree): - """Test K-tree str and repr.""" - assert repr(small_tree) == '' - assert str(small_tree) == 'KTree Root Val: 5' \ No newline at end of file + +def test_insert_empty_two(empty_Ktree): + """insert two items""" + empty_Ktree.insert(3) + empty_Ktree.insert(10) + assert empty_Ktree.root.val == 3 + assert empty_Ktree.root.right.val == 10 + assert empty_Ktree.root.left is None + + +def test_repr_small_Ktree(small_Ktree): + """test repr of small Ktree""" + assert small_Ktree.root.val == 4 + assert small_Ktree.root.right.val == 5 + + +def test_insert_for_small_Ktree(small_Ktree): + """test insert in small Ktree""" + small_Ktree.insert(1) + small_Ktree.insert(1.5) + assert small_Ktree.root.left.val == 3 + assert small_Ktree.root.right.val == 5 + + +def test_inorder_traverse(small_Ktree): + """test in order tarversal""" + a = [] + small_Ktree.in_order(a.append) + assert a == [2, 3, 3.5, 4, 5, 6, 7] + + +def test_pre_order(small_Ktree): + """test preorder traversal""" + a = [] + small_Ktree.pre_order(a.append) + assert a == [4, 3, 2, 3.5, 5, 6, 7] + + +def test_post_order(small_Ktree): + """test preorder""" + a = [] + small_Ktree.post_order(a.append) + assert a == [2, 3.5, 3, 7, 6, 5, 4] \ No newline at end of file