forked from ashvish183/Hacktoberfest2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Second-largest-BST.python
84 lines (66 loc) · 1.67 KB
/
Second-largest-BST.python
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
77
78
79
80
81
82
83
84
# Writing A Python3 code to find second largest element in Binary Search Tree
class Node:
# A Constructor to create a new node
def __init__(self, data):
self.key = data
self.left = None
self.right = None
# A function to find 2nd largest
# element in a given tree.
def secondLargestUtil(root, c):
# Base cases, the second condition
# is important to avoid unnecessary
# recursive calls
if root == None or c[0] >= 2:
return
# Follow reverse inorder traversal so that
# the largest element is visited first
secondLargestUtil(root.right, c)
# Increment count of visited nodes
c[0] += 1
# If c becomes k now, then this is
# the 2nd largest
if c[0] == 2:
print("2nd largest element is",
root.key)
return
# Recur for left subtree
secondLargestUtil(root.left, c)
# Function to find 2nd largest element
def secondLargest(root):
# Initialize count of nodes
# visited as 0
c = [0]
# Note that c is passed by reference
secondLargestUtil(root, c)
# A utility function to insert a new
# node with given key in BST
def insert(node, key):
# If the tree is empty, return a new node
if node == None:
return Node(key)
# Otherwise, recur down the tree
if key < node.key:
node.left = insert(node.left, key)
elif key > node.key:
node.right = insert(node.right, key)
# return the (unchanged) node pointer
return node
# Driver Code
if __name__ == '__main__':
# Let us create following BST
# 50
# / \
# 30 70
# / \ / \
# 20 40 60 80
root = None
root = insert(root, 50)
insert(root, 30)
insert(root, 20)
insert(root, 40)
insert(root, 70)
insert(root, 60)
insert(root, 80)
secondLargest(root)
# This code is contributed by PranchalK