I wrote code to show the structure of a Fibonacci sequence pattern in a graphical way. First I structure a tree given a variable the user inputs for how many iterations of the sequence they want to print. Then, it converts the numbers into the Fibonacci sequence.
My concern is that this doesn't seem very efficient, first I build a tree, then go through each node again and do the entire Fibonacci calculation on it. Instead, I was hoping for a suggestion to change the code so that it could generate the nodes and the Fibonacci number at the same time. Maybe, instead of two recursive functions, there is just one that calls itself:
(num-1) is the left node and
(num-2) is on the right.
from binarytree import Node # Generates a binarytree with the correct *iterations* numbers. def makeTree(node): if node.value > 1: node.left = makeTree(Node(node.value - 1)) node.right = makeTree(Node(node.value - 2)) return node # Converts the iterated binary tree numbers into fibonacci numbers def fibonacci(a, b , num): if num > 0: a += b return fibonacci(b, a, num - 1) return a a = int(input("Enter number: ")) fibTree = makeTree(Node(a)) for x in fibTree: x.value = fibonacci(0, 1, x.value) print(fibTree)