dsa.generators API documentation

# dsa.generators

View Source

      1from dsa.array import Array, DynamicArray
      2from dsa.stack import Stack, DynamicStack
      3from dsa.queue import Queue, DynamicQueue
      4from dsa.deque import Deque
      5
      6from dsa.singlylinkedlist import LinkedList
      7from dsa.doublylinkedlist import DoublyLinkedList
      8
      9from dsa.tree import Tree, TreeNode
     10from dsa.heap import Heap
     11from dsa.trie import Trie
     12
     13from dsa.graph import AdjacencyListGraph, AdjacencyListWeightedGraph
     14from dsa.graph import AdjacencyMatrixGraph, AdjacencyMatrixWeightedGraph
     15
     16import random
     17
     18def random_array(size: int, min_val: int=0, max_val=100) -> Array:
     19    """
     20    Generates a random array of integers.
     21    arguments:
     22        size -- number of elements in the array
     23        min_val -- minimum value of the elements
     24        max_val -- maximum value of the elements
     25    returns:
     26        Array
     27    """
     28    array = Array(capacity=size)
     29    for _ in range(size):
     30        array.append(random.randint(min_val, max_val))
     31    return array
     32
     33def random_dynamicarray(size: int, min_val: int=0, max_val=100) -> DynamicArray:
     34    """
     35    Generates a random dynamic array of integers.
     36    arguments:
     37        size -- number of elements in the array
     38        min_val -- minimum value of the elements
     39        max_val -- maximum value of the elements
     40    returns:
     41        DynamicArray
     42    """
     43    array = DynamicArray()
     44    for _ in range(size):
     45        array.append(random.randint(min_val, max_val))
     46    return array
     47
     48def random_stack(size: int, min_val: int=0, max_val=100) -> Stack:
     49    """
     50    Generates a random stack of integers.
     51    arguments:
     52        size -- number of elements in the stack
     53        min_val -- minimum value of the elements
     54        max_val -- maximum value of the elements
     55    returns:
     56        Stack
     57    """
     58    stack = Stack(capacity=size)
     59    for _ in range(size):
     60        stack.push(random.randint(min_val, max_val))
     61    return stack
     62
     63def linear_stack(size: int, min_val: int=0, max_val=100) -> Stack:
     64    """
     65    Generates a linear stack of integers.
     66    arguments:
     67        size -- number of elements in the stack
     68        min_val -- minimum value of the elements
     69        max_val -- maximum value of the elements
     70    returns:
     71        Stack
     72    """
     73    stack = Stack(capacity=size)
     74    for i in range(size):
     75        stack.push(i + min_val)
     76    return stack
     77
     78def random_dynamic_stack(size: int, min_val: int=0, max_val=100) -> DynamicStack:
     79    """
     80    Generates a random dynamic stack of integers.
     81    arguments:
     82        size -- number of elements in the stack
     83        min_val -- minimum value of the elements
     84        max_val -- maximum value of the elements
     85    returns:
     86        DynamicStack
     87    """
     88    stack = DynamicStack()
     89    for _ in range(size):
     90        stack.push(random.randint(min_val, max_val))
     91    return stack
     92
     93def linear_dynamic_stack(size: int, min_val: int=0, max_val=100) -> DynamicStack:
     94    """
     95    Generates a linear dynamic stack of integers.
     96    arguments:
     97        size -- number of elements in the stack
     98        min_val -- minimum value of the elements
     99        max_val -- maximum value of the elements
    100    returns:
    101        DynamicStack
    102    """
    103    stack = DynamicStack()
    104    for i in range(size):
    105        stack.push(i + min_val)
    106    return stack
    107
    108def random_queue(size: int, min_val: int=0, max_val=100) -> Queue:
    109    """
    110    Generates a random queue of integers.
    111    arguments:
    112        size -- number of elements in the queue
    113        min_val -- minimum value of the elements
    114        max_val -- maximum value of the elements
    115    returns:
    116        Queue
    117    """
    118    queue = Queue(capacity=size)
    119    for _ in range(size):
    120        queue.enqueue(random.randint(min_val, max_val))
    121    return queue
    122
    123
    124def linear_queue(size: int, min_val: int=0, max_val=100) -> Queue:
    125    """
    126    Generates a linear queue of integers.
    127    arguments:
    128        size -- number of elements in the queue
    129        min_val -- minimum value of the elements
    130        max_val -- maximum value of the elements
    131    returns:
    132        Queue
    133    """
    134    queue = Queue(capacity=size)
    135    for i in range(size):
    136        queue.enqueue(i + min_val)
    137    return queue
    138
    139def random_dynamic_queue(size: int, min_val: int=0, max_val=100) -> DynamicQueue:
    140    """
    141    Generates a random dynamic queue of integers.
    142    arguments:
    143        size -- number of elements in the queue
    144        min_val -- minimum value of the elements
    145        max_val -- maximum value of the elements
    146    returns:
    147        DynamicQueue
    148    """
    149    queue = DynamicQueue()
    150    for _ in range(size):
    151        queue.enqueue(random.randint(min_val, max_val))
    152    return queue
    153
    154def linear_dynamic_queue(size: int, min_val: int=0, max_val=100) -> DynamicQueue:
    155    """
    156    Generates a linear dynamic queue of integers.
    157    arguments:
    158        size -- number of elements in the queue
    159        min_val -- minimum value of the elements
    160        max_val -- maximum value of the elements
    161    returns:
    162        DynamicQueue
    163    """
    164    queue = DynamicQueue()
    165    for i in range(size):
    166        queue.enqueue(i + min_val)
    167    return queue
    168
    169def random_deque(size: int, min_val: int=0, max_val=100) -> Deque:
    170    """
    171    Generates a random deque of integers.
    172    arguments:
    173        size -- number of elements in the deque
    174        min_val -- minimum value of the elements
    175        max_val -- maximum value of the elements
    176    returns:
    177        Deque
    178    """
    179    deque = Deque(capacity=size)
    180    for _ in range(size):
    181        if random.choice([True, False]):
    182            deque.append_left(random.randint(min_val, max_val))
    183        else:
    184            deque.append_right(random.randint(min_val, max_val))
    185    return deque
    186
    187def random_linked_list(size: int, min_val: int=0, max_val=100) -> LinkedList:
    188    """
    189    Generates a random linked list of integers.
    190    arguments:
    191        size -- number of elements in the linked list
    192        min_val -- minimum value of the elements
    193        max_val -- maximum value of the elements
    194    returns:
    195        LinkedList
    196    """
    197    linked_list = LinkedList()
    198    for _ in range(size):
    199        linked_list.append(random.randint(min_val, max_val))
    200    return linked_list
    201
    202def linear_linked_list(size: int, min_val: int=0, max_val=100) -> LinkedList:
    203    """
    204    Generates a linear linked list of integers.
    205    arguments:
    206        size -- number of elements in the linked list
    207        min_val -- minimum value of the elements
    208        max_val -- maximum value of the elements
    209    returns:
    210        LinkedList
    211    """
    212    linked_list = LinkedList()
    213    for i in range(size):
    214        linked_list.append(i + min_val)
    215    return linked_list
    216
    217
    218def random_doubly_linked_list(size: int, min_val: int=0, max_val=100) -> DoublyLinkedList:
    219    """
    220    Generates a random doubly linked list of integers.
    221    arguments:
    222        size -- number of nodes in the list
    223        min_val -- minimum value of the nodes
    224        max_val -- maximum value of the nodes
    225    returns:
    226        DoublyLinkedList
    227    """
    228    doubly_linked_list = DoublyLinkedList()
    229    for _ in range(size):
    230        doubly_linked_list.append(random.randint(min_val, max_val))
    231    return doubly_linked_list
    232
    233def linear_doubly_linked_list(size: int, min_val: int=0, max_val=100) -> DoublyLinkedList:
    234    """
    235    Generates a linear doubly linked list of integers.
    236    arguments:
    237        size -- number of nodes in the list
    238        min_val -- minimum value of the nodes
    239        max_val -- maximum value of the nodes
    240    returns:
    241        DoublyLinkedList
    242    """
    243    doubly_linked_list = DoublyLinkedList()
    244    for i in range(size):
    245        doubly_linked_list.append(i + min_val)
    246    return doubly_linked_list
    247
    248def random_binary_tree(n: int) -> 'Tree':
    249    """Generates a random binary tree.
    250    arguments:
    251        n -- number of nodes in the tree
    252    returns:
    253        Tree
    254    """
    255    tree = Tree()
    256
    257    tree.root = random_binary_tree_node(n)
    258
    259    return tree
    260
    261def random_binary_tree_node(n: int) -> TreeNode:
    262    """
    263    Generates a random binary tree with exactly n nodes.
    264    arguments:
    265        n -- number of nodes in the tree
    266    returns:
    267        TreeNode
    268    """
    269    if n == 0:
    270        return None
    271    root = TreeNode(random.randint(0, 100))
    272    n -= 1
    273    # Randomly decide how many nodes go to the left (0 to n)
    274    left_count = random.randint(0, n)
    275    right_count = n - left_count
    276    root.left = random_binary_tree_node(left_count)
    277    root.right = random_binary_tree_node(right_count)
    278    return root
    279
    280def random_heap(n: int) -> Heap:
    281    """Generates a random heap.
    282    arguments:
    283        n -- number of nodes in the heap
    284    returns:
    285        Heap
    286    """
    287    from dsa.heap import Heap
    288    heap = Heap()
    289    for _ in range(n):
    290        heap.insert(random.randint(0, 100))
    291    return heap
    292
    293def random_trie(n: int) -> Trie:
    294    """Generates a random trie.
    295
    296    arguments:
    297        n -- number of words in the trie
    298    returns:
    299        Trie
    300    """
    301    from dsa.trie import Trie
    302    trie = Trie()
    303    for _ in range(n):
    304        word_length = random.randint(1, 10)
    305        word = ''.join(random.choice('abcdefghijklmnopqrstuvwxyz') for _ in range(word_length))
    306        trie.insert(word)
    307    return trie
    308
    309# Generates a random graphs
    310# options: directed, undirected, weighted, unweighted
    311
    312def random_adjacency_matrix_graph(n, density=0.1, directed=False) -> AdjacencyMatrixGraph:
    313    # create a list of strings starting from "A" to "Z", then "AA", "AB", etc.
    314    labels = [chr(i) for i in range(65, 65 + n)]
    315    graph = AdjacencyMatrixGraph(labels=labels)
    316    for i in range(n):
    317        for j in range(i + 1, n):
    318            if random.random() < density:
    319                graph.add_edge(labels[i], labels[j], directed=directed)
    320    return graph
    321
    322def random_adjacency_matrix_weighted_graph(n, density=0.1, directed=False) -> AdjacencyMatrixWeightedGraph:
    323    # create a list of strings starting from "A" to "Z", then "AA", "AB", etc.
    324    labels = [chr(i) for i in range(65, 65 + n)]
    325    graph = AdjacencyMatrixWeightedGraph(labels=labels)
    326    for i in range(n):
    327        for j in range(i + 1, n):
    328            if random.random() < density:
    329                weight = random.randint(1, 10)
    330                graph.add_edge(labels[i], labels[j], weight, directed=directed)
    331    return graph
    332
    333def random_adjacency_list_graph(n, density=0.1, directed=False) -> AdjacencyListGraph:
    334    # create a list of strings starting from "A" to "Z", then "AA", "AB", etc.
    335    labels = [chr(i) for i in range(65, 65 + n)]
    336    graph = AdjacencyListGraph()
    337    for i in range(n):
    338        for j in range(i + 1, n):
    339            if random.random() < density:
    340                graph.add_edge(labels[i], labels[j], directed=directed)
    341    return graph
    342
    343def random_adjacency_list_weighted_graph(n, density=0.1, directed=False) -> AdjacencyListWeightedGraph:
    344    # create a list of strings starting from "A" to "Z", then "AA", "AB", etc.
    345    labels = [chr(i) for i in range(65, 65 + n)]
    346    graph = AdjacencyListWeightedGraph()
    347    for i in range(n):
    348        for j in range(i + 1, n):
    349            if random.random() < density:
    350                weight = random.randint(1, 10)
    351                graph.add_edge(labels[i], labels[j], weight, directed=directed)
    352    return graph

def random_array(size: int, min_val: int = 0, max_val=100) -> dsa.array.Array: View Source

    19def random_array(size: int, min_val: int=0, max_val=100) -> Array:
    20    """
    21    Generates a random array of integers.
    22    arguments:
    23        size -- number of elements in the array
    24        min_val -- minimum value of the elements
    25        max_val -- maximum value of the elements
    26    returns:
    27        Array
    28    """
    29    array = Array(capacity=size)
    30    for _ in range(size):
    31        array.append(random.randint(min_val, max_val))
    32    return 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) -> dsa.array.DynamicArray: View Source

    34def random_dynamicarray(size: int, min_val: int=0, max_val=100) -> DynamicArray:
    35    """
    36    Generates a random dynamic array of integers.
    37    arguments:
    38        size -- number of elements in the array
    39        min_val -- minimum value of the elements
    40        max_val -- maximum value of the elements
    41    returns:
    42        DynamicArray
    43    """
    44    array = DynamicArray()
    45    for _ in range(size):
    46        array.append(random.randint(min_val, max_val))
    47    return array

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) -> dsa.stack.Stack: View Source

    49def random_stack(size: int, min_val: int=0, max_val=100) -> Stack:
    50    """
    51    Generates a random stack of integers.
    52    arguments:
    53        size -- number of elements in the stack
    54        min_val -- minimum value of the elements
    55        max_val -- maximum value of the elements
    56    returns:
    57        Stack
    58    """
    59    stack = Stack(capacity=size)
    60    for _ in range(size):
    61        stack.push(random.randint(min_val, max_val))
    62    return 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) -> dsa.stack.Stack: View Source

    64def linear_stack(size: int, min_val: int=0, max_val=100) -> Stack:
    65    """
    66    Generates a linear stack of integers.
    67    arguments:
    68        size -- number of elements in the stack
    69        min_val -- minimum value of the elements
    70        max_val -- maximum value of the elements
    71    returns:
    72        Stack
    73    """
    74    stack = Stack(capacity=size)
    75    for i in range(size):
    76        stack.push(i + min_val)
    77    return 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_dynamic_stack(size: int, min_val: int = 0, max_val=100) -> dsa.stack.DynamicStack: View Source

    79def random_dynamic_stack(size: int, min_val: int=0, max_val=100) -> DynamicStack:
    80    """
    81    Generates a random dynamic stack of integers.
    82    arguments:
    83        size -- number of elements in the stack
    84        min_val -- minimum value of the elements
    85        max_val -- maximum value of the elements
    86    returns:
    87        DynamicStack
    88    """
    89    stack = DynamicStack()
    90    for _ in range(size):
    91        stack.push(random.randint(min_val, max_val))
    92    return stack

Generates a random dynamic 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: DynamicStack

def linear_dynamic_stack(size: int, min_val: int = 0, max_val=100) -> dsa.stack.DynamicStack: View Source

     94def linear_dynamic_stack(size: int, min_val: int=0, max_val=100) -> DynamicStack:
     95    """
     96    Generates a linear dynamic stack of integers.
     97    arguments:
     98        size -- number of elements in the stack
     99        min_val -- minimum value of the elements
    100        max_val -- maximum value of the elements
    101    returns:
    102        DynamicStack
    103    """
    104    stack = DynamicStack()
    105    for i in range(size):
    106        stack.push(i + min_val)
    107    return stack

Generates a linear dynamic 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: DynamicStack

def random_queue(size: int, min_val: int = 0, max_val=100) -> dsa.queue.Queue: View Source

    109def random_queue(size: int, min_val: int=0, max_val=100) -> Queue:
    110    """
    111    Generates a random queue of integers.
    112    arguments:
    113        size -- number of elements in the queue
    114        min_val -- minimum value of the elements
    115        max_val -- maximum value of the elements
    116    returns:
    117        Queue
    118    """
    119    queue = Queue(capacity=size)
    120    for _ in range(size):
    121        queue.enqueue(random.randint(min_val, max_val))
    122    return 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) -> dsa.queue.Queue: View Source

    125def linear_queue(size: int, min_val: int=0, max_val=100) -> Queue:
    126    """
    127    Generates a linear queue of integers.
    128    arguments:
    129        size -- number of elements in the queue
    130        min_val -- minimum value of the elements
    131        max_val -- maximum value of the elements
    132    returns:
    133        Queue
    134    """
    135    queue = Queue(capacity=size)
    136    for i in range(size):
    137        queue.enqueue(i + min_val)
    138    return 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

def random_dynamic_queue(size: int, min_val: int = 0, max_val=100) -> dsa.queue.DynamicQueue: View Source

    140def random_dynamic_queue(size: int, min_val: int=0, max_val=100) -> DynamicQueue:
    141    """
    142    Generates a random dynamic queue of integers.
    143    arguments:
    144        size -- number of elements in the queue
    145        min_val -- minimum value of the elements
    146        max_val -- maximum value of the elements
    147    returns:
    148        DynamicQueue
    149    """
    150    queue = DynamicQueue()
    151    for _ in range(size):
    152        queue.enqueue(random.randint(min_val, max_val))
    153    return queue

Generates a random dynamic 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: DynamicQueue

def linear_dynamic_queue(size: int, min_val: int = 0, max_val=100) -> dsa.queue.DynamicQueue: View Source

    155def linear_dynamic_queue(size: int, min_val: int=0, max_val=100) -> DynamicQueue:
    156    """
    157    Generates a linear dynamic queue of integers.
    158    arguments:
    159        size -- number of elements in the queue
    160        min_val -- minimum value of the elements
    161        max_val -- maximum value of the elements
    162    returns:
    163        DynamicQueue
    164    """
    165    queue = DynamicQueue()
    166    for i in range(size):
    167        queue.enqueue(i + min_val)
    168    return queue

Generates a linear dynamic 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: DynamicQueue

def random_deque(size: int, min_val: int = 0, max_val=100) -> dsa.deque.Deque: View Source

    170def random_deque(size: int, min_val: int=0, max_val=100) -> Deque:
    171    """
    172    Generates a random deque of integers.
    173    arguments:
    174        size -- number of elements in the deque
    175        min_val -- minimum value of the elements
    176        max_val -- maximum value of the elements
    177    returns:
    178        Deque
    179    """
    180    deque = Deque(capacity=size)
    181    for _ in range(size):
    182        if random.choice([True, False]):
    183            deque.append_left(random.randint(min_val, max_val))
    184        else:
    185            deque.append_right(random.randint(min_val, max_val))
    186    return 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

def random_linked_list( size: int, min_val: int = 0, max_val=100) -> dsa.singlylinkedlist.LinkedList: View Source

    188def random_linked_list(size: int, min_val: int=0, max_val=100) -> LinkedList:
    189    """
    190    Generates a random linked list of integers.
    191    arguments:
    192        size -- number of elements in the linked list
    193        min_val -- minimum value of the elements
    194        max_val -- maximum value of the elements
    195    returns:
    196        LinkedList
    197    """
    198    linked_list = LinkedList()
    199    for _ in range(size):
    200        linked_list.append(random.randint(min_val, max_val))
    201    return linked_list

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

def linear_linked_list( size: int, min_val: int = 0, max_val=100) -> dsa.singlylinkedlist.LinkedList: View Source

    203def linear_linked_list(size: int, min_val: int=0, max_val=100) -> LinkedList:
    204    """
    205    Generates a linear linked list of integers.
    206    arguments:
    207        size -- number of elements in the linked list
    208        min_val -- minimum value of the elements
    209        max_val -- maximum value of the elements
    210    returns:
    211        LinkedList
    212    """
    213    linked_list = LinkedList()
    214    for i in range(size):
    215        linked_list.append(i + min_val)
    216    return linked_list

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) -> dsa.doublylinkedlist.DoublyLinkedList: View Source

    219def random_doubly_linked_list(size: int, min_val: int=0, max_val=100) -> DoublyLinkedList:
    220    """
    221    Generates a random doubly linked list of integers.
    222    arguments:
    223        size -- number of nodes in the list
    224        min_val -- minimum value of the nodes
    225        max_val -- maximum value of the nodes
    226    returns:
    227        DoublyLinkedList
    228    """
    229    doubly_linked_list = DoublyLinkedList()
    230    for _ in range(size):
    231        doubly_linked_list.append(random.randint(min_val, max_val))
    232    return doubly_linked_list

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 linear_doubly_linked_list( size: int, min_val: int = 0, max_val=100) -> dsa.doublylinkedlist.DoublyLinkedList: View Source

    234def linear_doubly_linked_list(size: int, min_val: int=0, max_val=100) -> DoublyLinkedList:
    235    """
    236    Generates a linear doubly linked list of integers.
    237    arguments:
    238        size -- number of nodes in the list
    239        min_val -- minimum value of the nodes
    240        max_val -- maximum value of the nodes
    241    returns:
    242        DoublyLinkedList
    243    """
    244    doubly_linked_list = DoublyLinkedList()
    245    for i in range(size):
    246        doubly_linked_list.append(i + min_val)
    247    return doubly_linked_list

Generates a linear 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) -> dsa.tree.Tree: View Source

    249def random_binary_tree(n: int) -> 'Tree':
    250    """Generates a random binary tree.
    251    arguments:
    252        n -- number of nodes in the tree
    253    returns:
    254        Tree
    255    """
    256    tree = Tree()
    257
    258    tree.root = random_binary_tree_node(n)
    259
    260    return tree

Generates a random binary tree. arguments: n -- number of nodes in the tree returns: Tree

def random_binary_tree_node(n: int) -> dsa.tree.TreeNode: View Source

    262def random_binary_tree_node(n: int) -> TreeNode:
    263    """
    264    Generates a random binary tree with exactly n nodes.
    265    arguments:
    266        n -- number of nodes in the tree
    267    returns:
    268        TreeNode
    269    """
    270    if n == 0:
    271        return None
    272    root = TreeNode(random.randint(0, 100))
    273    n -= 1
    274    # Randomly decide how many nodes go to the left (0 to n)
    275    left_count = random.randint(0, n)
    276    right_count = n - left_count
    277    root.left = random_binary_tree_node(left_count)
    278    root.right = random_binary_tree_node(right_count)
    279    return root

Generates a random binary tree with exactly n nodes. arguments: n -- number of nodes in the tree returns: TreeNode

def random_heap(n: int) -> dsa.heap.Heap: View Source

    281def random_heap(n: int) -> Heap:
    282    """Generates a random heap.
    283    arguments:
    284        n -- number of nodes in the heap
    285    returns:
    286        Heap
    287    """
    288    from dsa.heap import Heap
    289    heap = Heap()
    290    for _ in range(n):
    291        heap.insert(random.randint(0, 100))
    292    return heap

Generates a random heap. arguments: n -- number of nodes in the heap returns: Heap

def random_trie(n: int) -> dsa.trie.Trie: View Source

    294def random_trie(n: int) -> Trie:
    295    """Generates a random trie.
    296
    297    arguments:
    298        n -- number of words in the trie
    299    returns:
    300        Trie
    301    """
    302    from dsa.trie import Trie
    303    trie = Trie()
    304    for _ in range(n):
    305        word_length = random.randint(1, 10)
    306        word = ''.join(random.choice('abcdefghijklmnopqrstuvwxyz') for _ in range(word_length))
    307        trie.insert(word)
    308    return trie

Generates a random trie.

arguments: n -- number of words in the trie returns: Trie

def random_adjacency_matrix_graph(n, density=0.1, directed=False) -> dsa.graph.AdjacencyMatrixGraph: View Source

    313def random_adjacency_matrix_graph(n, density=0.1, directed=False) -> AdjacencyMatrixGraph:
    314    # create a list of strings starting from "A" to "Z", then "AA", "AB", etc.
    315    labels = [chr(i) for i in range(65, 65 + n)]
    316    graph = AdjacencyMatrixGraph(labels=labels)
    317    for i in range(n):
    318        for j in range(i + 1, n):
    319            if random.random() < density:
    320                graph.add_edge(labels[i], labels[j], directed=directed)
    321    return graph

def random_adjacency_matrix_weighted_graph(n, density=0.1, directed=False) -> dsa.graph.AdjacencyMatrixWeightedGraph: View Source

    323def random_adjacency_matrix_weighted_graph(n, density=0.1, directed=False) -> AdjacencyMatrixWeightedGraph:
    324    # create a list of strings starting from "A" to "Z", then "AA", "AB", etc.
    325    labels = [chr(i) for i in range(65, 65 + n)]
    326    graph = AdjacencyMatrixWeightedGraph(labels=labels)
    327    for i in range(n):
    328        for j in range(i + 1, n):
    329            if random.random() < density:
    330                weight = random.randint(1, 10)
    331                graph.add_edge(labels[i], labels[j], weight, directed=directed)
    332    return graph

def random_adjacency_list_graph(n, density=0.1, directed=False) -> dsa.graph.AdjacencyListGraph: View Source

    334def random_adjacency_list_graph(n, density=0.1, directed=False) -> AdjacencyListGraph:
    335    # create a list of strings starting from "A" to "Z", then "AA", "AB", etc.
    336    labels = [chr(i) for i in range(65, 65 + n)]
    337    graph = AdjacencyListGraph()
    338    for i in range(n):
    339        for j in range(i + 1, n):
    340            if random.random() < density:
    341                graph.add_edge(labels[i], labels[j], directed=directed)
    342    return graph

def random_adjacency_list_weighted_graph(n, density=0.1, directed=False) -> dsa.graph.AdjacencyListWeightedGraph: View Source

    344def random_adjacency_list_weighted_graph(n, density=0.1, directed=False) -> AdjacencyListWeightedGraph:
    345    # create a list of strings starting from "A" to "Z", then "AA", "AB", etc.
    346    labels = [chr(i) for i in range(65, 65 + n)]
    347    graph = AdjacencyListWeightedGraph()
    348    for i in range(n):
    349        for j in range(i + 1, n):
    350            if random.random() < density:
    351                weight = random.randint(1, 10)
    352                graph.add_edge(labels[i], labels[j], weight, directed=directed)
    353    return graph