In: Computer Science
6. Write a function to count a tree’s height, which is the length of the longest path from the root to a leaf.
Please finish this question in python programming
I hope this is what you want. if you still have any doubt please let me know. I have attached code as well as screen shot. I have created 2 version of this program. both are working perfectly. 1st is for binary tree while 2nd work with any tree.
Version1-------------
class tree:
    def __init__(self,data): # constructor to create
new node
        self.left=None
        self.right=None
        self.data=data
    def insert(self,data): #inseting a data in
binary search tree
        if self.data:
           
if(data<self.data): # if data is less than root then it is in
left child
               
if self.left is None: #if there is no left child it becomes left
child
                   
self.left=tree(data)
               
else:
                   
self.left.insert(data)
           
elif data>self.data: # if node has a higher value than root then
it is in right child
               
if self.right is None: #if there is no right child it becomes right
child
                   
self.right=tree(data)
               
else:
                   
self.right.insert(data)
                  
def height(tree):   # height function will count the
height of tree
    if tree is None: #if no tree it will return
height=0
        return 0 ;
    else :
        lheight =
height(tree.left) # recursion which will count the height of left
subtree at each node
        rheight =
height(tree.right) # recursion which will count the height of right
subtree at each node
  
        if (lheight >
rheight): # comparing left height and right hight at root node and
adding 1 to the greater height
           
return lheight+1
        else:
           
return rheight+1
y=input("Enter a value of root node for no root press 0 or
negartive value= ") # Enter a value of root Node
if int(y)>0:
    root = tree(int(y)) #passing the root node value
in tree
    x=1# varible for loop continuiety
    while(int(x)>0):   #if xhas a vale
>0 loop continue otherwise loop break
        x=input("Enter a node
value >0 for countinue and <=0 for stop=") # asking user for
input
        if(int(x)>0): #if
input >0 add in binary tree
           
root.insert(int(x)) #insert a value in binary search
tree    
    print("Height of Binary search tree is
"+f'{height(root)}')
else:
    print("Height of Binary search tree is 0 there
is no root")
Version 2------------------------------------------------------------------------------
class tree: # class tree for tree
    def __init__(self,data): # constructor to create
new node in tree
        self.left=None
        self.right=None
        self.data=data
def height(tree):   # height function will count the
height of tree
    if tree is None: #if no tree it will return
height=0
        return 0 ;
    else :
        lheight =
height(tree.left) # recursion which will count the height of left
subtree at each node
        rheight =
height(tree.right) # recursion which will count the height of right
subtree at each node
  
        if (lheight >
rheight): # comparing left height and right hight at root node and
adding 1 to the greater height
           
return lheight+1
        else:
           
return rheight+1
root=tree(50) #creating root node
root.left=tree(40) # creating left child
root.left.left=tree(30)
root.right=tree(60)
root.right.left=tree(59)
root.right.left.left=tree(58)
root.right.left.left.left=tree(57)
root.right.left.left.left.left=tree(56)
height=height(root) # calling heiht function using tree object as
argument
print("The height of the tree is "+f'{height}') # printing height
of tree
-----------------------------------------------------screenshot----------------------------------------------------------------------------------

