遗传算法求最优解的二叉树出现问题,出现堆栈溢出,求大神解释
程序代码:
#include <stdio.h> #include <stdlib.h> #include <time.h> typedef struct BiTreeNode BiTreeNode ; struct BiTreeNode { char data; BiTreeNode *lchild; BiTreeNode *rchild; }; void Intraverse (BiTreeNode *p_Root); BiTreeNode* Create (BiTreeNode *p_Node,char *a); int count = 0; int main () { char a[17]={"+-*/^LSCEOTS1234"}; BiTreeNode *p_Root[3] = {NULL}; int i = 0; srand (time (NULL)); for (i = 0;i<3;i++) { p_Root[i] = Create (p_Root[i],a); } for (i = 0;i<3;i++) { Intraverse (p_Root[i]); putchar (10); printf (",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,"); putchar (10); } printf ("%d ",count);//count用来统计递归次数; system ("pause"); return 0; } BiTreeNode* Create (BiTreeNode *p_Node,char *a) { count++; printf ("count = %d\n",count);//输出当前递归次数; long r = 0; r = rand(); r = r%16; printf ("%d\n",r); if (p_Node != NULL) { return p_Node; } else { p_Node = (BiTreeNode*) malloc (sizeof (BiTreeNode)); if (p_Node == NULL) { printf ("内存分配错误"); system ("pause"); exit (1); } else { p_Node->lchild = p_Node->rchild = NULL; p_Node->data = a[r]; //加减乘除需要运算左右子树; if (r<=4 && r>=0) { p_Node->lchild = Create (p_Node->lchild,a); p_Node->rchild = Create (p_Node->rchild,a); } else if (r>= 5 && r<= 11) { //log sin cos tan exp cot仅仅运算左子树; p_Node->lchild = Create (p_Node->lchild,a); } return p_Node; } } } void Intraverse (BiTreeNode *p_Root) { if (p_Root) { Intraverse (p_Root->lchild); printf (" %c ",p_Root->data); Intraverse (p_Root->rchild); } }