I want to implement a Binary Tree in Python. I sumit my code. What I would like to do is to set the height of the Binary Tree with the variable L. But, when I implement the code, it seems that the code has created a Binary Tree that is greater than I expected. I arrive to this conclusion because when I set the height as 1 and I do print(node.right.right.right), I still get 1.
class Tree:
def __init__(self,x,left=None,right=None):
self.x=x
self.left=left
self.right=right
def one_tree(self,node):
node=Tree(1)
node.right=Tree(1)
node.left=Tree(1)
return node
node=Tree(1)
node=node.one_tree(node)
L=1
while L>0:
node=node.one_tree(node)
node.left=node
node.right=node
L=L-1
print(node.right.right.right.right)