csnotes/370/samples/graph.py
Medium Fries ea2aa8d2a6 yote
2019-03-15 01:26:26 -07:00

54 lines
1.0 KiB
Python
Executable File

#!/usr/bin/python3
import random
class Node:
def __init__(self, data:int):
self.data = data
# empty list o rama
self.refs = []
def __str__(self):
return f'{self.data}:{self.refs}'
def new_node(data):
"""
Provides a new node for a ref list or a basic s
"""
return Node(data)
def add_node(base, data):
"""
Creates a new node for the base node to hook onto
"""
base.refs.append(new_node(data))
def show_children(node):
"""
LMAO @ your life
"""
print(node.refs)
def populate(node):
"""
Populate our node with some data
"""
for i in range(1,random.randint(2,10)):
new_tmp = new_node(random.randint(0,100))
node.refs.append(new_tmp)
def child_count(node):
print(f'{len(node.refs)}')
if __name__ == "__main__":
root = Node(0)
for i in 'ABCDE':
add_node(root, i)
print(f'Children count of root: {len(root.refs)}')
for i in root.refs:
populate(i)
child_count(i)
print('Root node:')
show_children(root)