Find the median array for Binary tree

Prerequisite: Tree Traversals (Inorder, Preorder and Postorder), Median

Given a Binary tree having integral nodes, the task is to find the median for each position in the preorder, postorder and inorder traversal of the tree.

The median array is given as the array formed with the help of PreOrder, PostOrder, and Inorder traversal of a tree, such that
med[i] = median(preorder[i], inorder[i], postorder[i])



Examples:

Input: Tree =
               1
             /   \
            2     3
          /  \
         4    5 
Output: {4, 2, 4, 3, 3}
Explanation:
Preorder traversal = {1 2 4 5 3}
Inorder traversal =  {4 2 5 1 3}
Postorder traversal = {4 5 2 3 1}
median[0] = median(1, 4, 4) = 4
median[1] = median(2, 2, 5) = 2
median[2] = median(4, 5, 2) = 4
median[3] = median(5, 1, 3) = 3
median[4] = median(3, 3, 1) = 3
Hence, Median array = {4 2 4 3 3}

Input: Tree = 
               25
             /    \
           20      30
         /    \   /   \
       18     22 24   32 
Output: 18 20 20 24 30 30 32

Approach:

  • First, find the preorder, postorder and inorder traversal of the given binary tree and store them each in a vector.
  • Now, for each position from 0 to N, insert the values at that position in each of the traversal arrays into a vector. The vector will be of 3N size.
  • Finally, sort this vector and the median for this position is given by the 2nd element. In this vector, it has 3N elements. Therefore after sorting, the median will be given by the middle element, the 2nd element, in every 3 elements.

Below is the implementation of the above approach:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to Obtain the median
// array for the preorder, postorder
// and inorder traversal of a binary tree
  
#include <bits/stdc++.h>
using namespace std;
  
// A binary tree node has data,
// a pointer to the left child
// and a pointer to the right child
struct Node {
    int data;
    struct Node *left, *right;
    Node(int data)
    {
        this->data = data;
        left = right = NULL;
    }
};
  
// Postorder traversal
void Postorder(
    struct Node* node,
    vector<int>& postorder)
{
    if (node == NULL)
        return;
  
    // First recur on left subtree
    Postorder(node->left, postorder);
  
    // then recur on right subtree
    Postorder(node->right, postorder);
  
    // now deal with the node
    postorder.push_back(node->data);
}
  
// Inorder traversal
void Inorder(
    struct Node* node,
    vector<int>& inorder)
{
    if (node == NULL)
        return;
  
    // First recur on left child
    Inorder(node->left, inorder);
  
    // then print the data of node
    inorder.push_back(node->data);
  
    // now recur on right child
    Inorder(node->right, inorder);
}
  
// Preorder traversal
void Preorder(
    struct Node* node,
    vector<int>& preorder)
{
    if (node == NULL)
        return;
  
    // First print data of node
    preorder.push_back(node->data);
  
    // then recur on left subtree
    Preorder(node->left, preorder);
  
    // now recur on right subtree
    Preorder(node->right, preorder);
}
  
// Function to print the any array
void PrintArray(vector<int> median)
{
    for (int i = 0;
         i < median.size(); i++)
        cout << median[i] << " ";
  
    return;
}
  
// Function to create and print
// the Median array
void MedianArray(struct Node* node)
{
    // Vector to store
    // the median values
    vector<int> median;
  
    if (node == NULL)
        return;
  
    vector<int> preorder,
        postorder,
        inorder;
  
    // Traverse the tree
    Postorder(node, postorder);
    Inorder(node, inorder);
    Preorder(node, preorder);
  
    int n = preorder.size();
    for (int i = 0; i < n; i++) {
  
        // Temporary vector to sort
        // the three values
        vector<int> temp;
  
        // Insert the values at ith index
        // for each traversal into temp
        temp.push_back(postorder[i]);
        temp.push_back(inorder[i]);
        temp.push_back(preorder[i]);
  
        // Sort the temp vector to
        // find the median
        sort(temp.begin(), temp.end());
  
        // Insert the middle value in
        // temp into the median vector
        median.push_back(temp[1]);
    }
  
    PrintArray(median);
    return;
}
  
// Driver Code
int main()
{
    struct Node* root = new Node(1);
    root->left = new Node(2);
    root->right = new Node(3);
    root->left->left = new Node(4);
    root->left->right = new Node(5);
  
    MedianArray(root);
  
    return 0;
}

chevron_right


Output:

4 2 4 3 3

Time Complexity: O(N)

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.