简体   繁体   中英

Sorting a Binary Tree - Function reaches seg. fault

I want to create a tree using the next array:

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

The idea is that the numbers sum up to 100.

I want to order this list in a tree using the next format:

1.Create a treeLeafNode space if it doesn't exist.

  1. Take the lowest values(4,6...This values will be the child nodes) and make a sum.(10...this will be the parent node)

3.Trim the list using the parent node, you now have (10,7,23,60)

  1. Sort from the numbers you now have low-high(7,10,23,60)

  2. ReDo step 1,2,3,4 until you reach the 'top', which would be(LeftChildNode: 40, ParentNode: 100, RightChildNode: 60);

The problem:From the tools you have now, what would be the easiest way to create the tree?

Here's what I've got so far.

I've got this functions: Trimming, Sorting from low to high. The problem: My tree creating function is as follows...but reaches a segmentation fault:

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

Thank you for your time and if possible, your help. Have a good day!

This is how the code should look like after solving the segmentation fault.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM