Print Binary Search Tree in Min Max Fashion

Given a Binary Search Tree (BST), the task is to print the BST in min-max fashion.

What is min-max fashion?
A min-max fashion means you have to print the maximum node first then the minimum then the second maximum then the second minimum and so on.

Examples:

Input:                
         100                            
        /   \    
      20     500     
     /  \                      
    10   30 
          \   
           40
Output: 10 500 20 100 30 40

Input:
         40                            
        /   \    
      20     50     
     /  \      \               
    10   35     60
        /      /   
      25      55
Output: 10 60 20 55 25 50 35 40

Approach:

  1. Create an array inorder[] and store the inorder traversal of the givrn binary search tree.
  2. Since the inorder traversal of the binary search tree is sorted in ascending, initialise i = 0 and j = n – 1.
  3. Print inorder[i] and update i = i + 1.
  4. Print inorder[j] and update j = j – 1.
  5. Repeat steps 3 and 4 until all the elements have been printed.

Below is the implementation of the above approach:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <iostream>
using namespace std;
  
// Structure of each node of BST
struct node {
    int key;
    struct node *left, *right;
};
  
// A utility function to create a new BST node
node* newNode(int item)
{
    node* temp = new node();
    temp->key = item;
    temp->left = temp->right = NULL;
    return temp;
}
  
/* A utility function to insert a new 
node with given key in BST */
struct node* insert(struct node* node, int key)
{
    /* If the tree is empty, return a new node */
    if (node == NULL)
        return newNode(key);
  
    /* Otherwise, recur down the tree */
    if (key < node->key)
        node->left = insert(node->left, key);
    else if (key > node->key)
        node->right = insert(node->right, key);
  
    /* return the (unchanged) node pointer */
    return node;
}
  
// Function to return the size of the tree
int sizeOfTree(node* root)
{
    if (root == NULL) {
        return 0;
    }
  
    // Calculate left size recursively
    int left = sizeOfTree(root->left);
  
    // Calculate right size recursively
    int right = sizeOfTree(root->right);
  
    // Return total size recursively
    return (left + right + 1);
}
  
// Utility function to print the
// Min max order of BST
void printMinMaxOrderUtil(node* root, int inOrder[],
                          int& index)
{
  
    // Base condition
    if (root == NULL) {
        return;
    }
  
    // Left recursive call
    printMinMaxOrderUtil(root->left, inOrder, index);
  
    // Store elements in inorder array
    inOrder[index++] = root->key;
  
    // Right recursive call
    printMinMaxOrderUtil(root->right, inOrder, index);
}
  
// Function to print the
// Min max order of BST
void printMinMaxOrder(node* root)
{
    // Store the size of BST
    int numNode = sizeOfTree(root);
  
    // Take auxiliary array for storing
    // The inorder traversal of BST
    int inOrder[numNode + 1];
    int index = 0;
  
    // Function call for printing
    // element in min max order
    printMinMaxOrderUtil(root, inOrder, index);
    int i = 0;
    index--;
  
    // While loop for printing elements
    // In front last order
    while (i < index) {
        cout << inOrder[i++] << " "
             << inOrder[index--] << " ";
    }
    if (i == index) {
        cout << inOrder[i] << endl;
    }
}
  
// Driver code
int main()
{
    struct node* root = NULL;
    root = insert(root, 50);
    insert(root, 30);
    insert(root, 20);
    insert(root, 40);
    insert(root, 70);
    insert(root, 60);
    insert(root, 80);
  
    printMinMaxOrder(root);
  
    return 0;
}

chevron_right


Output:

20 80 30 70 40 60 50


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.