簡體   English   中英

對二叉樹排序-函數達到段。 故障

[英]Sorting a Binary Tree - Function reaches seg. fault

我想使用下一個數組創建樹:

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

感謝您的寶貴時間,如果可以的話,感謝您的幫助。 祝你有美好的一天!

這是解決分段錯誤后的代碼外觀。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM