samedi 24 janvier 2015

Binary Tree Generation

I am very new to tree data structures. I know how the entire structure works, but am not sure how to approach randomly generating one.


For example, to create a binary tree with depth 3, you essentially go putting the pieces together one by one. ie:



root = Node()
root.leftChild = Node()
root.rightChild = Node()
root.leftChild.leftChild = 'left'
root.rightChild.rightChild = 'right'


The above doesn't work when I want to randomly create binary tree structures that vary differently between each other. What I mean by randomly creating a tree structure is essentially randomly creating a node type, randomly assign a child or not assign one but the end result will always have a depth of N.


Does anyone have any suggestions on how to approach this? I would love to see some pseudo code/algorithm or anything of that nature.


thanks





Aucun commentaire:

Enregistrer un commentaire