【发布时间】:2015-03-25 20:00:56
【问题描述】:
我想使用下一个数组创建一棵树:
int numbers[] = {4,6,7,23,60};
这个想法是数字总和为 100。
我想使用以下格式在树中排序此列表:
1.如果treeLeafNode空间不存在,则创建它。
- 取最小值(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
感谢您抽出宝贵时间,如果可能的话,感谢您的帮助。 祝你有美好的一天!
【问题讨论】:
标签: c arrays list sorting tree