Maximum element between two nodes of BST

Given an array of N elements and two integers A, B which belongs to the given array. Create a Binary Search Tree by inserting element from arr[0] to arr[n-1]. The task is to find the maximum element in the path from A to B.

Examples:

Input : arr[] = { 18, 36, 9, 6, 12, 10, 1, 8 }, 
        a = 1, 
        b = 10.
Output : 12


Path from 1 to 10 contains { 1, 6, 9, 12, 10 }. Maximum element is 12.

The idea is to find Longest Common Ancestor of node ‘a’ and node ‘b’. Then search maximum node between LCA and ‘a’, also find maximum node between LCA and ‘b’. Answer will be maximum node of two.

// C++ program to find maximum element in the path
// between two Nodes of Binary Search Tree.
#include <bits/stdc++.h>
using namespace std;

struct Node
{
    struct Node *left, *right;
    int data;
};

// Create and return a pointer of new Node.
Node *createNode(int x)
{
    Node *p = new Node;
    p->data = x;
    p->left = p->right = NULL;
    return p;
}

// Insert a new Node in Binary Search Tree.
void insertNode(struct Node *root, int x)
{
    Node *p = root, *q = NULL;

    while (p != NULL)
    {
        q = p;
        if (p->data < x)
            p = p->right;
        else
            p = p->left;
    }

    if (q == NULL)
        p = createNode(x);
    else
    {
        if (q->data < x)
            q->right = createNode(x);
        else
            q->left = createNode(x);
    }
}

// Return the maximum element between a Node
// and its given ancestor.
int maxelpath(Node *q, int x)
{
    Node *p = q;

    int mx = INT_MIN;

    // Traversing the path between ansector and
    // Node and finding maximum element.
    while (p->data != x)
    {
        if (p->data > x)
        {
            mx = max(mx, p->data);
            p = p->left;
        }
        else
        {
            mx = max(mx, p->data);
            p = p->right;
        }
    }

    return max(mx, x);
}

// Return maximum element in the path between
// two given Node of BST.
int maximumElement(struct Node *root, int x, int y)
{
    Node *p = root;

    // Finding the LCA of Node x and Node y
    while ((x < p->data && y < p->data) ||
           (x > p->data && y > p->data))
    {
        // Checking if both the Node lie on the
        // left side of the parent p.
        if (x < p->data && y < p->data)
            p = p->left;

        // Checking if both the Node lie on the
        // right side of the parent p.
        else if (x > p->data && y > p->data)
            p = p->right;
    }

    // Return the maximum of maximum elements occur
    // in path from ancestor to both Node.
    return max(maxelpath(p, x), maxelpath(p, y));
}


// Driven Program
int main()
{
    int arr[] = { 18, 36, 9, 6, 12, 10, 1, 8 };
    int a = 1, b = 10;
    int n = sizeof(arr)/sizeof(arr[0]);

    // Creating the root of Binary Search Tree
    struct Node *root = createNode(arr[0]);

    // Inserting Nodes in Binary Search Tree
    for (int i = 1; i < n; i++)
        insertNode(root, arr[i]);

    cout << maximumElement(root, a, b) << endl;

    return 0;
}

Output:

12

Time complexity : O(h) where h is height of BST

This article is contributed by Anuj Chauhan. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

GATE CS Corner    Company Wise Coding Practice





Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.