-
-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathheight_of_BST.py
76 lines (62 loc) · 1.57 KB
/
height_of_BST.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
'''
Aim: To form a Binary Search Tree of the integers entered and display its height.
'''
# initializing the tree
class Node:
def __init__(self,data):
self.right=self.left=None
self.data = data
class Solution:
# function for inserting values according to BST rules
def insert(self,root,data):
if root == None:
return Node(data)
else:
if data <= root.data:
cur = self.insert(root.left,data)
root.left = cur
else:
cur = self.insert(root.right,data)
root.right = cur
return root
# calculating the height of the tree
def getHeight(self,root):
if root == None:
return -1
return 1 + max(self.getHeight(root.left), self.getHeight(root.right))
# getting the input for total number of integers to be entered
T = int(input())
# making an object of the class
myTree = Solution()
root = None
# inserting values
for i in range(T):
data = int(input())
root = myTree.insert(root,data)
# geeting the height calculated
height = myTree.getHeight(root)
# printing the result
print("Height of BST:",height)
'''
COMPLEXITY:
Time Complexity -> O(N)
Space Complexity -> O(N)
Sample Input:
7
3
5
2
1
4
6
7
Sample Output:
Height of BST: 3
Explaination:
The BST looks something like this:
3
2 5
1 4 6
7
So, the height is --> 3.
'''