How to generate a random binary tree of given height h?

895 views Asked by At

I am learning to implement tree algorithms and often I need to test my algorithms by generating different binary tree, which i do manually. I tried writing functions for it but it will not generate all possible trees. Any help is highly appreciated.

1

There are 1 answers

0
Su Kai On

if you use Python, maybe the binarytree module is what you need

>>> from binarytree import tree
>>> t = tree(height=3)
>>> t.pprint()

         _______8_____
        /             \
    ___3___            2
   /       \          / \
  14       _4       _5   12
 /  \     /  \     /       \
6    7   10   0   13        11