Tuesday, November 25, 2025

Python library for all standard data structures.

 pygorithm-module-in-python/


# Help on package pygorithm.data_structures
help(data_structures)
Output:
NAME
    pygorithm.data_structures - Collection of data structure examples

PACKAGE CONTENTS
    graph
    heap
    linked_list
    quadtree
    queue
    stack
    tree
    trie
#Example 1:
# import the required data structure
from pygorithm.data_structures import stack


# create a stack with default stack size 10
myStack = stack.Stack()

# push elements into the stack
myStack.push(2)
myStack.push(5)
myStack.push(9)
myStack.push(10)

# print the contents of stack
print(myStack)

# pop elements from stack
myStack.pop()
print(myStack)

# peek element in stack
print(myStack.peek())

# size of stack
print(myStack.size())
Output:
2 5 9 10
2 5 9
9
3
#Example 2:
# to get code for BinarySearchTree
BStree = tree.BinarySearchTree.get_code()
print(BStree)
Output:
class BinarySearchTree(object):
   
    def __init__(self):
        self.root = None

    def insert(self, data):
        """
        inserts a node in the tree
        """
        if self.root:
            return self.root.insert(data)
        else:
            self.root = BSTNode(data)
            return True

    def delete(self, data):
        """
        deletes the node with the specified data from the tree
        """
        if self.root is not None:
            return self.root.delete(data)

    def find(self, data):
        if self.root:
            return self.root.find(data)
        else:
            return False

    def preorder(self):
        """
        finding the preorder of the tree
        """
        if self.root is not None:
            return self.root.preorder(self.root)

    def inorder(self):
        """
        finding the inorder of the tree
        """
        if self.root is not None:
            return self.root.inorder(self.root)

    def postorder(self):
        """
        finding the postorder of the tree
        """
        if self.root is not None:
            return self.root.postorder(self.root)
    
    @staticmethod
    def get_code():
        """
        returns the code of the current class
        """
        return inspect.getsource(BinarySearchTree)
Example 3:
To get complexities for the following scripts:
# create a stack with default stack size 10
Bsort = sorting.bubble_sort.time_complexities()
Output:
Best Case: O(n), Average Case: O(n ^ 2), Worst Case: O(n ^ 2).

For Improved Bubble Sort:
Best Case: O(n); Average Case: O(n * (n - 1) / 4); Worst Case: O(n ^ 2)
Source code of this package: https://github.com/OmkarPathak/pygorithm

No comments:

Post a Comment

Python library for all standard data structures.

  pygorithm-module-in-python/ # Help on package pygorithm.data_structures help ( data_structures ) Output: NAME pygorithm.data_structur...