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 9300321
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T22:45:15+00:00 2026-06-18T22:45:15+00:00

I’m searching a practical algorithm for enumerating all full labeled binary tree. A full

  • 0

I’m searching a practical algorithm for enumerating all full labeled binary tree.

A full binary tree is a tree where all internal nodes has a degree 3, the leaves has degree 1 and the root has a degree 2.

A labeled tree is a tree where all leaves has a unique label.

Example:

    *
    |\
    | \
    *  *
   /|  |\
  / |  | \
 T  C  D  F
  • 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-06-18T22:45:16+00:00Added an answer on June 18, 2026 at 10:45 pm

    From comments, it is clear that the question is to enumerate rooted unordered labelled full binary trees. As explained in this paper, the number of such trees with n labels is (2n-3)!! where !! is the double factorial function.

    The following python program is based on the recursive proof in the referenced paper; I think the code is straight-forward enough that it will pass as an explanation of the algorithm:

    # A very simple representation for Nodes. Leaves are anything which is not a Node.
    class Node(object):
      def __init__(self, left, right):
        self.left = left
        self.right = right
    
      def __repr__(self):
        return '(%s %s)' % (self.left, self.right)
    
    # Given a tree and a label, yields every possible augmentation of the tree by
    # adding a new node with the label as a child "above" some existing Node or Leaf.
    def add_leaf(tree, label):
      yield Node(label, tree)
      if isinstance(tree, Node):
        for left in add_leaf(tree.left, label):
          yield Node(left, tree.right)
        for right in add_leaf(tree.right, label):
          yield Node(tree.left, right)
    
    # Given a list of labels, yield each rooted, unordered full binary tree with
    # the specified labels.
    def enum_unordered(labels):
      if len(labels) == 1:
        yield labels[0]
      else:
        for tree in enum_unordered(labels[1:]):
          for new_tree in add_leaf(tree, labels[0]):
            yield new_tree
    

    For n == 4, there are (2*4 - 3)!! == 5!! == 1 * 3 * 5 == 15 trees:

    >>> for tree in enum_unordered(("a","b","c","d")): print tree
    ... 
    (a (b (c d)))
    ((a b) (c d))
    (b (a (c d)))
    (b ((a c) d))
    (b (c (a d)))
    (a ((b c) d))
    ((a (b c)) d)
    (((a b) c) d)
    ((b (a c)) d)
    ((b c) (a d))
    (a (c (b d)))
    ((a c) (b d))
    (c (a (b d)))
    (c ((a b) d))
    (c (b (a d)))
    

    Another possible interpretation of the question was that it sought an enumeration of rooted ordered full binary trees with a specified list of labels. The number of such trees with n leaves is given by Cn-1, from the Catalan number sequence.

    def enum_ordered(labels):
      if len(labels) == 1:
        yield labels[0]
      else:
        for i in range(1, len(labels)):
          for left in enum_ordered(labels[:i]):
            for right in enum_ordered(labels[i:]):
              yield Node(left, right)
    

    For 5 labels, we have C5-1 == 14:

    >>> for tree in enum_ordered(("a","b","c","d", "e")): print tree
    ... 
    (a (b (c (d e))))
    (a (b ((c d) e)))
    (a ((b c) (d e)))
    (a ((b (c d)) e))
    (a (((b c) d) e))
    ((a b) (c (d e)))
    ((a b) ((c d) e))
    ((a (b c)) (d e))
    (((a b) c) (d e))
    ((a (b (c d))) e)
    ((a ((b c) d)) e)
    (((a b) (c d)) e)
    (((a (b c)) d) e)
    ((((a b) c) d) e)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
Let's say I'm outputting a post title and in our database, it's Hello Y’all
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I've got a string that has curly quotes in it. I'd like to replace
In my XML file chapters tag has more chapter tag.i need to display chapters
I have an array which has BIG numbers and small numbers in it. I
I have a text area in my form which accepts all possible characters from

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.