Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 903445
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T15:52:46+00:00 2026-05-15T15:52:46+00:00

Consider the problem of counting the number of structurally distinct binary search trees :

  • 0

Consider the problem of counting the number of structurally distinct binary search trees:

Given N, find the number of structurally distinct binary search trees containing the values 1 .. N

It’s pretty easy to give an algorithm that solves this: fix every possible number in the root, then recursively solve the problem for the left and right subtrees:

countBST(numKeys)
    if numKeys <= 1
        return 1
    else
        result = 0
        for i = 1 .. numKeys
            leftBST = countBST(i - 1)
            rightBST = countBST(numKeys - i)

            result += leftBST * rightBST

        return result

I’ve recently been familiarizing myself with treaps, and I posed the following problem to myself:

Given N, find the number of distinct treaps containing the values 1 .. N with priorities 1 .. N. Two treaps are distinct if they are structurally different relative to EITHER the key OR the priority (read on for clarification).

I’ve been trying to figure out a formula or an algorithm that can solve this for a while now, but I haven’t been successful. This is what I noticed though:

  1. The answers for n = 2 and n = 3 seem to be 2 and 6, based on me drawing trees on paper.
  2. If we ignore the part that says treaps can also be different relative to the priority of the nodes, the problem seems to be identical to counting just binary search trees, since we’ll be able to assign priorities to each BST such that it also respects the heap invariant. I haven’t proven this though.
  3. I think the hard part is accounting for the possibility to permute the priorities without changing the structure. For example, consider this treap, where the nodes are represented as (key, priority) pairs:

              (3, 5)
              /    \ 
         (2, 3)    (4, 4)
         /              \
    (1, 1)               (5, 2)
    

    We can permute the priorities of both the second and third levels while still maintaining the heap invariant, so we get more solutions even though no keys switch place. This probably gets even uglier for bigger trees. For example, this is a different treap from the one above:

              (3, 5)
              /    \ 
         (2, 4)    (4, 3) // swapped priorities
         /              \
    (1, 1)               (5, 2)
    

I’d appreciate if anyone can share any ideas on how to approach this. It seemed like an interesting counting problem when I thought about it. Maybe someone else thought about it too and even solved it!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T15:52:47+00:00Added an answer on May 15, 2026 at 3:52 pm

    Interesting question! I believe the answer is N factorial!

    Given a tree structure, there is exactly one way to fill in the binary search tree key values.

    Thus all we need to do is count the different number of heaps.

    Given a heap, consider an in-order traversal of the tree.

    This corresponds to a permutation of the numbers 1 to N.

    Now given any permutation of {1,2…,N}, you can construct a heap as follows:

    Find the position of the largest element. The elements to its left form the left subtree and the elements to its right form the right subtree. These subtrees are formed recursively by finding the largest element and splitting there.

    This gives rise to a heap, as we always choose the max element and the in-order traversal of that heap is the permutation we started with. Thus we have a way of going from a heap to a permutaion and back uniquely.

    Thus the required number is N!.

    As an example:

        5
       / \
      3   4          In-order traversal ->   35142
         / \ 
         1  2
    

    Now start with 35142. Largest is 5, so 3 is left subtree and 142 is right.

        5
       / \
      3  {142}
    

    In 142, 4 is largest and 1 is left and 2 is right, so we get

        5
       / \
      3   4
         / \
        1   2
    

    The only way to fill in binary search keys for this is:

        (2,5)
       /     \
    (1,3)    (4,4)
            /     \
           (3,1)   (5,2)
    

    For a more formal proof:

    If HN is the number of heaps on 1…N, then we have that

    HN = Sum_{L=0 to N-1} HL * HN-1-L * (N-1 choose L)

    (basically we pick the max and assign to root. Choose the size of left subtree, and choose that many elements and recurse on left and right).

    Now,

    H0 = 1
    H1 = 1
    H2 = 2
    H3 = 6
    

    If Hn = n! for 0 ≤ n ≤ k

    Then HK+1 = Sum_{L=0 to K} L! * (K-L)! * (K!/L!*(K-L)!) = (K+1)!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Problem: Consider the problem of adding two n-bit binary integers, stored in two n-element
A sample problem asks us to consider the code below and predict what will
I'm writing some Boost.Preprocessor metaprogram, and I have the following problem. Consider the following
I'd like to know when you should consider using multiple table in your query
Consider simple case where user is deleting a post. This is simple HTTP DELETE/POST
Consider this code: TimeStamp.Text = BlogComment.Date.UtcNow.ToString(yyyy-MM-ddTHH\:mm\:ss.fffffffzzz); BlogComment.Date is a DateTime object with its date
Background Write an XML document to a browser's response stream and cause the browser
I'm seeking a behaviour which combines that of VerticalAlign=Stretch and VerticalAlign=Top . Please observe
As in PHP multiple queries are not supported using mysql_query(), how could I convert
I have been assigned for automated testing of Web services to achieve the following

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.