Quick actions

cmd+k|ctrl+k

Navigation

Languages

Height of a tree

Snippet info

Language

Python

Visibility

public

Author

arthurpc02

Created

2024-10-06T18:08:43.239369Z

Updated

2024-10-06T18:39:36.950602Z

class Node:
    def __init__(self, info): 
        self.info = info  
        self.left = None  
        self.right = None 
        self.level = None 

    def __str__(self):
        return str(self.info) 

class BinarySearchTree:
    def __init__(self): 
        self.root = None

    def create(self, val):  
        if self.root == None:
            self.root = Node(val)
        else:
            current = self.root
         
            while True:
                if val < current.info:
                    if current.left:
                        current = current.left
                    else:
                        current.left = Node(val)
                        break
                elif val > current.info:
                    if current.right:
                        current = current.right
                    else:
                        current.right = Node(val)
                        break
                else:
                    break

# Enter your code here. Read input from STDIN. Print output to STDOUT

'''
class Node:
      def __init__(self,info): 
          self.info = info  
          self.left = None  
          self.right = None 
           

       // this is a node of the tree , which contains info as data, left , right
'''
def height(root):
    nodeHeight = 0
    maxHeight = 0
    prevNode = None
    
    for i in range(30):
        # print("new run")
        nodeHeight = 0
        currentNode = root
        # print(f"root left: {root.left}")
        # print(f"root right: {root.right}")
        
        for j in range(20):
            # print(f"current left: {currentNode.left}")
            # print(f"current right: {currentNode.right}")
            if currentNode.left != None:
                prevNode = currentNode
                currentNode = currentNode.left
                # print(f"current node became: {currentNode} and his left is {currentNode.left}")
            elif currentNode.right != None:
                # print("no left, only right")
                prevNode = currentNode
                currentNode = currentNode.right
            else:
                # print("node end")
                break
                
            nodeHeight+=1
            maxHeight = max(nodeHeight, maxHeight)
            # print(f"node height: {nodeHeight} and max height")
                
        # print("remove this last node")
        if currentNode != root:
            if prevNode.left == currentNode:
                prevNode.left = None
            else:
                prevNode.right = None
        else:
            # print("ran through the whole tree")
            return maxHeight
            break
        
    return maxHeight



tree = BinarySearchTree()
t = int(input())

arr = list(map(int, input().split()))

for i in range(t):
    tree.create(arr[i])

print(height(tree.root))

""" inputs: 
    7
    3 5 2 1 4 6 7
    
    1
    15
    
    5
    3 1 7 5 4
    
"""
INFO