Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Algorithm 二叉树生成_Algorithm_Random_Tree_Genetic Programming - Fatal编程技术网

Algorithm 二叉树生成

Algorithm 二叉树生成,algorithm,random,tree,genetic-programming,Algorithm,Random,Tree,Genetic Programming,我对树数据结构非常陌生。我知道整个结构是如何工作的,但不确定如何随机生成一个 例如,要创建一个深度为3的二叉树,基本上需要将各个部分逐一放在一起。即: root = Node() root.leftChild = Node() root.rightChild = Node() root.leftChild.leftChild = 'left' root.rightChild.rightChild = 'right' 当我想要随机创建彼此不同的二叉树结构时,上述方法不起作用。我所说的随机创建树结

我对树数据结构非常陌生。我知道整个结构是如何工作的,但不确定如何随机生成一个

例如,要创建一个深度为3的二叉树,基本上需要将各个部分逐一放在一起。即:

root = Node()
root.leftChild = Node()
root.rightChild = Node()
root.leftChild.leftChild = 'left'
root.rightChild.rightChild = 'right'
当我想要随机创建彼此不同的二叉树结构时,上述方法不起作用。我所说的随机创建树结构本质上是随机创建一个节点类型,随机分配一个子节点或不分配一个子节点,但最终结果的深度始终为N

有人对如何处理这个问题有什么建议吗?我很想看到一些伪代码/算法或类似的东西


谢谢

我写了一个简单的程序来说明我的方法。该程序将生成一个类似于二进制堆的结构,并且将其转换为您的结构将非常简单

#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;
}
#包括
#包括
使用名称空间std;
int main(){
int maxDepth;//树的最大深度
int totalNodes;//树中的最小节点数。
int realTotalNodes=0;//树中节点的实际数量。
cin>>maxDepth>>totalNodes;
srand(时间(空));
int indexMax=(10){
int currentIndex=rand()%indexMax;//随机生成树中的叶子
总节点--;
当(currentIndex>0){//创建其父级时。。。
如果(nodes[currentIndex]==true){//如果某个父级存在,则其父级已被创建。
打破
}
节点[currentIndex]=真;
realTotalNodes++;
currentIndex=currentIndex/2;
总节点--;
}
}
//打印这些东西。
智力水平=2;
对于(int i=1;i如果要生成一个n级树,则不能复制,只需确保至少有一个节点的索引
idxMax
范围从
2^(n-1)+1
2^n
,因此树应该有
idxMax/2
idxMax/4
idxMax/(2^log(idxMax)
th节点。有什么算法可以帮我生成随机二叉树吗?@user1234440这里有遗传编程领域指南中的伪代码,Poli,Langdon,McPhee