-
Notifications
You must be signed in to change notification settings - Fork 35
/
binary-search-tree-max-value.py
57 lines (45 loc) · 1.19 KB
/
binary-search-tree-max-value.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
'''
Question: Find max value of Binary Search Tree(BST)
Max value is the right most leaf
'''
class BinarySearchTree:
def __init__(root, data):
root.left = None
root.right = None
root.data = data
def insertNode(root, node):
if root is None:
root = node
else:
if root.data > node.data:
if root.left == None:
root.left = node
else:
insertNode(root.left, node)
else:
if root.right == None:
root.right = node
else:
insertNode(root.right, node)
#Ascending order of elements
def inOrderTraversal(root):
if not root:
return
inOrderTraversal(root.left)
print(root.data,end=" ")
inOrderTraversal(root.right)
def maxValue(node):
if node.right:
return maxValue(node.right)
else:
return node.data
input = [12,5,17,3,7,13,19,1,9,14,20,8,11,18]
root = BinarySearchTree(input[0])
#Create BST
for x in input[1:]:
insertNode(root,BinarySearchTree(x))
#Print tree
print("Inorder traversal of BST >>> ",end="")
inOrderTraversal(root)
print("")
print("Max value =", maxValue(root))