From bd666817a9ee8c4dff3c7e1b437014fadb36068e Mon Sep 17 00:00:00 2001 From: warmachine028 Date: Tue, 28 Dec 2021 00:21:30 +0530 Subject: [PATCH] Added NEW MODULES in DataStax Library --- .gitignore | 4 + README.md | 40 ++++++---- datastax/__init__.py | 4 +- datastax/__main__.py | 21 +++-- datastax/arrays/__init__.py | 7 ++ datastax/arrays/queue.py | 69 +++++++++++++++++ datastax/arrays/stack.py | 56 ++++++++++++++ datastax/linkedlists/__init__.py | 4 +- datastax/linkedlists/linked_list.py | 19 ----- datastax/linkedlists/queue.py | 85 +++++++++++++++++++++ datastax/trees/__init__.py | 5 +- datastax/trees/binary_tree.py | 1 + datastax/trees/max_heap_tree.py | 58 ++++++++++++++ datastax/trees/min_heap_tree.py | 59 ++++++++++++++ desktop.ini | 2 - setup.py | 66 +++++++++++----- tests/arrays_tests/__init__.py | 0 tests/arrays_tests/queue_test.py | 54 +++++++++++++ tests/arrays_tests/stack_test.py | 18 +++++ tests/linkedlists_tests/linked_list_test.py | 16 ++++ tests/linkedlists_tests/queue_test.py | 64 ++++++++++++++++ tests/trees_tests/max_heap_tree_test.py | 19 +++++ tests/trees_tests/min_heap_tree_test.py | 24 ++++++ 23 files changed, 632 insertions(+), 63 deletions(-) create mode 100644 datastax/arrays/__init__.py create mode 100644 datastax/arrays/queue.py create mode 100644 datastax/arrays/stack.py create mode 100644 datastax/linkedlists/queue.py create mode 100644 datastax/trees/max_heap_tree.py create mode 100644 datastax/trees/min_heap_tree.py delete mode 100644 desktop.ini create mode 100644 tests/arrays_tests/__init__.py create mode 100644 tests/arrays_tests/queue_test.py create mode 100644 tests/arrays_tests/stack_test.py create mode 100644 tests/linkedlists_tests/queue_test.py create mode 100644 tests/trees_tests/max_heap_tree_test.py create mode 100644 tests/trees_tests/min_heap_tree_test.py diff --git a/.gitignore b/.gitignore index 480fca0..34e00e1 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,10 @@ # Created by https://www.toptal.com/developers/gitignore/api/python # Edit at https://www.toptal.com/developers/gitignore?templates=python +### MyExtra ### +.Box_Drawing_Symbols.txt +desktop.ini + ### Python ### # Byte-compiled / optimized / DLL files __pycache__/ diff --git a/README.md b/README.md index 4374f7d..9baf56a 100644 --- a/README.md +++ b/README.md @@ -16,11 +16,11 @@ ## What's New? -- Added terminal demo -- Improved type hinting using mypy static type checker -- Removed few bugs and potential errors -- Purified test_structure -- Improvised for uploading in PyPI +- Added Arrays + - Included Array Implementation of Stack + - Included Array Implementation of Queue +- Added LinkedList Implementation of Queue +- Added Heap Trees (Max Heap and Min Heap) ## Table of Contents @@ -65,12 +65,10 @@ ## Installation -1. Clone this repository locally -2. Reach base folder _datastax/_ -3. Use the python package manager [pip](https://pip.pypa.io/en/stable/) to install datastax. +1. Use the python package manager [pip](https://pip.pypa.io/en/stable/) to install datastax. ```bash -pip install . +pip install -i https://test.pypi.org/simple/ datastax ``` ## Usage @@ -93,12 +91,14 @@ pip install . Available modules are: 1. LinkedLists 2. Trees - + Usage $ py datastax [data] Data Structures: - trees Hierarchical DS - linkedlists Linear DS + -> trees Hierarchical DS + -> linkedlists Linear DS + -> arrays Fixed Size Linear DS + ``` - Then follow as the instruction guides @@ -130,5 +130,19 @@ print(bt) ┌──┴──┐ 4 5 """ -``` +--------------------------------------------------- +from datastax.trees import MinHeapTree +MiHT = MinHeapTree([1, 2, 4, 2, 6, 5, 9, 18, 3, 2]) +print(MiHT) +## OUTPUT +""" + 1 + ┌───────────┴───────────┐ + 2 4 + ┌─────┴─────┐ ┌─────┴─────┐ + 2 2 5 9 + ┌──┴──┐ ┌──┘ + 18 3 6 +""" +``` diff --git a/datastax/__init__.py b/datastax/__init__.py index 5b007d7..31d548b 100644 --- a/datastax/__init__.py +++ b/datastax/__init__.py @@ -1,5 +1,5 @@ # __init__.py # Versioning of the datastax package -__version__ = "0.0.2" -__all__ = ['trees', 'linkedlists'] +__version__ = "0.0.3" +__all__ = ['trees', 'linkedlists', 'arrays'] diff --git a/datastax/__main__.py b/datastax/__main__.py index 29a098d..606f3b3 100644 --- a/datastax/__main__.py +++ b/datastax/__main__.py @@ -1,18 +1,28 @@ import sys -from datastax import linkedlists as ll, trees +from datastax import linkedlists as ll, trees, arrays def main(): if len(sys.argv) > 1: data_structure = sys.argv[1].lower() data = sys.argv[2:] if len(sys.argv) > 2 else [*range(5)] # take User given data or default - if data_structure in ('linkedlist', "linkedlists"): + if data_structure in ('array', 'arrays'): + queue = arrays.Queue(len(data)) + stack = arrays.Stack(len(data)) + for i in data: + stack.push(i) + queue.enqueue(i) + print("Visuals for Arrays:\n\n" + f"1. Stack: \n{stack}\n\n" + f"2. Queue: \n{queue}\n\n") + elif data_structure in ('linkedlist', "linkedlists"): print("Visuals for LinkedLists:\n\n" f"1. Singly Linked List: \n{ll.LinkedList(data)}\n\n" f"2. Doubly Linked List: \n{ll.DoublyLinkedList(data)}\n\n" f"3. Circular Linked List: \n{ll.CircularLinkedList(data)}\n\n" - f"4. Doubly Circular List: \n{ll.DoublyCircularList(data)}\n\n") + f"4. Doubly Circular List: \n{ll.DoublyCircularList(data)}\n\n" + f"5. Queue: \n{ll.Queue(data)}\n\n") elif data_structure in ('tree', 'trees'): print("Visuals for Trees:\n\n" f"1. Binary Tree \n{trees.BinaryTree(data)}\n\n" @@ -27,8 +37,9 @@ def main(): "\nUsage\n" "$ py datastax [data]\n" "Data Structures: \n" - " trees Hierarchical DS\n" - " linkedlists Linear DS") + "-> trees Hierarchical DS\n" + "-> linkedlists Linear DS\n" + "-> arrays Fixed Size Linear DS") if __name__ == '__main__': diff --git a/datastax/arrays/__init__.py b/datastax/arrays/__init__.py new file mode 100644 index 0000000..3ffa34e --- /dev/null +++ b/datastax/arrays/__init__.py @@ -0,0 +1,7 @@ +from .queue import Queue +from .stack import Stack + +__all__ = [ + 'Queue', + 'Stack' +] diff --git a/datastax/arrays/queue.py b/datastax/arrays/queue.py new file mode 100644 index 0000000..9fef293 --- /dev/null +++ b/datastax/arrays/queue.py @@ -0,0 +1,69 @@ +# Queue Implementation using Lists (Pseudo Arrays) +import math +from typing import Any, Union + + +class Queue: + def __init__(self, capacity: int = None): + self._capacity = capacity if capacity is not None else math.inf + self._array: list[Any] = [] + self._front = self._rear = 0 + + @property + def array_repr(self) -> list[Any]: + return self._array[self._front:self._rear] + + def is_full(self) -> bool: + return len(self._array) == self._capacity + + def is_empty(self) -> bool: + return not len(self._array) + + def enqueue(self, item: Any) -> int: + if self.is_full(): + print("WARNING: THE QUEUE IS ALREADY FULL, CANT ENQUEUE ANY FURTHER") + return -1 + self._array.append(item) + self._rear += 1 + return 0 + + def dequeue(self) -> Union[int, Any]: + if self.is_empty() or self._front >= self._rear: + print("WARNING: THE QUEUE IS ALREADY EMPTY, CANT DEQUEUE ANY FURTHER") + return -1 + deleted_item = self._array[self._front] + self._front += 1 + return deleted_item + + def peek(self) -> str: + if self.is_empty() or self._front >= self._rear: return "QUEUE EMPTY" + return str(self._array[self._front]) + + def __str__(self): + if self.is_empty(): return '┌───────────────────┐\n' \ + '│ QUEUE EMPTY │\n' \ + '└───────────────────┘' + padding = 4 + max_breadth = max(len(str(item)) for item in self._array) + padding + middle_part = 'FRONT -> │' + upper_part = f"{' ' * (len(middle_part) - 1)}┌" + lower_part = f"{' ' * (len(middle_part) - 1)}└" + if self._front: # Representing Garbage Values with '╳' + for _ in self._array[:self._front]: + middle_part += f"{'╳'.center(max_breadth)}│" + upper_part += f"{'─' * max_breadth}┬" + lower_part += f"{'─' * max_breadth}┴" + upper_part = upper_part[:-1] + '╥' + middle_part = middle_part[:-1] + '║' + lower_part = lower_part[:-1] + '╨' + for item in self._array[self._front:]: + middle_part += f'{str(item).center(max_breadth)}│' + upper_part += f"{'─' * max_breadth}┬" + lower_part += f"{'─' * max_breadth}┴" + upper_part = f"{upper_part[:-1]}{'╖' if len(self._array) == self._front else '┐'}\n" + middle_part += ' <- REAR\n' + lower_part = f"{lower_part[:-1]}{'╜' if len(self._array) == self._front else '┘'}\n" + return upper_part + middle_part + lower_part + + def __repr__(self): + return self.__str__() diff --git a/datastax/arrays/stack.py b/datastax/arrays/stack.py new file mode 100644 index 0000000..e1689e3 --- /dev/null +++ b/datastax/arrays/stack.py @@ -0,0 +1,56 @@ +# Stack Implementation using Lists (Pseudo Arrays) +import math +from typing import Any, Union + + +class Stack: + def __init__(self, capacity: int = None): + self.capacity = capacity if capacity is not None else math.inf + self._array: list[Any] = [] + + @property + def array(self): + return self._array + + def is_full(self) -> bool: + return len(self._array) == self.capacity + + def is_empty(self) -> bool: + return not len(self._array) + + def push(self, item: Any) -> int: + if self.is_full(): # Overflow Condition + print('OVERFLOW WARNING: ATTEMPTED PUSH OPERATION IN FULL STACK') + return -1 + self._array.append(item) + return 0 + + def pop(self) -> Union[int, Any]: + if self.is_empty(): # Underflow Condition handled + print('UNDERFLOW WARNING: ATTEMPTED POP OPERATION FROM EMPTY STACK') + return -1 + return self._array.pop() + + def peek(self) -> str: + return 'STACK EMPTY' if self.is_empty() else self._array[-1] + + def __str__(self): + if self.is_empty(): return '│STACK EMPTY│\n' \ + '╰───────────╯\n' + padding = 9 + maximum_breadth = (max(len(str(item)) for item in self._array)) + padding + if self.is_full(): string = f"┌{'─' * maximum_breadth}┐\n" # For FULLY LOADED STACK + elif (len(self._array) < 0.7 * self.capacity) or (self.capacity - len(self._array) > 5): + string = f"│{' ' * maximum_breadth}│\n" \ + f":{' ' * maximum_breadth}:\n" # Shrink visualization + else: string = f"│{' ' * maximum_breadth}│\n" * 2 * (self.capacity - len(self._array)) # Expand Visualization + + for n, item in enumerate(self._array[::-1]): + top = '' if self.is_full() and not n else f"├{'─' * maximum_breadth}┤\n" + bottom = f"│{str(item).center(maximum_breadth)}│ {'<- TOP' if not n else ''}\n" + string += top + bottom + string += f"╰{'─' * maximum_breadth}╯\n" + return string + + def __repr__(self): + return self.__str__() diff --git a/datastax/linkedlists/__init__.py b/datastax/linkedlists/__init__.py index c0c3fdc..1ade1c5 100644 --- a/datastax/linkedlists/__init__.py +++ b/datastax/linkedlists/__init__.py @@ -2,10 +2,12 @@ from .doubly_circular_list import DoublyCircularList from .doubly_linked_list import DoublyNode, DoublyLinkedList from .linked_list import LinkedList, Node +from .queue import Queue __all__ = [ 'LinkedList', 'Node', 'DoublyLinkedList', 'DoublyNode', 'CircularLinkedList', - 'DoublyCircularList' + 'DoublyCircularList', + 'Queue' ] diff --git a/datastax/linkedlists/linked_list.py b/datastax/linkedlists/linked_list.py index 2031710..da55547 100644 --- a/datastax/linkedlists/linked_list.py +++ b/datastax/linkedlists/linked_list.py @@ -52,22 +52,3 @@ def __str__(self, head: Node = None): def __repr__(self): return self.__str__() - - -# __main__ -if __name__ == '__main__': - LL = LinkedList() - print(LL) - LL.insert(10) - LL.insert(11) - LL.append(90) - LL.insert(199) - LL.append(109) - print(LL) - - D = LinkedList([*range(5)]) - D.insert(10) - D.insert(20) - D.append(199) - print(D) - # print(D.__str__(True)) diff --git a/datastax/linkedlists/queue.py b/datastax/linkedlists/queue.py new file mode 100644 index 0000000..8f4f322 --- /dev/null +++ b/datastax/linkedlists/queue.py @@ -0,0 +1,85 @@ +# Queue implementation using LinkedLists +from __future__ import annotations + +from sys import maxsize +from typing import Any, Optional, Union + +from datastax.linkedlists.linked_list import LinkedList, Node + + +class Queue(LinkedList): + def __init__(self, array: list[Any] = None, capacity: int = None): + super().__init__() + self._rear = 0 + if capacity is not None and capacity < 0: + print("Capacity can't be negative" + "Setting unbounded capacity") + self._capacity = capacity if capacity is not None and capacity > 0 else maxsize + if array and array[0] is not None: + for item in array[:self._capacity]: + self.enqueue(item) + + def is_empty(self) -> bool: + return self.head is None + + def is_full(self) -> bool: + return self._rear == self._capacity + + def enqueue(self, data: Any) -> int: + if self.is_full(): + print("WARNING: THE QUEUE IS ALREADY FULL, CANT ENQUEUE ANY FURTHER") + return -1 + super().append(data) + self._rear += 1 + return 0 + + def dequeue(self) -> Union[int, Any]: + if self.is_empty(): + print("WARNING: THE QUEUE IS ALREADY EMPTY, CANT DEQUEUE ANY FURTHER") + return -1 + # Dequeue Operation + deleted_node = self.head + deleted_item = deleted_node.data + self._head = self.head.next + self._rear -= 1 + return deleted_item + + def peek(self) -> str: + if self.is_empty(): return "QUEUE EMPTY" + return str(self._tail.data if self._tail else None) + + def append(self, data: Any) -> None: + print("WARNING: Method not available here.") + + def insert(self, data: Any) -> None: + print("WARNING: Method not implemented here." + "Please use enqueue method to insert") + + def __str__(self, head: Node = None): + def maximum_breadth(ref: Optional[Node]) -> int: + result = 0 + while ref: + result = max(len(str(ref.data)), result) + ref = ref.next + return result + + if self.is_empty(): return '╔═══════════════════╗\n' \ + '║ QUEUE EMPTY ║\n' \ + '╚═══════════════════╝' + padding = 4 + max_breadth = maximum_breadth(self.head) + padding + middle_part = 'FRONT -> ' + upper_part = f"{' ' * (len(middle_part) - 1)} " + lower_part = f"{' ' * (len(middle_part) - 1)} " + temp = self.head + while temp: + item = temp.data + upper_part += f"╔{'═' * max_breadth}╗ " + middle_part += f'║{str(item).center(max_breadth)}║ <-' + lower_part += f"╚{'═' * max_breadth}╝ " + temp = temp.next + upper_part = f"{upper_part[:-1]}\n" + middle_part += ' REAR\n' + lower_part = f"{lower_part[:-1]}\n" + + return upper_part + middle_part + lower_part diff --git a/datastax/trees/__init__.py b/datastax/trees/__init__.py index 701b442..dc2ed4f 100644 --- a/datastax/trees/__init__.py +++ b/datastax/trees/__init__.py @@ -1,9 +1,12 @@ from .avl_tree import AVLTree, AVLNode from .binary_search_tree import BinarySearchTree from .binary_tree import BinaryTree, TreeNode +from .max_heap_tree import MaxHeapTree +from .min_heap_tree import MinHeapTree __all__ = [ "BinaryTree", "TreeNode", "BinarySearchTree", - "AVLTree", "AVLNode" + "AVLTree", "AVLNode", + "MaxHeapTree", "MinHeapTree" ] diff --git a/datastax/trees/binary_tree.py b/datastax/trees/binary_tree.py index 576eef1..c53a8fb 100644 --- a/datastax/trees/binary_tree.py +++ b/datastax/trees/binary_tree.py @@ -132,6 +132,7 @@ def _construct(self, array: list[Any] = None) -> Optional[BinaryTree]: def __str__(self, root: TreeNode = None): root = root or self.root if not root: return " NULL" + lines: list[list[Optional[str]]] = [] level: list[Optional[TreeNode]] = [root] nodes: int = 1 diff --git a/datastax/trees/max_heap_tree.py b/datastax/trees/max_heap_tree.py new file mode 100644 index 0000000..c3b7525 --- /dev/null +++ b/datastax/trees/max_heap_tree.py @@ -0,0 +1,58 @@ +# Max Heap Tree Implementation +from __future__ import annotations + +from typing import Optional, Any + +from datastax.trees.binary_tree import BinaryTree + + +class MaxHeapTree(BinaryTree): + def __init__(self, array: list[Any] = None, root=None): + self.heap: list[Any] = [] + super().__init__(array, root) + + def array_repr(self) -> list[Any]: + return self.heap + + def _construct(self, array: list[Any] = None) -> Optional[MaxHeapTree]: + if not array or array[0] is None: return None + for item in array: + try: + self.heappush(item) + except TypeError as error: + print(error) + break + return self + + def _shift_up(self, heap: list[Any], index: int, length: int) -> None: + root = index + left_child = root * 2 + 1 + right_child = root * 2 + 2 + if left_child < length and heap[left_child] > heap[root]: + root = left_child + if right_child < length and heap[right_child] > heap[root]: + root = right_child + if root == index: return + heap[root], heap[index] = heap[index], heap[root] + self._shift_up(heap, root, length) + + def heapify(self, array: list[Any]) -> None: + for item in array: + self.heappush(item) + + def heappop(self, heap: list[Any] = None) -> Optional[Any]: + if not heap: heap = self.heap + if not heap: return None + deleted_item = heap[0] + heap[0] = heap.pop() + self._shift_up(heap, 0, len(heap)) + if heap: super()._construct(heap) + else: self._root = None + return deleted_item + + def heappush(self, item: Any, heap: list[Any] = None) -> None: + if heap is None: heap = self.heap + heap.append(item) + for i in range(len(heap) - 1, -1, -1): + self._shift_up(heap, i, len(heap)) + super()._construct(heap) diff --git a/datastax/trees/min_heap_tree.py b/datastax/trees/min_heap_tree.py new file mode 100644 index 0000000..ecba3f9 --- /dev/null +++ b/datastax/trees/min_heap_tree.py @@ -0,0 +1,59 @@ +# Min Heap Tree Implementation +from __future__ import annotations + +from typing import Optional, Any + +from datastax.trees.binary_tree import BinaryTree + + +class MinHeapTree(BinaryTree): + def __init__(self, array: list[Any] = None, root=None): + self.heap: list[Any] = [] + super().__init__(array, root) + + def array_repr(self) -> list[Any]: + return self.heap + + def _construct(self, array: list[Any] = None) -> Optional[MinHeapTree]: + if not array or array[0] is None: return None + for item in array: + try: + self.heappush(item) + except TypeError as error: + print(error) + break + return self + + def _shift_up(self, heap: list[Any], index: int, length: int) -> None: + root = index + left_child = root * 2 + 1 + right_child = root * 2 + 2 + if left_child < length and heap[left_child] < heap[root]: + root = left_child + if right_child < length and heap[right_child] < heap[root]: + root = right_child + if root == index: return + heap[root], heap[index] = heap[index], heap[root] + self._shift_up(heap, root, length) + + def heapify(self, array: list[Any]) -> None: + for item in array: + self.heappush(item) + + def heappop(self, heap: list[Any] = None) -> Optional[Any]: + if not heap: heap = self.heap + if not heap: return None + deleted_item = heap[0] + heap[0] = heap[-1] + heap.pop() + self._shift_up(heap, 0, len(heap)) + if heap: super()._construct(heap) + else: self._root = None + return deleted_item + + def heappush(self, item: Any, heap: list[Any] = None) -> None: + if heap is None: heap = self.heap + heap.append(item) + for i in range(len(heap) - 1, -1, -1): + self._shift_up(heap, i, len(heap)) + super()._construct(heap) diff --git a/desktop.ini b/desktop.ini deleted file mode 100644 index 3a8f04c..0000000 --- a/desktop.ini +++ /dev/null @@ -1,2 +0,0 @@ -[.ShellClassInfo] -IconResource=D:\Programs\Python\datastax\assets\datastax.ico,0 diff --git a/setup.py b/setup.py index b20d895..4bd34c6 100644 --- a/setup.py +++ b/setup.py @@ -1,22 +1,48 @@ from setuptools import setup -setup(name='datastax', - version='0.0.2', - packages=['datastax', 'datastax/linkedlists', 'datastax/trees'], - license='MIT', - description='A python library to handle dataStructures', - long_description='This library which supports ADTs like Linkedlists and Trees and its types. This instant ' - 'library is solely written from scratch and requires no additional libraries to be installed. ' - 'It solves the purpose of writing programs for complex data structures from scratch, ' - 'visualizing ADTs and simplify writing its inner architectures', - url='https://github.com/warmachine028/datastax', - author='Pritam K', - author_email='pritamkundu771@gmail.com', - zip_safe=False, - classifiers=[ - "Programming Language :: Python :: 3.10", - "License :: OSI Approved :: MIT License", - "Operating System :: OS Independent", - "Topic :: Software Development :: Documentation", - "Topic :: Software Development :: Libraries :: Python Modules" - ]) +setup( + name='datastax', + version='0.0.3', + packages=[ + 'datastax', + 'datastax/arrays', + 'datastax/linkedlists', + 'datastax/trees', + ], + license='MIT', + description='A python library to handle dataStructures', + long_description='This library which supports ADTs like Linkedlists and Trees and its types. This instant ' + 'library is solely written from scratch and requires no additional libraries to be installed. ' + 'It solves the purpose of writing programs for complex data structures from scratch, ' + 'visualizing ADTs and simplify writing its inner architectures. This Module Supports the ' + 'following dataStructures:\n' + '1. Arrays:\n' + ' a. Queue\n' + ' b. Stack\n\n' + '2. LinkedLists:\n' + ' a. Singly Linked List\n' + ' b. Doubly Linked List\n' + ' c. Circular Linked List\n' + ' d. Doubly Circular List\n' + ' e. Queue Representation\n\n' + '3. Trees:\n' + ' a. Binary Tree\n' + ' b. Binary Search Tree\n' + ' c. AVL Tree\n' + ' d. Min Heap Tree\n' + ' e. Max Heap Tree', + url='https://github.com/warmachine028/datastax', + author='Pritam K', + author_email='pritamkundu771@gmail.com', + zip_safe=False, + classifiers=[ + "Programming Language :: Python :: 3.7", + "Programming Language :: Python :: 3.8", + "Programming Language :: Python :: 3.9", + "Programming Language :: Python :: 3.10", + "License :: OSI Approved :: MIT License", + "Operating System :: OS Independent", + "Topic :: Software Development :: Documentation", + "Topic :: Software Development :: Libraries :: Python Modules" + ] +) diff --git a/tests/arrays_tests/__init__.py b/tests/arrays_tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/arrays_tests/queue_test.py b/tests/arrays_tests/queue_test.py new file mode 100644 index 0000000..af5cc20 --- /dev/null +++ b/tests/arrays_tests/queue_test.py @@ -0,0 +1,54 @@ +from datastax.arrays import Queue +from datastax.linkedlists import LinkedList + +# +q = Queue(2) # Fixed Size Queue +q.dequeue() # deletion from empty queue +print(q) +q.enqueue(50) # inserting in empty queue +print(q) +q.dequeue() +q.enqueue(40) # inserting in queue with garbage values +print(q) +q.enqueue(60) # inserting in Queue full with garbage values +q.dequeue() # deleting from queue with all garbage values +print(q) +q.enqueue(10) # inserting in queue with all garbage values +print(q) +print("##########################################") +q = Queue() # Unlimited Size Queue +q.dequeue() # deletion from empty queue +print(q) +q.enqueue(50) # inserting in empty queue +print(q) +q.dequeue() +q.enqueue(40) # inserting in queue with some garbage values +q.dequeue() +print(q) +q.enqueue(60) # inserting in Queue full with all garbage values +print(q) +q.dequeue() +q.dequeue() # deleting from queue with all garbage values +print(q) +q.enqueue(10) # inserting in queue with all garbage values +print(q) + + +# inserting miscellaneos items in queue +def insert(queue: Queue, item) -> None: + queue.enqueue(item) + print(queue) + + +q = Queue() +insert(q, 1000) # integer +insert(q, {1, 2, 3, 4, 5, 6, 7}) # set +insert(q, [190, 200, 300]) # list +insert(q, "STRING") # string +insert(q, "A") # character +insert(q, {1: 2, 2: 3, 3: 4}) # dictionary +insert(q, LinkedList([1, 2]).head) # inserting a singly node +insert(q, LinkedList([1, 2])) # inserting singly linked list +q.dequeue() +print(q) +print(q.array_repr) diff --git a/tests/arrays_tests/stack_test.py b/tests/arrays_tests/stack_test.py new file mode 100644 index 0000000..19a3fa1 --- /dev/null +++ b/tests/arrays_tests/stack_test.py @@ -0,0 +1,18 @@ +from datastax.arrays import Stack + +s = Stack(2) # Fixed size stack +print(s) # printing empty stack +s.pop() # deleting from empty stack +for i in range(2): s.push(10) +print(s) +s.push(1) # inserting inside full stack +s.pop() # deleting from full stack +s = Stack() +for i in range(10): + print(f"Stack is {len(s.array) / s.capacity:.2%} full") + print(s) + s.push(i) +print(s) +for i in range(19): + s.pop() + print("AFTER POPPING\n", s, sep='') diff --git a/tests/linkedlists_tests/linked_list_test.py b/tests/linkedlists_tests/linked_list_test.py index 4d1b6ad..ccecab3 100644 --- a/tests/linkedlists_tests/linked_list_test.py +++ b/tests/linkedlists_tests/linked_list_test.py @@ -24,3 +24,19 @@ def test(array: list[Any] = None) -> None: LL.insert(1) # Insertion at the front LL.append(2) # Insertion at the back print(LL) + +LL = LinkedList() +print(LL) +LL.insert(10) +LL.insert(11) +LL.append(90) +LL.insert(199) +LL.append(109) +print(LL) + +D = LinkedList([*range(5)]) +D.insert(10) +D.insert(20) +D.append(199) +print(D) +# print(D.__str__(True)) diff --git a/tests/linkedlists_tests/queue_test.py b/tests/linkedlists_tests/queue_test.py new file mode 100644 index 0000000..155bbed --- /dev/null +++ b/tests/linkedlists_tests/queue_test.py @@ -0,0 +1,64 @@ +from datastax.linkedlists import Queue, LinkedList + +# +q = Queue(None, 2) # Fixed Size Queue +q.dequeue() # deletion from empty queue +print(q) +q.enqueue(50) # inserting in empty queue +print(q) +q.dequeue() +q.enqueue(40) # inserting in queue with garbage values +print(q) +q.enqueue(60) # inserting in Queue full with garbage values +q.dequeue() # deleting from queue with all garbage values +print("HELLOOO") +print(q) +print("Did not print ?") +q.enqueue(10) # inserting in queue with all garbage values +print(q) +print("##########################################") +q = Queue() # Unlimited Size Queue +q.dequeue() # deletion from empty queue +print(q) +q.enqueue(50) # inserting in empty queue +print(q) +q.dequeue() +q.enqueue(40) # inserting in queue with some garbage values +q.dequeue() +print(q) +q.enqueue(60) # inserting in Queue full with all garbage values +print(q) +q.dequeue() +q.dequeue() # deleting from queue with all garbage values +print(q) +q.enqueue(10) # inserting in queue with all garbage values +print(q) + + +# +# +# inserting miscellaneous items in queue +def insert(queue: Queue, item) -> None: + queue.enqueue(item) + print(queue) + + +q = Queue() +insert(q, {1: 2, 2: 3, 3: 4}) # integer +insert(q, {1, 2, 3, 4, 5, 6, 7}) # set +insert(q, [190, 200, 300]) # list +insert(q, "STRING") # string +insert(q, "A") # character +insert(q, 1000) # dictionary +insert(q, LinkedList([1, 2]).head) # inserting a singly node +insert(q, LinkedList([1, 2])) # inserting singly linked list +q.dequeue() +q.dequeue() +# q.insert(10) +print(q) + +# x = Queue([1, 2, 3, 4, 5, 6, 7], -2) +# x.enqueue(10) +# print(x) +# print("here") +# print(q.array_repr) diff --git a/tests/trees_tests/max_heap_tree_test.py b/tests/trees_tests/max_heap_tree_test.py new file mode 100644 index 0000000..17c4465 --- /dev/null +++ b/tests/trees_tests/max_heap_tree_test.py @@ -0,0 +1,19 @@ +from datastax.trees import MaxHeapTree + +MHT = MaxHeapTree() +print(MHT) +MHT.heappush(30) +print(MHT) +x = MHT.heappop() +print(x) +print("HEAP MUST BE EMPTY") +print(MHT) +MHT.heapify([*range(9, -1, -1)][::-1]) +print(MHT.preorder_print()) +print(MHT.array_repr()) +items = [1, 3, 4, 6, 3, 8, 9, 12, 14, 2] +for item in items: + MHT.heappush(item) + print(MHT) + print(MHT.array_repr()) + print(f"Inserted {item}") diff --git a/tests/trees_tests/min_heap_tree_test.py b/tests/trees_tests/min_heap_tree_test.py new file mode 100644 index 0000000..a8beb92 --- /dev/null +++ b/tests/trees_tests/min_heap_tree_test.py @@ -0,0 +1,24 @@ +from datastax.trees import MinHeapTree + +MiHT = MinHeapTree() +print(MiHT) +MiHT.heappush(30) +print(MiHT) +x = MiHT.heappop() +print(x) +print("HEAP MUST BE EMPTY") +print(MiHT) +MiHT.heapify([*range(9, -1, -1)][::-1]) +print(MiHT.preorder_print()) +print(MiHT.array_repr()) +items = [1, 3, 4, 6, 3, 8, 9, 12, 14, 2] +for item in items: + MiHT.heappush(item) + print(MiHT) + print(MiHT.array_repr()) + print(f"Inserted {item}") + +print('############') +print(MiHT) +x = [MiHT.heappop() for i in range(len(MiHT.array_repr()))] +print(x)