Find top three elements in binary tree

Here given code implementation process.

/*
  C Program  
  Find top three elements in binary tree
*/
#include <stdio.h>

#include <stdlib.h>

//Binary Tree node
struct Node
{
	int data;
	struct Node *left, *right;
};
//This is creating a binary tree node and return this
struct Node *add_node(int data)
{
	// Create dynamic node
	struct Node *new_node = (struct Node *) malloc(sizeof(struct Node));
	if (new_node != NULL)
	{
		//Set data and pointer values
		new_node->data = data;
		new_node->left = NULL;
		new_node->right = NULL;
	}
	else
	{
		//This is indicates, segmentation fault or memory overflow problem
		printf("Memory Overflow\n");
	}
	//return new node
	return new_node;
}
//Recursive function
//Display preorder view of binary tree
void pre_order(struct Node *node)
{
	if (node != NULL)
	{
		//Print node value
		printf("  %d", node->data);
		pre_order(node->left);
		pre_order(node->right);
	}
}
//Find top three largest nodes in binary tree
void top_three(struct Node *node, 
               struct Node **first, 
               struct Node **second, 
               struct Node **third)
{
	if (node != NULL)
	{
		if ( *first == NULL)
		{
			// First node of tree
			*first = node;
		}
		else if (( *first)->data < node->data)
		{
			//When get new first largest node
			//Interchange the node values
			*third = *second;*second = *first;*first = node;
		}
		else if ( *second == NULL)
		{
			if (( *first)->data != node->data)
			{
				//Get second largest node
				*second = node;
			}
		}
		else
		{
			if (( *second)->data < node->data && ( *first)->data > node->data)
			{
				//When we get new second largest
				//Replace the third node with the second node
				*third = *second;
				//This is new second largest element
				*second = node;
			}
			else if ( *third == NULL)
			{
				if (( *second)->data > node->data && ( *first)->data > node->data)
				{
					//Get the third largest node
					*third = node;
				}
			}
			else if (( *third)->data < node->data 
                     && ( *first)->data > node->data 
                     && ( *second)->data > node->data)
			{
				*third = node;
			}
		}
		// Recursively, execute left and right subtree
		top_three(node->left, first, second, third);
		top_three(node->right, first, second, third);
	}
}
// This are handle the request to finding top three nodes in binary tree
void find_node(struct Node *root)
{
	if (root == NULL)
	{
		return;
	}
	//Display tree elements
	printf("\n Preorder : ");
	pre_order(root);
	//Set the initial all three nodes
	struct Node *first = NULL;
	struct Node *second = NULL;
	struct Node *third = NULL;
	// Find three largest element
	top_three(root, &first, &second, &third);
	// Display calculated result
	printf("\n First  : %d ", first->data);
	if (second != NULL && second != first && second->data < first->data)
	{
		printf("\n Second : %d ", second->data);
	}
	else
	{
		printf("\n Second : None ");
	}
	if (third != NULL 
        && third != second 
        && third != first 
        && third->data < second->data)
	{
		printf("\n Third  : %d \n", third->data);
	}
	else
	{
		printf("\n Third  : None \n");
	}
}
int main()
{
	struct Node *root1 = NULL;
	struct Node *root2 = NULL;
	/*
	Construct Binary Tree
	-----------------------
	      10
	     /   \
	    6     8
	   / \   / \
	  2   3 7   5
	 /       \   \
	9        -6   13
	*/
	//Add nodes
	root1 = add_node(10);
	root1->left = add_node(6);
	root1->left->left = add_node(2);
	root1->right = add_node(8);
	root1->right->right = add_node(5);
	root1->right->left = add_node(7);
	root1->right->left->right = add_node(-6);
	root1->left->right = add_node(3);
	root1->left->left->left = add_node(9);
	root1->right->right->right = add_node(13);
	find_node(root1);
	/*  
	Construct Tree
	--------------
	     1
	   /  \
	  1    2
	 /    /  \
	1    2    2
	*/
	//Add second tree node
	root2 = add_node(1);
	root2->left = add_node(1);
	root2->right = add_node(2);
	root2->right->right = add_node(2);
	root2->right->left = add_node(2);
	root2->left->left = add_node(1);
	find_node(root2);
	return 0;
}

Output

 Preorder :   10  6  2  9  3  8  7  -6  5  13
 First  : 13
 Second : 10
 Third  : 9

 Preorder :   1  1  1  2  2  2
 First  : 2
 Second : 1
 Third  : None
/* 
  Java Program 
  Find top three elements in binary tree
*/
//Node of Binary Tree
class Node
{
	public int data;
	public Node left;
	public Node right;
	public Node(int data)
	{
		//Set node value
		this.data = data;
		this.left = null;
		this.right = null;
	}
}
class BinaryTree
{
	public Node root;
	public static Node first;
	public static Node second;
	public static Node third;
	public BinaryTree()
	{
		//Set initial tree root to null
		this.root = null;
		//This is initial resultant Node
		first = null;
		second = null;
		third = null;
	}
	//Recursive function
	//Display preorder view of binary tree
	public void pre_order(Node node)
	{
		if (node != null)
		{
			//Print node value
			System.out.print("  " + node.data);
			pre_order(node.left);
			pre_order(node.right);
		}
	}
	//Find top three largest nodes in binary tree
	public void top_three(Node node)
	{
		if (node != null)
		{
			if (first == null)
			{
				// First node of tree
				first = node;
			}
			else if (first.data < node.data)
			{
				//When get new first largest node
				//Interchange the node values
				third = second;
				second = first;
				first = node;
			}
			else if (second == null)
			{
				if (first.data != node.data)
				{
					//Get second largest node
					second = node;
				}
			}
			else
			{
				if (second.data < node.data && first.data > node.data)
				{
					//When we get new second largest
					//Replace the third node with the second node
					third = second;
					//This is new second largest element
					second = node;
				}
				else if (third == null)
				{
					if (second.data > node.data && first.data > node.data)
					{
						//Get the third largest node
						third = node;
					}
				}
				else if (third.data < node.data 
                         && first.data > node.data && second.data > node.data)
				{
					third = node;
				}
			}
			// Recursively, execute left and right subtree
			top_three(node.left);
			top_three(node.right);
		}
	}
	// This are handle the request to finding top three nodes in binary tree
	public void find_node()
	{
		if (this.root == null)
		{
			return;
		}
		//Display tree elements
		System.out.print("\n Preorder : ");
		this.pre_order(this.root);
		//Set the initial all three nodes
		first = null;
		second = null;
		third = null;
		// Find three largest element
		this.top_three(this.root);
		// Display calculated result
		System.out.print("\n First : " + first.data);
		if (second != null && second != first && second.data < first.data)
		{
			System.out.print("\n Second : " + second.data);
		}
		else
		{
			System.out.print("\n Second : None ");
		}
		if (third != null && third != second 
            && third != first && third.data < second.data)
		{
			System.out.print("\n Third : " + third.data + " \n");
		}
		else
		{
			System.out.print("\n Third : None \n");
		}
	}
	public static void main(String[] args)
	{
		BinaryTree tree1 = new BinaryTree();
		BinaryTree tree2 = new BinaryTree();
		/* 
		Construct Binary Tree
		-----------------------
		      10
		     /   \
		    6     8
		   / \   / \
		  2   3 7   5
		 /       \   \
		9        -6   13
		*/
		//Add nodes
		tree1.root = new Node(10);
		tree1.root.left = new Node(6);
		tree1.root.left.left = new Node(2);
		tree1.root.right = new Node(8);
		tree1.root.right.right = new Node(5);
		tree1.root.right.left = new Node(7);
		tree1.root.right.left.right = new Node(-6);
		tree1.root.left.right = new Node(3);
		tree1.root.left.left.left = new Node(9);
		tree1.root.right.right.right = new Node(13);
		//Test One
		tree1.find_node();
		/*   
		Construct Tree
		--------------
		     1
		   /  \
		  1    2
		 /    /  \
		1    2    2
		*/
		//Add second tree node
		tree2.root = new Node(1);
		tree2.root.left = new Node(1);
		tree2.root.right = new Node(2);
		tree2.root.right.right = new Node(2);
		tree2.root.right.left = new Node(2);
		tree2.root.left.left = new Node(1);
		//Test Two
		tree2.find_node();
	}
}

Output

 Preorder :   10  6  2  9  3  8  7  -6  5  13
 First : 13
 Second : 10
 Third : 9

 Preorder :   1  1  1  2  2  2
 First : 2
 Second : 1
 Third : None
//Include header file
#include <iostream>

using namespace std;
/*
  C++ Program 
  Find top three elements in binary tree
*/
//Node of Binary Tree
class Node
{
    public: int data;
    Node *left;
    Node *right;
    Node(int data)
    {
        //Set node value
        this->data = data;
        this->left = NULL;
        this->right = NULL;
    }
};
class BinaryTree
{
    public: Node *root;
    static Node *first;
    static Node *second;
    static Node *third;
    BinaryTree()
    {
        //Set initial tree root to null
        this->root = NULL;
      
    }
    //Recursive function
    //Display preorder view of binary tree
    void pre_order(Node *node)
    {
        if (node != NULL)
        {
            //Print node value
            cout << "  " << node->data;
            this->pre_order(node->left);
            this->pre_order(node->right);
        }
    }
    //Find top three largest nodes in binary tree
    void top_three(Node *node)
    {
        if (node != NULL)
        {
            if (first == NULL)
            {
                // First node of tree
                first = node;
            }
            else if (BinaryTree::first->data < node->data)
            {
                //When get new first largest node
                //Interchange the node values
                third = BinaryTree::second;
                second = BinaryTree::first;
                first = node;
            }
            else if (BinaryTree::second == NULL)
            {
                if (BinaryTree::first->data != node->data)
                {
                    //Get second largest node
                    BinaryTree::second = node;
                }
            }
            else
            {
                if (BinaryTree::second->data < node->data && BinaryTree::first->data > node->data)
                {
                    //When we get new second largest
                    //Replace the third node with the second node
                    BinaryTree::third = BinaryTree::second;
                    //This is new second largest element
                    BinaryTree::second = node;
                }
                else if (BinaryTree::third == NULL)
                {
                    if (BinaryTree::second->data > node->data && BinaryTree::first->data > node->data)
                    {
                        //Get the third largest node
                        BinaryTree::third = node;
                    }
                }
                else if (BinaryTree::third->data < node->data && BinaryTree::first->data > node->data && BinaryTree::second->data > node->data)
                {
                    BinaryTree::third = node;
                }
            }
            // Recursively, execute left and right subtree
            this->top_three(node->left);
            this->top_three(node->right);
        }
    }
    // This are handle the request to finding top three nodes in binary tree
    void find_node()
    {
        if (this->root == NULL)
        {
            return;
        }
        //Display tree elements
        cout << "\n Preorder : ";
        this->pre_order(this->root);
        //Set the initial all three nodes
        BinaryTree::first = NULL;
        BinaryTree::second = NULL;
        BinaryTree::third = NULL;
        // Find three largest element
        this->top_three(this->root);
        // Display calculated result
        cout << "\n First : " << BinaryTree::first->data;
        if (BinaryTree::second != NULL && BinaryTree::second != BinaryTree::first && BinaryTree::second->data < BinaryTree::first->data)
        {
            cout << "\n Second : " << BinaryTree::second->data;
        }
        else
        {
            cout << "\n Second : None ";
        }
        if (BinaryTree::third != NULL && BinaryTree::third != BinaryTree::second && BinaryTree::third != BinaryTree::first && BinaryTree::third->data < BinaryTree::second->data)
        {
            cout << "\n Third : " << BinaryTree::third->data << " \n";
        }
        else
        {
            cout << "\n Third : None \n";
        }
    }
};

//This is initial resultant Node
Node * BinaryTree :: first = NULL;
Node * BinaryTree :: second = NULL;
Node * BinaryTree :: third = NULL;

int main()
{
    BinaryTree *tree1 = new BinaryTree();
    BinaryTree *tree2 = new BinaryTree();
    /*
            Construct Binary Tree
            -----------------------
                  10
                 /   \
                6     8
               / \   / \
              2   3 7   5
             /       \   \
            9        -6   13
            */
    //Add nodes
    tree1->root = new Node(10);
    tree1->root->left = new Node(6);
    tree1->root->left->left = new Node(2);
    tree1->root->right = new Node(8);
    tree1->root->right->right = new Node(5);
    tree1->root->right->left = new Node(7);
    tree1->root->right->left->right = new Node(-6);
    tree1->root->left->right = new Node(3);
    tree1->root->left->left->left = new Node(9);
    tree1->root->right->right->right = new Node(13);
    //Test One
    tree1->find_node();
    /* 
            Construct Tree
            --------------
                 1
               /  \
              1    2
             /    /  \
            1    2    2
            */
    //Add second tree node
    tree2->root = new Node(1);
    tree2->root->left = new Node(1);
    tree2->root->right = new Node(2);
    tree2->root->right->right = new Node(2);
    tree2->root->right->left = new Node(2);
    tree2->root->left->left = new Node(1);
    //Test Two
    tree2->find_node();
    return 0;
}

Output

 Preorder :   10  6  2  9  3  8  7  -6  5  13
 First : 13
 Second : 10
 Third : 9

 Preorder :   1  1  1  2  2  2
 First : 2
 Second : 1
 Third : None
//Include namespace system
using System;
/* 
  C# Program 
  Find top three elements in binary tree
*/
//Node of Binary Tree
class Node
{
	public int data;
	public Node left;
	public Node right;
	public Node(int data)
	{
		//Set node value
		this.data = data;
		this.left = null;
		this.right = null;
	}
}
class BinaryTree
{
	public Node root;
	public static Node first;
	public static Node second;
	public static Node third;
	public BinaryTree()
	{
		//Set initial tree root to null
		this.root = null;
		//This is initial resultant Node
		first = null;
		second = null;
		third = null;
	}
	//Recursive function
	//Display preorder view of binary tree
	public void pre_order(Node node)
	{
		if (node != null)
		{
			//Print node value
			Console.Write("  " + node.data);
			pre_order(node.left);
			pre_order(node.right);
		}
	}
	//Find top three largest nodes in binary tree
	public void top_three(Node node)
	{
		if (node != null)
		{
			if (first == null)
			{
				// First node of tree
				first = node;
			}
			else if (first.data < node.data)
			{
				//When get new first largest node
				//Interchange the node values
				third = second;
				second = first;
				first = node;
			}
			else if (second == null)
			{
				if (first.data != node.data)
				{
					//Get second largest node
					second = node;
				}
			}
			else
			{
				if (second.data < node.data && first.data > node.data)
				{
					//When we get new second largest
					//Replace the third node with the second node
					third = second;
					//This is new second largest element
					second = node;
				}
				else if (third == null)
				{
					if (second.data > node.data && first.data > node.data)
					{
						//Get the third largest node
						third = node;
					}
				}
				else if (third.data < node.data && first.data > node.data && second.data > node.data)
				{
					third = node;
				}
			}
			// Recursively, execute left and right subtree
			top_three(node.left);
			top_three(node.right);
		}
	}
	// This are handle the request to finding top three nodes in binary tree
	public void find_node()
	{
		if (this.root == null)
		{
			return;
		}
		//Display tree elements
		Console.Write("\n Preorder : ");
		this.pre_order(this.root);
		//Set the initial all three nodes
		first = null;
		second = null;
		third = null;
		// Find three largest element
		this.top_three(this.root);
		// Display calculated result
		Console.Write("\n First : " + first.data);
		if (second != null && second != first && second.data < first.data)
		{
			Console.Write("\n Second : " + second.data);
		}
		else
		{
			Console.Write("\n Second : None ");
		}
		if (third != null && third != second && third != first && third.data < second.data)
		{
			Console.Write("\n Third : " + third.data + " \n");
		}
		else
		{
			Console.Write("\n Third : None \n");
		}
	}
	public static void Main(String[] args)
	{
		BinaryTree tree1 = new BinaryTree();
		BinaryTree tree2 = new BinaryTree();
		/* 
				Construct Binary Tree
				-----------------------
				      10
				     /   \
				    6     8
				   / \   / \
				  2   3 7   5
				 /       \   \
				9        -6   13
				*/
		//Add nodes
		tree1.root = new Node(10);
		tree1.root.left = new Node(6);
		tree1.root.left.left = new Node(2);
		tree1.root.right = new Node(8);
		tree1.root.right.right = new Node(5);
		tree1.root.right.left = new Node(7);
		tree1.root.right.left.right = new Node(-6);
		tree1.root.left.right = new Node(3);
		tree1.root.left.left.left = new Node(9);
		tree1.root.right.right.right = new Node(13);
		//Test One
		tree1.find_node();
		/*   
				Construct Tree
				--------------
				     1
				   /  \
				  1    2
				 /    /  \
				1    2    2
				*/
		//Add second tree node
		tree2.root = new Node(1);
		tree2.root.left = new Node(1);
		tree2.root.right = new Node(2);
		tree2.root.right.right = new Node(2);
		tree2.root.right.left = new Node(2);
		tree2.root.left.left = new Node(1);
		//Test Two
		tree2.find_node();
	}
}

Output

 Preorder :   10  6  2  9  3  8  7  -6  5  13
 First : 13
 Second : 10
 Third : 9

 Preorder :   1  1  1  2  2  2
 First : 2
 Second : 1
 Third : None
<?php
/* 
  Php Program 
  Find top three elements in binary tree
*/
//Node of Binary Tree
class Node
{
	public $data;
	public $left;
	public $right;

	function __construct($data)
	{
		//Set node value
		$this->data = $data;
		$this->left = null;
		$this->right = null;
	}
}
class BinaryTree
{
	public $root;
	public static $first;
	public static $second;
	public static $third;

	function __construct()
	{
		//Set initial tree root to null
		$this->root = null;
		//This is initial resultant Node
		self::$first = null;
		self::$second = null;
		self::$third = null;
	}
	//Recursive function
	//Display preorder view of binary tree
	public	function pre_order($node)
	{
		if ($node != null)
		{
			//Print node value
			echo "  ". $node->data;
			$this->pre_order($node->left);
			$this->pre_order($node->right);
		}
	}
	//Find top three largest nodes in binary tree
	public	function top_three($node)
	{
		if ($node != null)
		{
			if (self::$first == null)
			{
				// First node of tree
				self::$first = $node;
			}
			else if (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;
			}
			else if (self::$second == null)
			{
				if (self::$first->data != $node->data)
				{
					//Get second largest node
					self::$second = $node;
				}
			}
			else
			{
				if (self::$second->data < $node->data && 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;
				}
				else if (self::$third == null)
				{
					if (self::$second->data > $node->data && self::$first->data > $node->data)
					{
						//Get the third largest node
						self::$third = $node;
					}
				}
				else if (self::$third->data < $node->data && self::$first->data > $node->data && self::$second->data > $node->data)
				{
					self::$third = $node;
				}
			}
			// Recursively, execute left and right subtree
			$this->top_three($node->left);
			$this->top_three($node->right);
		}
	}
	// This are handle the request to finding top three nodes in binary tree
	public	function find_node()
	{
		if ($this->root == null)
		{
			return;
		}
		//Display tree elements
		echo "\n Preorder : ";
		$this->pre_order($this->root);
		//Set the initial all three nodes
		self::$first = null;
		self::$second = null;
		self::$third = null;
		// Find three largest element
		$this->top_three($this->root);
		// Display calculated result
		echo "\n First : ". self::$first->data;
		if (self::$second != null && self::$second != self::$first && self::$second->data < self::$first->data)
		{
			echo "\n Second : ". self::$second->data;
		}
		else
		{
			echo "\n Second : None ";
		}
		if (self::$third != null && self::$third != self::$second && self::$third != self::$first && self::$third->data < self::$second->data)
		{
			echo "\n Third : ". self::$third->data ." \n";
		}
		else
		{
			echo "\n Third : None \n";
		}
	}
}

function main()
{
	$tree1 = new BinaryTree();
	$tree2 = new BinaryTree();
	/* 
			Construct Binary Tree
			-----------------------
			      10
			     /   \
			    6     8
			   / \   / \
			  2   3 7   5
			 /       \   \
			9        -6   13
			*/
	//Add nodes
	$tree1->root = new Node(10);
	$tree1->root->left = new Node(6);
	$tree1->root->left->left = new Node(2);
	$tree1->root->right = new Node(8);
	$tree1->root->right->right = new Node(5);
	$tree1->root->right->left = new Node(7);
	$tree1->root->right->left->right = new Node(-6);
	$tree1->root->left->right = new Node(3);
	$tree1->root->left->left->left = new Node(9);
	$tree1->root->right->right->right = new Node(13);
	//Test One
	$tree1->find_node();
	/*   
			Construct Tree
			--------------
			     1
			   /  \
			  1    2
			 /    /  \
			1    2    2
			*/
	//Add second tree node
	$tree2->root = new Node(1);
	$tree2->root->left = new Node(1);
	$tree2->root->right = new Node(2);
	$tree2->root->right->right = new Node(2);
	$tree2->root->right->left = new Node(2);
	$tree2->root->left->left = new Node(1);
	//Test Two
	$tree2->find_node();
}
main();

Output

 Preorder :   10  6  2  9  3  8  7  -6  5  13
 First : 13
 Second : 10
 Third : 9

 Preorder :   1  1  1  2  2  2
 First : 2
 Second : 1
 Third : None
/* 
  Node Js Program 
  Find top three elements in binary tree
*/
//Node of Binary Tree
class Node
{
	constructor(data)
	{
		//Set node value
		this.data = data;
		this.left = null;
		this.right = null;
	}
}
class BinaryTree
{
	constructor()
	{
		//Set initial tree root to null
		this.root = null;
		//This is initial resultant Node
		this.first = null;
		this.second = null;
		this.third = null;
	}
	//Recursive function
	//Display preorder view of binary tree
	pre_order(node)
	{
		if (node != null)
		{
			//Print node value
			process.stdout.write("  " + node.data);
			this.pre_order(node.left);
			this.pre_order(node.right);
		}
	}
	//Find top three largest nodes in binary tree
	top_three(node)
	{
		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.top_three(node.left);
			this.top_three(node.right);
		}
	}
	// This are handle the request to finding top three nodes in binary tree
	find_node()
	{
		if (this.root == null)
		{
			return;
		}
		//Display tree elements
		process.stdout.write("\n Preorder : ");
		this.pre_order(this.root);
		//Set the initial all three nodes
		this.first = null;
		this.second = null;
		this.third = null;
		// Find three largest element
		this.top_three(this.root);
		// Display calculated result
		process.stdout.write("\n First : " + this.first.data);
		if (this.second != null && this.second != this.first 
            && this.second.data < this.first.data)
		{
			process.stdout.write("\n Second : " + this.second.data);
		}
		else
		{
			process.stdout.write("\n Second : None ");
		}
		if (this.third != null && this.third != this.second 
            && this.third != this.first && this.third.data < this.second.data)
		{
			process.stdout.write("\n Third : " + this.third.data + " \n");
		}
		else
		{
			process.stdout.write("\n Third : None \n");
		}
	}
}

function main()
{
	var tree1 = new BinaryTree();
	var tree2 = new BinaryTree();
	/* 
			Construct Binary Tree
			-----------------------
			      10
			     /   \
			    6     8
			   / \   / \
			  2   3 7   5
			 /       \   \
			9        -6   13
			*/
	//Add nodes
	tree1.root = new Node(10);
	tree1.root.left = new Node(6);
	tree1.root.left.left = new Node(2);
	tree1.root.right = new Node(8);
	tree1.root.right.right = new Node(5);
	tree1.root.right.left = new Node(7);
	tree1.root.right.left.right = new Node(-6);
	tree1.root.left.right = new Node(3);
	tree1.root.left.left.left = new Node(9);
	tree1.root.right.right.right = new Node(13);
	//Test One
	tree1.find_node();
	/*   
			Construct Tree
			--------------
			     1
			   /  \
			  1    2
			 /    /  \
			1    2    2
			*/
	//Add second tree node
	tree2.root = new Node(1);
	tree2.root.left = new Node(1);
	tree2.root.right = new Node(2);
	tree2.root.right.right = new Node(2);
	tree2.root.right.left = new Node(2);
	tree2.root.left.left = new Node(1);
	//Test Two
	tree2.find_node();
}
main();

Output

 Preorder :   10  6  2  9  3  8  7  -6  5  13
 First : 13
 Second : 10
 Third : 9

 Preorder :   1  1  1  2  2  2
 First : 2
 Second : 1
 Third : None
#   Python 3 Program 
#   Find top three elements in binary tree

# Node of Binary Tree
class Node :
	
	def __init__(self, data) :
		# Set node value
		self.data = data
		self.left = None
		self.right = None
	

class BinaryTree :
	# This is initial resultant Node
	first = None
	second = None
	third = None
	def __init__(self) :
		# Set initial tree root to null
		self.root = None
	
	# Recursive function
	# Display preorder view of binary tree
	def pre_order(self, node) :
		if (node != None) :
			# Print node value
			print("  ", node.data, end = "")
			self.pre_order(node.left)
			self.pre_order(node.right)
		
	
	# Find top three largest nodes in binary tree
	def top_three(self, node) :
		if (node != None) :
			if (BinaryTree.first == None) :
				#  First node of tree
				BinaryTree.first = node
			
			elif(BinaryTree.first.data < node.data) :
				# When get new first largest node
				# Interchange the node values
				BinaryTree.third = BinaryTree.second
				BinaryTree.second = BinaryTree.first
				BinaryTree.first = node
			
			elif(BinaryTree.second == None) :
				if (BinaryTree.first.data != node.data) :
					# Get second largest node
					BinaryTree.second = node
				
			else :
				if (BinaryTree.second.data < node.data 
                    and BinaryTree.first.data > node.data) :
					# When we get new second largest
					# Replace the third node with the second node
					BinaryTree.third = BinaryTree.second
					# This is new second largest element
					BinaryTree.second = node
				
				elif(BinaryTree.third == None) :
					if (BinaryTree.second.data > node.data 
                        and BinaryTree.first.data > node.data) :
						# Get the third largest node
						BinaryTree.third = node
					
				
				elif(BinaryTree.third.data < node.data 
                     and BinaryTree.first.data > node.data 
                     and BinaryTree.second.data > node.data) :
					BinaryTree.third = node
				
			
			#  Recursively, execute left and right subtree
			self.top_three(node.left)
			self.top_three(node.right)
		
	
	#  This are handle the request to finding top three nodes in binary tree
	def find_node(self) :
		if (self.root == None) :
			return
		
		# Display tree elements
		print("\n Preorder : ", end = "")
		self.pre_order(self.root)
		# Set the initial all three nodes
		BinaryTree.first = None
		BinaryTree.second = None
		BinaryTree.third = None
		#  Find three largest element
		self.top_three(self.root)
		#  Display calculated result
		print("\n First : ", BinaryTree.first.data, end = "")
		if (BinaryTree.second != None 
            and BinaryTree.second != BinaryTree.first 
            and BinaryTree.second.data < BinaryTree.first.data) :
			print("\n Second : ", BinaryTree.second.data, end = "")
		else :
			print("\n Second : None ", end = "")
		
		if (BinaryTree.third != None 
            and BinaryTree.third != BinaryTree.second 
            and BinaryTree.third != BinaryTree.first 
            and BinaryTree.third.data < BinaryTree.second.data) :
			print("\n Third : ", BinaryTree.third.data ," \n", end = "")
		else :
			print("\n Third : None \n", end = "")
		
	

def main() :
	tree1 = BinaryTree()
	tree2 = BinaryTree()
	#  
	# 		Construct Binary Tree
	# 		-----------------------
	# 		      10
	# 		     /   \
	# 		    6     8
	# 		   / \   / \
	# 		  2   3 7   5
	# 		 /       \   \
	# 		9        -6   13
	# 		
	
	# Add nodes
	tree1.root = Node(10)
	tree1.root.left = Node(6)
	tree1.root.left.left = Node(2)
	tree1.root.right = Node(8)
	tree1.root.right.right = Node(5)
	tree1.root.right.left = Node(7)
	tree1.root.right.left.right = Node(-6)
	tree1.root.left.right = Node(3)
	tree1.root.left.left.left = Node(9)
	tree1.root.right.right.right = Node(13)
	# Test One
	tree1.find_node()
	#    
	# 		Construct Tree
	# 		--------------
	# 		     1
	# 		   /  \
	# 		  1    2
	# 		 /    /  \
	# 		1    2    2
	# 		
	
	# Add second tree node
	tree2.root = Node(1)
	tree2.root.left = Node(1)
	tree2.root.right = Node(2)
	tree2.root.right.right = Node(2)
	tree2.root.right.left = Node(2)
	tree2.root.left.left = Node(1)
	# Test Two
	tree2.find_node()

if __name__ == "__main__": main()

Output

 Preorder :    10   6   2   9   3   8   7   -6   5   13
 First :  13
 Second :  10
 Third :  9

 Preorder :    1   1   1   2   2   2
 First :  2
 Second :  1
 Third : None
/* 
  Scala Program 
  Find top three elements in binary tree
*/
//Node of Binary Tree
class Node(var data: Int,
	var left: Node,
		var right: Node)
{
	def this(data: Int)
	{
		this(data, null, null);
	}
}
class BinaryTree(var root: Node,
	var first: Node,
		var second: Node,
			var third: Node)
{
	def this()
	{
		this(null, null, null, null);
	}
	//Recursive function
	//Display preorder view of binary tree
	def pre_order(node: Node): Unit = {
		if (node != null)
		{
			//Print node value
			print("  " + node.data);
			pre_order(node.left);
			pre_order(node.right);
		}
	}
	//Find top three largest nodes in binary tree
	def top_three(node: Node): Unit = {
		if (node != null)
		{
			if (first == null)
			{
				// First node of tree
				first = node;
			}
			else if (first.data < node.data)
			{
				//When get new first largest node
				//Interchange the node values
				third = second;
				second = first;
				first = node;
			}
			else if (second == null)
			{
				if (first.data != node.data)
				{
					//Get second largest node
					second = node;
				}
			}
			else
			{
				if (second.data < node.data && first.data > node.data)
				{
					//When we get new second largest
					//Replace the third node with the second node
					third = second;
					//This is new second largest element
					second = node;
				}
				else if (third == null)
				{
					if (second.data > node.data && first.data > node.data)
					{
						//Get the third largest node
						third = node;
					}
				}
				else if (third.data < node.data && first.data > node.data && second.data > node.data)
				{
					third = node;
				}
			}
			// Recursively, execute left and right subtree
			top_three(node.left);
			top_three(node.right);
		}
	}
	// This are handle the request to finding top three nodes in binary tree
	def find_node(): Unit = {
		if (this.root == null)
		{
			return;
		}
		//Display tree elements
		print("\n Preorder : ");
		this.pre_order(this.root);
		//Set the initial all three nodes
		first = null;
		second = null;
		third = null;
		// Find three largest element
		this.top_three(this.root);
		// Display calculated result
		print("\n First : " + first.data);
		if (second != null && second != first && second.data < first.data)
		{
			print("\n Second : " + second.data);
		}
		else
		{
			print("\n Second : None ");
		}
		if (third != null && third != second && third != first && third.data < second.data)
		{
			print("\n Third : " + third.data + " \n");
		}
		else
		{
			print("\n Third : None \n");
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var tree1: BinaryTree = new BinaryTree();
		var tree2: BinaryTree = new BinaryTree();
		/* 
				Construct Binary Tree
				-----------------------
				      10
				     /   \
				    6     8
				   / \   / \
				  2   3 7   5
				 /       \   \
				9        -6   13
				*/
		//Add nodes
		tree1.root = new Node(10);
		tree1.root.left = new Node(6);
		tree1.root.left.left = new Node(2);
		tree1.root.right = new Node(8);
		tree1.root.right.right = new Node(5);
		tree1.root.right.left = new Node(7);
		tree1.root.right.left.right = new Node(-6);
		tree1.root.left.right = new Node(3);
		tree1.root.left.left.left = new Node(9);
		tree1.root.right.right.right = new Node(13);
		//Test One
		tree1.find_node();
		/*   
				Construct Tree
				--------------
				     1
				   /  \
				  1    2
				 /    /  \
				1    2    2
				*/
		//Add second tree node
		tree2.root = new Node(1);
		tree2.root.left = new Node(1);
		tree2.root.right = new Node(2);
		tree2.root.right.right = new Node(2);
		tree2.root.right.left = new Node(2);
		tree2.root.left.left = new Node(1);
		//Test Two
		tree2.find_node();
	}
}

Output

 Preorder :   10  6  2  9  3  8  7  -6  5  13
 First : 13
 Second : 10
 Third : 9

 Preorder :   1  1  1  2  2  2
 First : 2
 Second : 1
 Third : None
/* 
  Swift 4 Program 
  Find top three elements in binary tree
*/
//Node of Binary Tree
class Node
{
	var data: Int;
	var left: Node? ;
	var right: Node? ;
	init(_ data: Int)
	{
		//Set node value
		self.data = data;
		self.left = nil;
		self.right = nil;
	}
}
class BinaryTree
{
	var root: Node? ;
	var first: Node? ;
	var second: Node? ;
	var third: Node? ;
	init()
	{
		//Set initial tree root to null
		self.root = nil;
		//This is initial resultant Node
		self.first = nil;
		self.second = nil;
		self.third = nil;
	}
	//Recursive function
	//Display preorder view of binary tree
	func pre_order(_ node: Node? )
	{
		if (node != nil)
		{
			//Print node value
			print("  ", node!.data, terminator: "");
			self.pre_order(node!.left);
			self.pre_order(node!.right);
		}
	}
	//Find top three largest nodes in binary tree
	func top_three(_ node: Node? )
	{
		if (node != nil)
		{
			if (self.first == nil)
			{
				// First node of tree
				self.first = node;
			}
			else if (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;
			}
			else if (self.second == nil)
			{
				if (self.first!.data != node!.data)
				{
					//Get second largest node
					self.second = node;
				}
			}
			else
			{
				if (self.second!.data < node!.data && 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;
				}
				else if (self.third == nil)
				{
					if (self.second!.data > node!.data && self.first!.data > node!.data)
					{
						//Get the third largest node
						self.third = node;
					}
				}
				else if (self.third!.data < node!.data && self.first!.data > node!.data && self.second!.data > node!.data)
				{
					self.third = node;
				}
			}
			// Recursively, execute left and right subtree
			self.top_three(node!.left);
			self.top_three(node!.right);
		}
	}
	// This are handle the request to finding top three nodes in binary tree
	func find_node()
	{
		if (self.root == nil)
		{
			return;
		}
		//Display tree elements
		print("\n Preorder : ", terminator: "");
		self.pre_order(self.root);
		//Set the initial all three nodes
		self.first = nil;
		self.second = nil;
		self.third = nil;
		// Find three largest element
		self.top_three(self.root);
		// Display calculated result
		print("\n First : ", self.first!.data, terminator: "");
		if (self.second != nil 
            && !(self.second === self.first)
            && self.second!.data < self.first!.data)
		{
			print("\n Second : ", self.second!.data, terminator: "");
		}
		else
		{
			print("\n Second : None ", terminator: "");
		}
		if (self.third != nil && !(self.third === self.second) 
            && !(self.third === self.first) && self.third!.data < self.second!.data)
		{
			print("\n Third : ", self.third!.data ," \n", terminator: "");
		}
		else
		{
			print("\n Third : None \n", terminator: "");
		}
	}
}
func main()
{
	let tree1: BinaryTree = BinaryTree();
	let tree2: BinaryTree = BinaryTree();
	/* 
			Construct Binary Tree
			-----------------------
			      10
			     /   \
			    6     8
			   / \   / \
			  2   3 7   5
			 /       \   \
			9        -6   13
			*/
	//Add nodes
	tree1.root = Node(10);
	tree1.root!.left = Node(6);
	tree1.root!.left!.left = Node(2);
	tree1.root!.right = Node(8);
	tree1.root!.right!.right = Node(5);
	tree1.root!.right!.left = Node(7);
	tree1.root!.right!.left!.right = Node(-6);
	tree1.root!.left!.right = Node(3);
	tree1.root!.left!.left!.left = Node(9);
	tree1.root!.right!.right!.right = Node(13);
	//Test One
	tree1.find_node();
	/*   
			Construct Tree
			--------------
			     1
			   /  \
			  1    2
			 /    /  \
			1    2    2
			*/
	//Add second tree node
	tree2.root = Node(1);
	tree2.root!.left = Node(1);
	tree2.root!.right = Node(2);
	tree2.root!.right!.right = Node(2);
	tree2.root!.right!.left = Node(2);
	tree2.root!.left!.left = Node(1);
	//Test Two
	tree2.find_node();
}
main();

Output

 Preorder :    10   6   2   9   3   8   7   -6   5   13
 First :  13
 Second :  10
 Third :  9

 Preorder :    1   1   1   2   2   2
 First :  2
 Second :  1
 Third : None
#   Ruby Program 
#   Find top three elements in binary tree

# Node of Binary Tree
class Node  
	# Define the accessor and reader of class Node  
	attr_reader :data, :left, :right
	attr_accessor :data, :left, :right
 
	
	def initialize(data) 
		# Set node value
		self.data = data
		self.left = nil
		self.right = nil
	end
end
class BinaryTree  
	# Define the accessor and reader of class BinaryTree  
	attr_reader :root
	attr_accessor :root
 
	
	def initialize() 
		# Set initial tree root to null
		self.root = nil
		# This is initial resultant Node
		@@first = nil
		@@second = nil
		@@third = nil
	end
	# Recursive function
	# Display preorder view of binary tree
	def pre_order(node) 
		if (node != nil) 
			# Print node value
			print("  ", node.data)
			self.pre_order(node.left)
			self.pre_order(node.right)
		end
	end
	# Find top three largest nodes in binary tree
	def top_three(node) 
		if (node != nil) 
			if (@@first == nil) 
				#  First node of tree
				@@first = node
			elsif(@@first.data < node.data) 
				# When get new first largest node
				# Interchange the node values
				@@third = @@second
				@@second = @@first
				@@first = node
			elsif(@@second == nil) 
				if (@@first.data != node.data) 
					# Get second largest node
					@@second = node
				end
			else 
				if (@@second.data < node.data && @@first.data > node.data) 
					# When we get new second largest
					# Replace the third node with the second node
					@@third = @@second
					# This is new second largest element
					@@second = node
				elsif(@@third == nil) 
					if (@@second.data > node.data && @@first.data > node.data) 
						# Get the third largest node
						@@third = node
					end
				elsif(@@third.data < node.data && @@first.data > node.data && @@second.data > node.data) 
					@@third = node
				end
			end
			#  Recursively, execute left and right subtree
			self.top_three(node.left)
			self.top_three(node.right)
		end
	end
	#  This are handle the request to finding top three nodes in binary tree
	def find_node() 
		if (self.root == nil) 
			return
		end
		# Display tree elements
		print("\n Preorder : ")
		self.pre_order(self.root)
		# Set the initial all three nodes
		@@first = nil
		@@second = nil
		@@third = nil
		#  Find three largest element
		self.top_three(self.root)
		#  Display calculated result
		print("\n First : ", @@first.data)
		if (@@second != nil && @@second != @@first && @@second.data < @@first.data) 
			print("\n Second : ", @@second.data)
		else 
			print("\n Second : None ")
		end
		if (@@third != nil && @@third != @@second && @@third != @@first && @@third.data < @@second.data) 
			print("\n Third : ", @@third.data ," \n")
		else 
			print("\n Third : None \n")
		end
	end
end
def main() 
	tree1 = BinaryTree.new()
	tree2 = BinaryTree.new()
	#  
	# 		Construct Binary Tree
	# 		-----------------------
	# 		      10
	# 		     /   \
	# 		    6     8
	# 		   / \   / \
	# 		  2   3 7   5
	# 		 /       \   \
	# 		9        -6   13
	# 		
	
	# Add nodes
	tree1.root = Node.new(10)
	tree1.root.left = Node.new(6)
	tree1.root.left.left = Node.new(2)
	tree1.root.right = Node.new(8)
	tree1.root.right.right = Node.new(5)
	tree1.root.right.left = Node.new(7)
	tree1.root.right.left.right = Node.new(-6)
	tree1.root.left.right = Node.new(3)
	tree1.root.left.left.left = Node.new(9)
	tree1.root.right.right.right = Node.new(13)
	# Test One
	tree1.find_node()
	#    
	# 		Construct Tree
	# 		--------------
	# 		     1
	# 		   /  \
	# 		  1    2
	# 		 /    /  \
	# 		1    2    2
	# 		
	
	# Add second tree node
	tree2.root = Node.new(1)
	tree2.root.left = Node.new(1)
	tree2.root.right = Node.new(2)
	tree2.root.right.right = Node.new(2)
	tree2.root.right.left = Node.new(2)
	tree2.root.left.left = Node.new(1)
	# Test Two
	tree2.find_node()
end
main()

Output

 Preorder :   10  6  2  9  3  8  7  -6  5  13
 First : 13
 Second : 10
 Third : 9 

 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







© 2021, kalkicode.com, All rights reserved