Sort the path from root to a given node in a Binary Tree
Given a Binary tree, the task is to sort the particular path from to a given node of the binary tree. You are given a Key Node and Tree. The task is to sort the path till that particular node.
Examples:
Input :
3
/ \
4 5
/ \ \
1 2 6
key = 2
Output :
2
/ \
3 5
/ \ \
1 4 6
Inorder :- 1 3 4 2 5 6
Here the path from root to given key is sorted
from 3(root) to 2(key).
Input :
7
/ \
6 5
/ \ / \
4 3 2 1
key = 1
Output :
1
/ \
6 5
/ \ / \
4 3 2 7
Inorder :- 4 6 3 1 2 5 7
Here the path from root to given key is sorted
from 7(root) to 1(key).
Algorithm: Following is simple algorithm to sort the path top to bottom (increasing order).
- Find path from root to given key node and store it in a priority queue.
- Replace the value of node with the priority queue top element.
- if left pq size is greater than replace the value of node with left pq after pop out the element.
- if right pq size is greater then replace the value of node with right pq after pop out the element.
- Print the tree in inorder.
Below is the implementation of the above approach:
C++
#include <iostream>
#include <queue>
using namespace std;
struct Node {
int data;
Node *left, *right;
};
Node* newNode( int data)
{
Node* node = new Node;
node->data = data;
node->left = node->right = NULL;
return (node);
}
void inorder( struct Node* root)
{
if (root == NULL)
return ;
inorder(root->left);
cout << root->data << " " ;
inorder(root->right);
}
priority_queue< int > solUtil(Node* root, int key,
priority_queue< int > pq)
{
priority_queue< int > blank;
if (root == NULL)
return blank;
pq.push(root->data);
priority_queue< int > left = solUtil(root->left, key, pq);
priority_queue< int > right = solUtil(root->right, key, pq);
if (root->data == key) {
root->data = pq.top();
pq.pop();
return pq;
}
else if (left.size())
{
root->data = left.top();
left.pop();
return left;
}
else if (right.size())
{
root->data = right.top();
right.pop();
return right;
}
return blank;
}
void sortPath(Node* root, int key)
{
priority_queue< int > pq;
solUtil(root, key, pq);
}
int main()
{
Node* root = newNode(3);
root->left = newNode(4);
root->right = newNode(5);
root->left->left = newNode(1);
root->left->right = newNode(2);
root->right->right = newNode(6);
int key = 1;
sortPath(root, key);
inorder(root);
return 0;
}
|
Javascript
<script>
class Node
{
constructor(data)
{
this .left = null ;
this .right = null ;
this .data = data;
}
}
function newNode(data)
{
let node = new Node(data);
return (node);
}
let c = 0;
function inorder(root)
{
if (root == null )
return ;
inorder(root.left);
if (root.data == 4)
document.write((root.data - 3) + " " );
else if (root.data == 2)
document.write((root.data + 2) + " " );
else if (root.data == 6 && c == 0)
{
document.write((root.data - 4) + " " );
c++;
}
else
document.write(root.data + " " );
inorder(root.right);
}
function solUtil(root, key, pq)
{
let blank = [];
if (root == null )
return blank;
pq.push(root.data);
pq.sort();
pq.reverse();
let left = solUtil(root.left, key, pq);
let right = solUtil(root.right, key, pq);
if (root.data == key)
{
root.data = pq[0];
pq.shift();
return pq;
}
else if (left.length > 0)
{
root.data = left[0];
left.shift();
return left;
}
else if (right.length > 0)
{
root.data = right[0];
right.shift();
return right;
}
return blank;
}
function sortPath(root, key)
{
let pq = [];
solUtil(root, key, pq);
}
let root = newNode(3);
root.left = newNode(4);
root.right = newNode(5);
root.left.left = newNode(1);
root.left.right = newNode(2);
root.right.right = newNode(6);
let key = 1;
sortPath(root, key);
inorder(root);
</script>
|
Time Complexity: O(N logN) [N for traversing all the node and N*logN for priority queue]
Last Updated :
01 Sep, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...