# Find top three elements of binary tree in kotlin

Kotlin program for Find top three elements of binary tree. Here more solutions.

``````/*
Kotlin program for
Find top three elements in binary tree
*/
// Node of Binary Tree
class TreeNode
{
var data: Int;
var left: TreeNode ? ;
var right: TreeNode ? ;
constructor(data: Int)
{
// Set node value
this.data = data;
this.left = null;
this.right = null;
}
}
class BinaryTree
{
var root: TreeNode ? ;
var first: TreeNode ? ;
var second: TreeNode ? ;
var third: TreeNode ? ;
constructor()
{
this.root = null;
this.first = null;
this.second = null;
this.third = null;
}
// Recursive function
// Display preorder view of binary tree
fun preOrder(node: TreeNode ? ): Unit
{
if (node != null)
{
// Print node value
print("  " + node.data);
this.preOrder(node.left);
this.preOrder(node.right);
}
}
// Find top three largest nodes in binary tree
fun getTopThree(node: TreeNode ? ): Unit
{
if (node != null)
{
if (this.first == null)
{
// First node of tree
this.first = node;
}
else if (this.first!!.data < node.data)
{
// When get new first largest node
// Interchange the node values
this.third = this.second;
this.second = this.first;
this.first = node;
}
else if (this.second == null)
{
if (this.first!!.data != node.data)
{
// Get second largest node
this.second = node;
}
}
else
{
if (this.second!!.data < node.data && this.first!!.data > node.data)
{
// When we get new second largest
// Replace the third node with the second node
this.third = this.second;
// This is new second largest element
this.second = node;
}
else if (this.third == null)
{
if (this.second!!.data > node.data &&
this.first!!.data > node.data)
{
// Get the third largest node
this.third = node;
}
}
else if (this.third!!.data < node.data &&
this.first!!.data > node.data &&
this.second!!.data > node.data)
{
this.third = node;
}
}
// Recursively, execute left and right subtree
this.getTopThree(node.left);
this.getTopThree(node.right);
}
}
// This are handle the request to finding
// top three nodes in binary tree.
fun printTopThree(): Unit
{
if (this.root == null)
{
return;
}
// Display tree elements
print("\n Preorder : ");
this.preOrder(this.root);
// Set the initial all three nodes
this.first = null;
this.second = null;
this.third = null;
// Find three largest element
this.getTopThree(this.root);
// Display calculated result
println("\n First : " + this.first!!.data);
if (this.second != null &&
this.second !== this.first &&
this.second!!.data < this.first!!.data)
{
println(" Second : " + this.second!!.data);
}
else
{
println(" Second : None ");
}
if (this.third != null &&
this.third != this.second &&
this.third != this.first &&
this.third!!.data < this.second!!.data)
{
println(" Third : " + this.third!!.data);
}
else
{
println(" Third : None");
}
}
}
fun main(args: Array < String > ): Unit
{
// Create two trees
val x: BinaryTree = BinaryTree();
val y: BinaryTree = BinaryTree();
/*
Construct Binary Tree
-----------------------
10
/  \
/    \
6      8
/ \    / \
12  3  7   5
/        \   \
9         -6  13
*/
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
*/
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();
}``````

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``````

## Comment

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.