Binary Tree Algorithms: Generating Random Trees

  • Thread starter Thread starter ytoruno
  • Start date Start date
  • Tags Tags
    Binary Tree
Click For Summary

Discussion Overview

The discussion centers on generating random binary trees, exploring various methods and algorithms for achieving randomness in tree topology. Participants express interest in both theoretical and practical aspects, including applications in financial modeling.

Discussion Character

  • Exploratory
  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant seeks guidance on generating random binary trees, referencing the Yule process and uniform method but finding existing literature challenging.
  • Another participant suggests that generating random binary trees may be akin to producing random binary digits for insertion into a tree, raising questions about the randomness of the numbers used.
  • A different viewpoint emphasizes that sorting random numbers before tree creation can lead to a more balanced tree, while acknowledging the challenges of inserting new numbers dynamically.
  • One participant proposes a method for creating random trees by starting with a root and randomly deciding to create left or right children, expressing a desire for alternative methods to randomize tree topology.
  • Another participant offers a recursive function approach to generate random trees, discussing the balance between randomness and the potential for tree size to grow excessively.
  • A practical application is mentioned, highlighting the use of trees in financial modeling for pricing derivatives, along with a recommendation for relevant literature and source code.

Areas of Agreement / Disagreement

Participants express various methods and ideas for generating random binary trees, but no consensus is reached on a single best approach. Multiple competing views on the nature of randomness and tree construction remain evident throughout the discussion.

Contextual Notes

Participants note limitations in existing methods and the potential for trees to become unbalanced, but do not resolve these issues. The discussion reflects a range of assumptions about randomness and tree structure.

Who May Find This Useful

Individuals interested in algorithms for data structures, particularly in computer science and financial modeling, may find this discussion relevant.

ytoruno
Messages
85
Reaction score
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
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?
 
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
 
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
 
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
 
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.
 

Similar threads

  • · Replies 13 ·
Replies
13
Views
5K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 1 ·
Replies
1
Views
2K