Binary Tree Algorithms: Generating Random Trees

In summary, generating a random binary tree is not as simple as generating random binary digits piped into a binary tree.
  • #1
ytoruno
85
0
Does anybody know where i can get info on generating random binary trees? I've been checking a lot of publications and have come across the yule process and uniform method. The trees they are being applied to, however, are speciation trees and most of the stuff is incomprehensible to me. Can anybody help and point me in some good direction?
 
Technology news on Phys.org
  • #2
Isnt the problem of producing a random binary tree the same as producing random binary digits piped into a binary tree?

I mean, binary trees are available for most languages and in most introductory texts, so i guess your problem is one of generating sufficiently "random" numbers?
 
  • #3
BarkingMad said:
Isnt the problem of producing a random binary tree the same as producing random binary digits piped into a binary tree?

Not quite-- if you create your list of random numbers first, sort it, and then create a binary tree from that data, you are basically assured that your tree is more-or-less uniformly distributed. The size of the right branch is going to be nearly equal to the size of the left branch.

However, in the real world, you might not know your numbers beforehand. Hence, you either need to reconstruct your tree each time a new number comes in (big pain!), or you simply put the new number into the tree wherever it goes. The first option is more time consuming for inserts, but assures you that you'll get an evenly distributed tree (faster retrieval time). The second option is pretty fast for inserts, but might result in a really lopsided tree. For instance, if you got the numbers:

5, 46, 8, 44, 12, 37, 14, 33, 15, 26, 19, 24, 22, 23

You'd get a really deep tree that would be more time consuming to traverse, because it was built poorly. I assume there are plenty of algorithms to build trees more intelligently, or to "prune" trees quickly once they get this ugly (which is I think what's being discussed). Sadly, I'm too long out of college to remember which algorithms were out there for doing this sort of thing...

DaveE
 
  • #4
thanks for the replies fellas. Well piping random numbers into a binary tree (assuming we have an insert function that inserts as if its a binary search tree) would indeed create a more or less balanced tree. To explain more fully I want to create a method like this:

start with a root, fill it randomly
roll a random int number from 1 to 2
if 1 create left child
else create a right child

and so forth- it would probably need an array somewhere in there for randomly picking nodes to expand, but that's the gist

while i can build an algorithm like this, I am merely looking for alternate methods for generating random trees, because i figure there has to be a better way. Creating random numbers and feeding them into an insert function would not solve this. Its more about randomizing topology of the tree than numbers. Also while I am on the subject, does anybody know a good way to print a binary tree to a console. I made one that becomes hard to read accurately if the tree is too unbalanced.
- yasser toruno
 
  • #5
ytoruno said:
start with a root, fill it randomly
roll a random int number from 1 to 2
if 1 create left child
else create a right child

I dunno-- I think I'd opt for something like a recursive function-- pseudocode:

Code:
function set_child_nodes(parent_node,depth,max) {
  rand = random number 0 to max
  if(rand < depth) {
    #this node has no children
  } else if(rand < depth+(max-depth)/3)) {
    create left_child
    set_child_nodes(left_child,depth+1,max)
  } else if(rand < depth+2*(max-depth)/3) {
    create right_child
    set_child_nodes(right_child,depth+1,max)
  } else {
    create left_child
    set_child_nodes(left_child,depth+1,max)
    create right_child
    set_child_nodes(right_child,depth+1,max)
  }
}

Basically, this algorithm makes a random tree, with an increasingly large chance that no children will get created. Hence, your root node will never be empty, and its children will probably have 1 or more children, and so on down the line, getting progressively more likely that a node will have no children as you progress down the tree.

Of course, you could take out that probability completely, and make it just a random number (0 = no children, 1 = left child, 2 = right child, 3 = both children). You could also change it so that it picks a random number for each child-- that is 0 = no left child, 1 = left child, then pick another random number, 0 = no right child, 1 = right child.

Ultimately, I suppose the "totally random" method (rather than the probability one) would yield more truly random results-- it just has the potential to get REALLY large. I guess you could keep the "truly random" element and just cut it off regardless at a maximum depth, too-- but that depends on what you want.

Anyway, building in probability like that would likely have a similar effect to building up a tree based on a random series of numbers-- it's a little closer to what you might expect in "reality". But if you're looking for genuine randomness of the tree's topology, I'd probably skip it and go for the straight random approach.

DaveE
 
  • #6
On a more practical level, trees are used extensively in financial modelling to price various types of derivative instruments. A good account of some of the practicalities involved in implementing trees for this purpose can be found in Joshi's C++ Design Patterns and Derivatives Pricing.

If you're interested in looking at some actual code in which tree algorithms are used you could do an awful lot worse than studying the QuantLib source, which is available at sourceforge.
 

What is a binary tree algorithm?

A binary tree algorithm is a method used to create a data structure called a binary tree, which is a type of tree data structure where each node has at most two children. These algorithms are used to generate random trees, which are useful for a variety of applications such as data storage, searching, and sorting.

How do binary tree algorithms generate random trees?

There are several different algorithms that can be used to generate random binary trees. One common approach is to use a recursive method, where the algorithm starts at the root node and randomly chooses one of the two children nodes to move to. This process is repeated for each new node until the desired number of nodes is reached.

What is the purpose of generating random trees?

Random trees are useful for a variety of purposes, including data storage, searching, and sorting. They can also be used in machine learning and artificial intelligence applications, as well as in computer graphics and game development.

How do binary tree algorithms ensure randomness in the generated trees?

Most binary tree algorithms use a random number generator to make decisions when creating the tree. This means that each time the algorithm is run, a different random tree will be generated. Additionally, many algorithms have built-in checks to prevent the creation of unbalanced or skewed trees, which could result in less randomness.

What are the advantages of using binary tree algorithms to generate random trees?

Binary tree algorithms are efficient and can generate trees with a large number of nodes in a relatively short amount of time. These trees are also easy to search and manipulate, which makes them useful for a variety of applications. Additionally, the random nature of the trees can help to avoid any biases or patterns in the data, making them useful for statistical analysis and modeling.

Similar threads

  • Programming and Computer Science
Replies
13
Views
4K
  • Programming and Computer Science
Replies
3
Views
3K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
1
Views
618
  • Programming and Computer Science
Replies
3
Views
1K
  • General Math
Replies
2
Views
123
Replies
4
Views
1K
Replies
3
Views
2K
  • Programming and Computer Science
Replies
8
Views
2K
  • Programming and Computer Science
Replies
14
Views
2K
Back
Top