Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

For a given binary tree find maximum binary search sub-tree

For a given binary tree, find the largest subtree which is also binary search tree?

Example:

Input:

                   10
               /         \
             50           150
            /  \         /   \
          25    75     200    20
         / \   / \    /  \    / \
        15 35 65 30  120 135 155 250 

Output:

                   50
                  /   \
                 25   75
                / \   /
               15 35  65
like image 627
gtikok Avatar asked Jul 02 '10 05:07

gtikok


People also ask

Where is the maximum number in a binary search tree Mcq?

How will you find the maximum element in a binary search tree? Explanation: Since all the elements greater than a given node are towards the right, iterating through the tree to the rightmost leaf of the root will give the maximum element in a binary search tree.

How do you find the maximum number of nodes in a binary tree?

If binary tree has height h, maximum number of nodes will be when all levels are completely full. Total number of nodes will be 2^0 + 2^1 + …. 2^h = 2^(h+1)-1. For example, the binary tree shown in Figure 2(b) with height 2 has 2^(2+1)-1 = 7 nodes.

What is maximum binary tree?

Maximum Binary Tree in C++The root will hold the maximum number in the array. The left subtree is the maximum tree constructed from left side of the subarray divided by the maximum number. The right subtree is the maximum tree constructed from right side of subarray divided by the maximum number.


1 Answers

This answer previously contained an O(n log n) algorithm based on link/cut trees. Here is a simpler O(n) solution.

The core is a procedure that accepts a node, the unique maximum BSST rooted at its left child, the unique maximum BSST rooted at its right child, and pointers to the left-most and right-most elements of these BSSTs. It destroys its inputs (avoidable with persistent data structures) and constructs the unique maximum BSST rooted at the given node, together with its minimum and maximum elements. All BSST nodes are annotated with the number of descendants. As before, this procedure is called repeatedly from a post-order traversal. To recover the sub-tree, remember the root of the largest BSST; reconstructing it requires only a simple traversal.

I'll treat the left BSST only; the right is symmetric. If the root of the left BSST is greater than the new root, then the entire sub-tree is removed, and the new root is now left-most. Otherwise, the old left-most node is still left-most. Starting from the right-most node of the left BSST and moving upward, find the first node that is less than or equal to the root. Its right child must be removed; note now that due to the BST property, no other nodes need to go! Proceed to the root of the left BSST, updating the counts to reflect the deletion.

The reason this is O(n) is that in spite of the loop, each edge in the original tree is in essence traversed only once.


EDIT: collectively, the paths traversed are the maximal straight-line paths in a BST, except for the left spine and the right spine. For example, on the input

              H
             / \
            /   \
           /     \
          /       \
         /         \
        /           \
       /             \
      D               L
     / \             / \
    /   \           /   \
   /     \         /     \
  B       F       J       N
 / \     / \     / \     / \
A   C   E   G   I   K   M   O

here are the recursive calls on which each edge is traversed:

              H
             / \
            /   \
           /     \
          /       \
         /         \
        /           \
       /             \
      D               L
     / h             h \
    /   h           h   \
   /     h         h     \
  B       F       J       N
 / d     d h     h l     l \
A   C   E   G   I   K   M   O
like image 155
user382751 Avatar answered Nov 04 '22 22:11

user382751