The generators module provides functions to create random instances of various data structures. These generators are invaluable for testing, benchmarking, and developing algorithms.
All generators support customizable size and value ranges, making them flexible for different testing scenarios.
def random_array(size: int, min_val: int=0, max_val=100) -> Array: """ Generates a random array of integers. arguments: size -- number of elements in the array min_val -- minimum value of the elements max_val -- maximum value of the elements returns: Array """
def random_dynamicarray(size: int, min_val: int=0, max_val=100) -> DynamicArray: """ Generates a random dynamic array of integers. arguments: size -- number of elements in the array min_val -- minimum value of the elements max_val -- maximum value of the elements returns: DynamicArray """
def random_stack(size: int, min_val: int=0, max_val=100) -> Stack: """ Generates a random stack of integers. arguments: size -- number of elements in the stack min_val -- minimum value of the elements max_val -- maximum value of the elements returns: Stack """
def linear_stack(size: int, min_val: int=0, max_val=100) -> Stack: """ Generates a linear stack of integers. arguments: size -- number of elements in the stack min_val -- minimum value of the elements max_val -- maximum value of the elements returns: Stack """
def random_queue(size: int, min_val: int=0, max_val=100) -> Queue: """ Generates a random queue of integers. arguments: size -- number of elements in the queue min_val -- minimum value of the elements max_val -- maximum value of the elements returns: Queue """
def linear_queue(size: int, min_val: int=0, max_val=100) -> Queue: """ Generates a linear queue of integers. arguments: size -- number of elements in the queue min_val -- minimum value of the elements max_val -- maximum value of the elements returns: Queue """
Generates a random deque with elements added from both ends.
generators.py:169
def random_deque(size: int, min_val: int=0, max_val=100) -> Deque: """ Generates a random deque of integers. arguments: size -- number of elements in the deque min_val -- minimum value of the elements max_val -- maximum value of the elements returns: Deque """
Example:
from dsa.generators import random_deque# Generates deque with random insertions from left and rightdeque = random_deque(20, 0, 100)
def random_linked_list(size: int, min_val: int=0, max_val=100) -> LinkedList: """ Generates a random linked list of integers. arguments: size -- number of elements in the linked list min_val -- minimum value of the elements max_val -- maximum value of the elements returns: LinkedList """
Generates a singly linked list with sequential values.
generators.py:202
def linear_linked_list(size: int, min_val: int=0, max_val=100) -> LinkedList: """ Generates a linear linked list of integers. arguments: size -- number of elements in the linked list min_val -- minimum value of the elements max_val -- maximum value of the elements returns: LinkedList """
def random_doubly_linked_list(size: int, min_val: int=0, max_val=100) -> DoublyLinkedList: """ Generates a random doubly linked list of integers. arguments: size -- number of nodes in the list min_val -- minimum value of the nodes max_val -- maximum value of the nodes returns: DoublyLinkedList """
def random_binary_tree(n: int) -> 'Tree': """Generates a random binary tree. arguments: n -- number of nodes in the tree returns: Tree """
Parameters:
n (int): Number of nodes in the tree
Returns:
Tree: Random binary tree with n nodes
Example:
from dsa.generators import random_binary_treefrom dsa.draw import TreeDraw# Generate and visualize a random treetree = random_binary_tree(15)td = TreeDraw(tree)td.draw()
def random_heap(n: int) -> Heap: """Generates a random heap. arguments: n -- number of nodes in the heap returns: Heap """
Parameters:
n (int): Number of elements in the heap
Returns:
Heap: Random min-heap with n elements
Example:
from dsa.generators import random_heapfrom dsa.draw import HeapDraw# Generate and visualize a random heapheap = random_heap(20)hd = HeapDraw(heap)hd.draw()
directed (bool): Whether the graph is directed (default: False)
Returns:
AdjacencyListWeightedGraph: Random weighted graph with edge weights from 1-10
Example:
from dsa.generators import random_adjacency_list_weighted_graphfrom dsa.draw import GraphDraw# Generate a random weighted graph with 10 vertices# 30% density (dense graph)graph = random_adjacency_list_weighted_graph(10, density=0.3, directed=False)# Visualize the graphgd = GraphDraw(graph)gd.draw()
Vertex labels are automatically generated as ‘A’, ‘B’, ‘C’, etc. The density parameter controls how many edges are created (0.1 = 10% of possible edges).
# Fast generation for unit testsfrom dsa.generators import random_arraydef test_algorithm(): # Small test cases for _ in range(100): data = random_array(10, 0, 100) # Test your algorithm