I tried to make a dynamic tree in python. It is simple and i have gotten it to work so far. I created a function in main that generates some example data for the tree and then the main goal was to create a member function that uses recursion to print all the data (display_data()). The only issue with this that i have is the recursion depth problem, as well as the speed. Essentially the recursion and loop pattern builds up quite a bit on the overall run time. Also note this is a side project, I need to understand dynamic trees for a tic-tac-toe AI I am attempting(failing) to write.
class Tree(): """Implement a dynamic tree 1 / | \ / | \ 2 3 4 / \ / | \ 5 6 7 8 9 / / | \ 10 11 12 13 """ def __init__(self): self.children =  self.data =  def create_children(self, amount): for i in range(amount): self.children.append(Tree()) def create_data(self, data): for datum in data: self.data.append(datum) def display_data(self): print(self.data, end = ' ') for child in self.children: child.display_data()
from tree import Tree def example_data(root): """ ['Child', 1] ['nChild', 1] ['nChild', 2] ['Child', 2] ['nChild', 1] ['nChild', 2] ['Child', 3] ['nChild', 1] ['nChild', 2] """ root.create_data(["Root"]) root.create_children(3) counter = 1 for child in root.children: child.create_data(["Child", counter]) child.create_children(2) counter += 1 ncounter = 1 for nchild in child.children: nchild.create_data(["nChild", ncounter]) ncounter += 1 return root if __name__ == "__main__": root = example_data(Tree()) root.display_data()