我要使用下一个数组创建树:

int numbers[] = {4,6,7,23,60};

这个想法是这些数字加起来是100。
我想使用下一种格式在树中订购此列表:
1、如果不存在,则创建树型节点空间。
取最小值(4,6…这个值将是子节点)并求和。(10…这个将是父节点)
3.使用父节点修剪列表,现在有(10,7,23,60)
从你现在拥有的低-高(7,10,23,60)的数字中进行排序
重复步骤1、2、3、4,直到到达“顶部”,即(LeftChildNode:40,ParentNode:100,RightChildNode:60);
问题是:从现在的工具来看,创建树的最简单方法是什么?
这是我到目前为止得到的。
我有这个功能:修剪,从低到高排序。
问题:我的树创建功能如下…但出现了分段错误:
void insert_Leaf(int L,int R, hojaNodo **leaf)
{

//JiCase
    if(L+R >= 101)
        return;



    hojaNodo *tempLeafMain;
    tempLeafMain = (hojaNodo*)malloc(sizeof(hojaNodo));
    tempLeafMain = *leaf;

    hojaNodo *tempLeafLeft;
    tempLeafLeft = (hojaNodo*)malloc(sizeof(hojaNodo));




    int tempLeft = L;
    int tempRight = R;
    int tempParent = tempLeft + tempRight;
    //Creamos Parent
  if(tempLeafMain == 0)
    {


    hojaNodo *tempLeafRight;
    tempLeafRight = (hojaNodo*)malloc(sizeof(hojaNodo));


      tempLeafMain->probabilidad = tempParent;
      // init los hijos a null
      tempLeafMain->left = tempLeafLeft;
      tempLeafMain->right = tempLeafRight;

      tempLeafLeft ->probabilidad = tempLeft;
      tempLeafRight->probabilidad = tempRight;

      return;
    }//eof check doesnt exist

   else if(tempLeafMain != NULL){
   if(tempLeft < tempLeafMain->probabilidad){

    hojaNodo *tempLeafParent;
    tempLeafParent = (hojaNodo*)malloc(sizeof(hojaNodo));
    tempLeafParent->probabilidad = tempLeft + tempLeafMain->probabilidad;
    tempLeafParent->right = tempLeafMain;
    tempLeafParent->left = tempLeafLeft;

    tempLeafLeft->probabilidad = tempLeft;
    //tempLeaf->right = *leaf;
    *leaf = tempLeafParent;
    }//eof if

   }//elseif

   }//eof insertLeaf

谢谢你的时间,如果可能的话,谢谢你的帮助。
祝您今天过得愉快!

最佳答案

这就是解决分段错误后代码的外观。

void insert_Leaf(int L,int R, hojaNodo **leaf)
{

//JiCase
printf("inside insert_leaf\n");

    if(L+R >= 101)
        return;


    hojaNodo *tempLeafHead;
    tempLeafHead = (hojaNodo*)malloc(sizeof(hojaNodo));
    tempLeafHead = *leaf;





    int tempLeft = L;
    int tempRight = R;
    int tempParent = tempLeft + tempRight;
    //Creamos Parent
  if(tempLeafHead == 0)
    {
    printf("head is null\n");


    tempLeafHead = (hojaNodo*)malloc(sizeof(hojaNodo));

    hojaNodo *tempLeafLeftChild;
    tempLeafLeftChild = (hojaNodo*)malloc(sizeof(hojaNodo));

    hojaNodo *tempLeafRightChild;
    tempLeafRightChild = (hojaNodo*)malloc(sizeof(hojaNodo));


      tempLeafHead->probabilidad = tempLeft + tempRight;
      tempLeafHead->left = tempLeafLeftChild;
      tempLeafHead->right = tempLeafRightChild;
      tempLeafLeftChild->probabilidad = tempLeft;
      tempLeafRightChild->probabilidad = tempRight;
      *leaf = tempLeafHead;
      return;
    } else if(tempLeafHead != NULL){

    printf("head is NOT null\n");
        if(tempLeft < tempLeafHead->probabilidad){

        hojaNodo *tempLeafParent;
        tempLeafParent = (hojaNodo*)malloc(sizeof(hojaNodo));

        hojaNodo *tempLeafLeftChild;
        tempLeafLeftChild = (hojaNodo*)malloc(sizeof(hojaNodo));

        tempLeafParent->probabilidad = tempLeft + tempLeafHead->probabilidad;
        tempLeafParent->right = tempLeafHead;
        tempLeafParent->left = tempLeafLeftChild;

        tempLeafLeftChild->probabilidad = tempLeft;
        //tempLeaf->right = *leaf;
        *leaf = tempLeafParent;
        }//eof if

   }//elseif

}//eof insertLeaf

08-06 04:40