gpt4 book ai didi

c - 对二叉树进行排序 - 函数达到 seg。过错

转载 作者:太空宇宙 更新时间:2023-11-04 03:40:16 25 4
gpt4 key购买 nike

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

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

关于c - 对二叉树进行排序 - 函数达到 seg。过错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29265402/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com