【发布时间】:2016-12-02 00:41:11
【问题描述】:
我正在尝试从一个未排序的浮点数组构建一棵二叉树以进行分配,但我无法完全弄清楚。我的目标是将大小为ndata 的未排序数组xdata 发送到函数build_tree(),该函数使用函数create_node() 创建一个新节点。在数组大小大于1的情况下,它会调用函数paritition_data()(工作正常,但我把它放在问题的底部以供参考),这将交换数组值的顺序,所以所有小于mid 的值都在它的左边,而更大的值在它的右边。该函数返回nleft,即mid 左侧的值的数量。然后我想递归调用partition_data() 来创建新的left 和right 子节点。我认为正是在这一步我的程序似乎失败了,尽管它正在编译,但程序似乎无限递归地调用partition_data(),我不知道为什么。感谢您的帮助。
typedef struct treenode_struct {
int n;
float data;
struct treenode_struct *left, *right;
} treenode;
treenode *create_node( ) {
treenode *node;
node = malloc(sizeof(treenode));
if (node == NULL) {
printf("Allocate Failed");
exit(-1);
}
node->n = 0;
node->right = NULL;
node->left = NULL;
tree_nodes++;
return node;
}
treenode *build_tree( float xdata[], int ndata, float xmin, float xmax ) {
treenode *node;
int nleft;
float mid = (xmin+xmax)/2.;
node = create_node();
node->n = ndata;
if (ndata == 1) { // Add code for this case
node->data = xdata[0];
node->left = NULL;
node->right = NULL;
return node;
}
if (ndata == 0){
printf("Allocate failed\n");
exit(-1);
}
// More than one data point: use partition function
if (ndata > 1) {
nleft = partition_data(xdata,ndata,mid);
int nright = ndata-nleft;
// Add code to make a left child
if(nleft != 0){
node->left=build_tree(xdata,nleft,xmin,xdata[nleft-1]);
}
else{
node->left = NULL;
}
// Add code to make a right child
if(nright != 0){
node->right=build_tree(xdata,nright,xdata[nleft],xmax);
}
else{
node->right = NULL;
}
return node;
}
}
int tree_nodes;
int main() {
const int ndata = 16;
float xdata[] = { 0.963, 0.003, 0.0251, 0.353, 0.667, 0.838, 0.335, 0.915,
0.796, 0.833, 0.345, 0.871, 0.089, 0.888, 0.701, 0.735 };
int i;
float xmiddle = 0.5;
printf("Input data:\n");
for (i=0;i<ndata;i++) printf("%f ",xdata[i]);
printf("\n");
treenode *tree_root;
float tree_xmin, tree_xmax;
tree_nodes = 0;
tree_xmin = 0;
tree_xmax = 1;
tree_root = build_tree( xdata, ndata, tree_xmin, tree_xmax );
printf("Tree Built: nodes %d\n",tree_nodes);
printf("Tree Ordered data:\n");
for (i=0;i<ndata;i++) printf("%f ",xdata[i]);
printf("\n\n");
}
这里是partition_data():
int partition_data( float xdata[], int ndata, float xmiddle ) {
// Your code goes here
int left = 0;
int right = ndata-1;
float temp;
while(left < ndata){ //left loop
if(xdata[left] < xmiddle){
if(left == right){
return left+1;
break;
}//DONE
left = left + 1;
}
else{
while(right<ndata){ //right loop, search for swappable Xright
if(xdata[right] >= xmiddle){//X[right] is greater than/equal to xmiddle
if(left == right){
return left;
break;
}
right=right-1;
}
else{ //found X[right] to swap
temp = xdata[left];
xdata[left] = xdata[right];//swap
xdata[right]=temp;
right = right-1;
if(left == right) {
return left+1;
break;
}
left=left+1;
break;
}
break;
}
}
}
【问题讨论】:
-
你的
mid变量应该代表什么?边界的中间还是数组的中间?? -
mid表示数组中值范围的中间值,它是根据发送到build_tree()的xmin和xmax值计算得出的。 -
你可能会混淆......我认为
mid应该代表数组中间的元素 -
这个想法是
mid不是数组中的一个元素,它表示元素在任一侧交换的边界,因此小于mid的元素在其左侧,并且那些更大的在它的右边。我包含了分区函数以更好地解释它是如何工作的。 -
我想我知道你的递归来自哪里......但我不明白为什么
mid不是数组的中间元素以及为什么node->n = ndata;我不能说如果我是对还是错
标签: c arrays sorting binary-tree bsp-tree