GeeksforGeeks » Algorithms

Largest Binary Search Tree in a given Binary Tree

(9 posts)
  • Started 1 year ago by ovlncl
  • Latest reply from Guddu Sharma

Tags:

  1. Ranga
    guest
    Posted 1 year ago #

    Given a binary tree, find the largest binary SEARCH tree in this binary tree.

    When I mean largest, the tree with maximum number of nodes. And the binary search tree should be a sub-tree in the given binary tree.

    Note: Not every binary tree is a binary search tree

  2. inba
    guest
    Posted 1 year ago #

    Just do inorder traversal of binary tree, we will get an array.

    in the array, find longest increasing substring, which is the biggest bst.

  3. bala
    Member
    Posted 1 year ago #

    @inba : This will give the longest increasing sub array like you said. But the question requirement is different. They are asking for the BST , i.e. a pointer to the root of the BST .

    The solution would be to perform a recursive call to all the nodes , if BST then return the no of nodes under it , else return -1 ( which should be handled as it gets passed upwards ) . Maintain a global variable, i.e. Headref to the current biggest BST and the no of elements under it .

    comments are welcome !

  4. Shashank Mani Narayan
    guest
    Posted 1 year ago #

    Traverse the tree. From each node to the parent, return the following
    set of values.

    1) If BST, size of the current BST or -1 if the tree is not.
    2) Minval & Maxval of the subtree and maxbstsize seen so far (
    probably using references)

    So in each node check the following:

    If( leftmax < node->data && node->data < rightmin)  // Means it is a BST
    {
      new size = rightsize+leftsize+1
      update size if greater than max
      return size
    }
    else
    {
      return -1;
    } 
    
    /* Largest Binary search tree inside a binary tree -- O(n)*/
    
    #include <stdlib.h>
    #include <stdio.h>
    using namespace std;
    
    struct node
    {
      int data;
      struct node* left;
      struct node* right;
    };
    
    struct node* insert_bst(struct node* root, int num){
      if(root == NULL){
        root = (struct node*)malloc(sizeof(struct node));
        root->data = num;
        root->left = NULL;
        root->right = NULL;
        return root;
      }else{
        if(num == root->data){
          return root;
        }
    
        if(num > root->data){
          root->right=insert_bst(root->right,num);
        }else{
          root->left=insert_bst(root->left,num);
        }
      }
      return root;
    }
    
    void inorder_traverse(struct node* root){
      if(root == NULL) return;
    
      inorder_traverse(root->left);
      printf("%d ",root->data);
      inorder_traverse(root->right);
    }
    
    struct node* bst_root = NULL;
    
    int getmaxbst(struct node* root, int& subtreemin, int &subtreemax, int& max)
    {
      if(root == NULL) return 0;
    
      int leftsubtreemin = -32767, rightsubtreemin = -32767;
      int leftsubtreemax = 32767, rightsubtreemax = 32767;
      int x,y;
    
      x = getmaxbst(root->left, leftsubtreemin, leftsubtreemax, max);
      y = getmaxbst(root->right, rightsubtreemin, rightsubtreemax, max);
    
      if(x==-1 || y ==-1)
        return -1;
      if(x==0) { leftsubtreemax = root->data; leftsubtreemin = root->data;}
      if(y==0) { rightsubtreemin = root->data; rightsubtreemax = root->data;}
    
      if(root->data < leftsubtreemax ||
         root->data > rightsubtreemin){
        return -1;
      }
    
      subtreemin = leftsubtreemin;
      subtreemax = rightsubtreemax;
    
      if(x+y+1 > max){
        max = x+y+1;
        bst_root = root;
      }
    
      return x+y+1;
    
    }
    
    int main()
    {
      struct node* root=NULL;
    
      root=insert_bst(root,5);
      root=insert_bst(root,3);
      root=insert_bst(root,9);
      root=insert_bst(root,7);
      root=insert_bst(root,4);
      root=insert_bst(root,1);
      root=insert_bst(root,14);
      root=insert_bst(root,11);
    
      root->data = 0;
    
      int a,b,c,max=-32767;
      c = getmaxbst(root,a,b,max);
      printf("\nmax is %d\n",max);
    
      inorder_traverse(bst_root);
      return 1;
    }
    
  5. Shashank Mani Narayan
    guest
    Posted 1 year ago #

    Traverse the tree. From each node to the parent, return the following
    set of values.

    1) If BST, size of the current BST or -1 if the tree is not.
    2) Minval & Maxval of the subtree and maxbstsize seen so far (
    probably using references)

    So in each node check the following:

    If( leftmax < node->data && node->data < rightmin)// Means it is a BST
    {
      new size = rightsize+leftsize+1
      update size if greater than max
      return size
    
    }else{
      return -1;
    }
    
  6. raghu
    Member
    Posted 1 year ago #

    The SIMPLEST would be.,

    1. Do an InOrder traversal of the Binary tree and store the node values.,
    2. Now find the longest Conitgious Increasing SubSequence in the array which would be the ans.,

  7. xTristan
    guest
    Posted 1 year ago #

    @raghu your solution doesn't work. Your results would end up with incomplete sub-trees. An counter example would be:

    a tree with inorder traversal: 12340, preorder 21430. You can construct the tree yourself from these two kinds of traversal.

    as you can see, 0 is the right child of 4, which makes the sub-tree from 4 a non-BST.

    Your solution would be 1234, which include node 4 and its left child 3. So only left half of the sub-tree starting from node 4 is included.

  8. aayush kumar
    guest
    Posted 4 months ago #

    @ all LIS i inorder of Binary tree will not be the ans
    see
    http://www.leetcode.com/2010/11/largest-binary-search-tree-bst-in.html

  9. Guddu Sharma
    Member
    Posted 1 month ago #

    Here is O(N) approach with O(1) space complexity.
    1. A tree is called BST if it satisfies the following 3 conditions.
    a. Both left & right subtrees are BST.
    b. root node value is greater than the max value in the left subtree.
    c. root node value is smaller then the minimum value in the right subtree.

    So, we will include three more information in a node.
    min: keeps track of the minimum value in a sub-tree.
    max: keeps track of the maximum value in a sub-tree.
    sizeBST: keeps track of the size if a sub-tree if found to be BST.

    We will visit the tree in post-order and update the min,max & sizeBST.

    So,here is the working code.

    void findLargestBSTinBT(struct node *root,struct node **BSTnode)
    {
    	static int maxBSTsize,flag;
    	if(root)
    	{
    		findLargestBSTinBT(root->left,BSTnode);
    
    		findLargestBSTinBT(root->right,BSTnode);
    
    		flag=0;
    		if(!(root->left) && !(root->right))	//encountered a leaf node
    		{
    			root->min=root->data;
    			root->max=root->data;
    			root->sizeBST=1;
    		}
    
    		else if(root->left && root->right && root->left->sizeBST && root->right->sizeBST && root->data>root->left->max &&        										root->data<root->right->min)
    		{
    			root->sizeBST=root->left->sizeBST + root->right->sizeBST + 1;
    
    			root->min=root->left->min;
    			root->max=root->right->max;
    
    			flag=1;
    		}
    		else if(root->left && !root->right && root->left->sizeBST && root->data>root->left->max)
    		{
    			root->sizeBST=root->left->sizeBST + 1;
    
    			root->min=root->left->min;
    			root->max=root->data;
    
    			flag=1;
    		}
    		else if(!root->left && root->right && root->right->sizeBST && root->data<root->right->min)
    		{
    			root->sizeBST=root->right->sizeBST + 1;
    
    			root->min=root->data;
    			root->max=root->right->max;
    
    			flag=1;
    		}
    
    		if(flag && (root->sizeBST)>maxBSTsize)
    		{
    			maxBSTsize=root->sizeBST;
    			*BSTnode=root;
    		}
    	}
    }

    Comments are welcome.
    Pls let me know if there is any corner case that i have left..


Reply

You must log in to post.

RSS feed for this topic