0

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

user1234440
  • 22,521
  • 18
  • 61
  • 103
  • possible duplicate of [Insertion function in a random binary tree](http://stackoverflow.com/questions/11173956/insertion-function-in-a-random-binary-tree) – Stefan Ch Jan 24 '15 at 13:19
  • If you want to generate a n-level tree, just make sure you have at least one node which index `idxMax` ranges from `2^(n-1) + 1` to `2^n`, and thus the tree should have `idxMax / 2`, `idxMax / 4` ... `idxMax / (2^log(idxMax)`th node. – pwwpche Jan 24 '15 at 13:30
  • Are there any algorithms that you can point me to for generating random binary trees? – user1234440 Jan 24 '15 at 14:22
  • @user1234440 Here you have pseudo code from A Field Guide to Genetic Programming, Poli, Langdon, McPhee http://wklej.org/id/1798250/ –  Sep 17 '15 at 11:20

1 Answers1

0

I wrote a simple program to illustrate my method. The program will generate a binary-heap-like structure, and it will be simple to convert it to your structure.

#include <iostream>
#include <time.h>
using namespace std;

int main(){

    int maxDepth;    //The max depth of the tree
    int totalNodes;   //The least number of nodes in the tree.
    int realTotalNodes = 0;  //The real number of nodes in the tree.
    cin >> maxDepth >> totalNodes;

    srand(time(NULL));
    int indexMax = (1 << maxDepth) - 1 ;        //Max index of the nodes in the n-depth binary tree.
    bool* nodes = new bool[indexMax + 1];
    memset(nodes, 0, indexMax + 1);
    int lastMax = indexMax, lastMin =1 << (maxDepth - 1);       //Min and Max index of nodes at n-th level

    //First, promise that the tree will be n-level high.
    //That is, create a path from root to n-th level. 

    int lastIndex = (rand() % lastMin) + lastMin;       //Generate a node that is at n-th level.
    while(lastIndex > 0){       //Create its parent, grand-parent, grand-grand-parent...
            nodes[lastIndex] = true;
            realTotalNodes++;
            lastIndex = lastIndex / 2;
            totalNodes--;

    }

    while(totalNodes > 0){
        int currentIndex = rand() % indexMax;       //Randomly generate the leaves in the tree
        totalNodes--;
        while(currentIndex > 0){        //Create its parents...
            if(nodes[currentIndex] == true){    //If some parent exists, then its grand-parents have already been created. 
                break;
            }
            nodes[currentIndex] = true;
            realTotalNodes++;
            currentIndex = currentIndex / 2;
            totalNodes--;
        }
    }

    //Print these stuff.
    int level = 2;
    for(int i = 1 ; i < indexMax ; i++){
        if(nodes[i]){
            cout << i << "\t";
        }
        if(i == level - 1){
            cout << endl;
            level = level * 2;
        }
    }

    return 0;
}
pwwpche
  • 621
  • 1
  • 5
  • 20