Find top three elements of binary tree in c++

C++ program for Find top three elements of binary tree. Here mentioned other language solution.
// Include header file
#include <iostream>
using namespace std;
/*
C++ program for
Find top three elements in binary tree
*/
// Node of Binary Tree
class TreeNode
{
public:
int data;
TreeNode *left;
TreeNode *right;
TreeNode(int data)
{
//Set node value
this->data = data;
this->left = nullptr;
this->right = nullptr;
}
};
class BinaryTree
{
public:
TreeNode *root;
TreeNode *first;
TreeNode *second;
TreeNode *third;
BinaryTree()
{
this->root = nullptr;
this->first = nullptr;
this->second = nullptr;
this->third = nullptr;
}
// Recursive function
// Display preorder view of binary tree
void preOrder(TreeNode *node)
{
if (node != nullptr)
{
//Print node value
cout << " " << node->data;
this->preOrder(node->left);
this->preOrder(node->right);
}
}
// Find top three largest nodes in binary tree
void getTopThree(TreeNode *node)
{
if (node != nullptr)
{
if (this->first == nullptr)
{
// 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 == nullptr)
{
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 == nullptr)
{
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.
void printTopThree()
{
if (this->root == nullptr)
{
return;
}
// Display tree elements
cout << "\n Preorder : ";
this->preOrder(this->root);
// Set the initial all three nodes
this->first = nullptr;
this->second = nullptr;
this->third = nullptr;
// Find three largest element
this->getTopThree(this->root);
// Display calculated result
cout << "\n First : " << this->first->data << endl;
if (this->second != nullptr &&
this->second != this->first &&
this->second->data < this->first->data)
{
cout << " Second : " << this->second->data << endl;
}
else
{
cout << " Second : None " << endl;
}
if (this->third != nullptr &&
this->third != this->second &&
this->third != this->first &&
this->third->data < this->second->data)
{
cout << " Third : " << this->third->data << endl;
}
else
{
cout << " Third : None" << endl;
}
}
};
int main()
{
// Create two trees
BinaryTree *x = new BinaryTree();
BinaryTree *y = new BinaryTree();
/*
Construct Binary Tree
-----------------------
10
/ \
/ \
6 8
/ \ / \
12 3 7 5
/ \ \
9 -6 13
*/
// Add nodes
x->root = new TreeNode(10);
x->root->left = new TreeNode(6);
x->root->left->left = new TreeNode(12);
x->root->right = new TreeNode(8);
x->root->right->right = new TreeNode(5);
x->root->right->left = new TreeNode(7);
x->root->right->left->right = new TreeNode(-6);
x->root->left->right = new TreeNode(3);
x->root->left->left->left = new TreeNode(9);
x->root->right->right->right = new TreeNode(13);
/*
Construct Tree
--------------
1
/ \
/ \
1 2
/ / \
1 2 2
*/
// Add second tree node
y->root = new TreeNode(1);
y->root->left = new TreeNode(1);
y->root->right = new TreeNode(2);
y->root->right->right = new TreeNode(2);
y->root->right->left = new TreeNode(2);
y->root->left->left = new TreeNode(1);
// Test One
x->printTopThree();
// Test Two
y->printTopThree();
return 0;
}
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