Find top three elements of binary tree in python

Python program for Find top three elements of binary tree. Here mentioned other language solution.
# Python 3 program for
# Find top three elements in binary tree
# Node of Binary Tree
class TreeNode :
def __init__(self, data) :
# Set node value
self.data = data
self.left = None
self.right = None
class BinaryTree :
def __init__(self) :
# Set initial tree root to null
self.root = None
# This is initial resultant Node
self.first = None
self.second = None
self.third = None
# Recursive function
# Display preorder view of binary tree
def preOrder(self, node) :
if (node != None) :
# Print node value
print(" ",node.data, end ="")
self.preOrder(node.left)
self.preOrder(node.right)
# Find top three largest nodes in binary tree
def getTopThree(self, node) :
if (node != None) :
if (self.first == None) :
# First node of tree
self.first = node
elif (self.first.data < node.data) :
# When get new first largest node
# Interchange the node values
self.third = self.second
self.second = self.first
self.first = node
elif (self.second == None) :
if (self.first.data != node.data) :
# Get second largest node
self.second = node
else :
if (self.second.data < node.data and
self.first.data > node.data) :
# When we get new second largest
# Replace the third node with the second node
self.third = self.second
# This is new second largest element
self.second = node
elif (self.third == None) :
if (self.second.data > node.data and
self.first.data > node.data) :
# Get the third largest node
self.third = node
elif (self.third.data < node.data and
self.first.data > node.data and
self.second.data > node.data) :
self.third = node
# Recursively, execute left and right subtree
self.getTopThree(node.left)
self.getTopThree(node.right)
# This are handle the request to finding
# top three nodes in binary tree.
def printTopThree(self) :
if (self.root == None) :
return
# Display tree elements
print("\n Preorder : ", end ="")
self.preOrder(self.root)
# Set the initial all three nodes
self.first = None
self.second = None
self.third = None
# Find three largest element
self.getTopThree(self.root)
# Display calculated result
print("\n First : " + str(self.first.data))
if (self.second != None and
self.second != self.first and
self.second.data < self.first.data) :
print(" Second : " + str(self.second.data))
else :
print(" Second : None ")
if (self.third != None and
self.third != self.second and
self.third != self.first and
self.third.data < self.second.data) :
print(" Third :",self.third.data)
else :
print(" Third : None")
@staticmethod
def main() :
# Create two trees
x = BinaryTree()
y = BinaryTree()
# Construct Binary Tree
# -----------------------
# 10
# / \
# / \
# 6 8
# / \ / \
# 12 3 7 5
# / \ \
# 9 -6 13
# Add nodes
x.root = TreeNode(10)
x.root.left = TreeNode(6)
x.root.left.left = TreeNode(12)
x.root.right = TreeNode(8)
x.root.right.right = TreeNode(5)
x.root.right.left = TreeNode(7)
x.root.right.left.right = TreeNode(-6)
x.root.left.right = TreeNode(3)
x.root.left.left.left = TreeNode(9)
x.root.right.right.right = TreeNode(13)
# Construct Tree
# --------------
# 1
# / \
# / \
# 1 2
# / / \
# 1 2 2
# Add second tree node
y.root = TreeNode(1)
y.root.left = TreeNode(1)
y.root.right = TreeNode(2)
y.root.right.right = TreeNode(2)
y.root.right.left = TreeNode(2)
y.root.left.left = TreeNode(1)
# Test One
x.printTopThree()
# Test Two
y.printTopThree()
if __name__=="__main__":
BinaryTree.main()
Output
Preorder : 10 6 12 9 3 8 7 -6 5 13
First : 13
Second : 12
Third : 10
Preorder : 1 1 1 2 2 2
First : 2
Second : 1
Third : None
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment