【问题标题】:Sorting a Binary Tree - Function reaches seg. fault对二叉树进行排序 - 函数到达 seg。过错
【发布时间】:2015-03-25 20:00:56
【问题描述】:

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

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

这个想法是数字总和为 100。

我想使用以下格式在树中排序此列表:

1.如果treeLeafNode空间不存在,则创建它。

  1. 取最小值(4,6...这个值将是子节点)并求和。(10...这将是父节点)

3.使用父节点修剪列表,你现在有(10,7,23,60)

  1. 从你现在拥有的低-高数字排序(7,10,23,60)

  2. 重做步骤 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

感谢您抽出宝贵时间,如果可能的话,感谢您的帮助。 祝你有美好的一天!

【问题讨论】:

    标签: c arrays list sorting tree


    【解决方案1】:

    这是解决分段错误后代码的样子。

    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
    

    【讨论】:

      猜你喜欢
      • 2015-11-18
      • 2013-03-19
      • 1970-01-01
      • 1970-01-01
      • 2011-12-23
      • 2020-07-19
      • 1970-01-01
      • 2013-12-14
      • 1970-01-01
      相关资源
      最近更新 更多