我对指针算术感到困惑,我想做一个树遍历函数,但我不太确定指针算术是否可以在树中获取远距离节点。在代码中看到会更加清晰,所以在这里。
node **root = huffman_tree(probabilities); // I can only return that as a double ptr
现在,如果我想从我的根节点获取数据:
printf("%lf", (*root)->data);
如果我想要来自根孩子的数据:
printf("%lf", (*root)->left->data); // or (*root)->right->data
但是如果我想进一步深入搜索,我不知道如何到达那些节点呢?
printf("%lf", (*root)->left->left->data); // thats not working
此外,对于树遍历,这不起作用:程序崩溃。
node **root = huffman_tree(probabailities);
preorder(*root);
void preorder(node *n){
if(n == NULL) return;
printf("%lf", n->data);
preorder(n->left);
preorder(n->right);
}
对于上述示例,程序崩溃。
更新1:
似乎 huffman_tree() 确实返回了带有损坏节点的树,我必须为它们错误地分配内存。
该函数被传递一个概率数组,然后获得如下步骤:
1)创建具有给定概率的节点(n个概率-> n个新节点)[工作正常]
2)找到概率最低的两个节点[工作正常] 3)创建一个新节点,它是两个最低概率节点的父节点
4)分配一个新的节点概率等于它的孩子的概率之和
5) 从步骤 2) 开始重复,直到只剩下一个无父节点
node **huffman_tree(double *probabs){
int num_of_nodes = NUM_OF_SYMBOLS;
int num = NUM_OF_SYMBOLS;
// 1) create nodes for given probabilities
node *leafs = (node*) malloc(num_of_nodes*sizeof(node));
int i;
for(i=0; i<num_of_nodes; i+=1){
node *n = (node *) malloc(sizeof(node));
n->probab = *(probabs + i);
n->symbol = *(SYMBOLS + i);
n->left = NULL;
n->right = NULL;
*(leafs+i) = *n;
//free(n);
}
node **root;
while(num_of_nodes > 1){
// 2) Find the two nodes with lowest probabilities
node *two_mins =(node *)malloc(2*sizeof(node));
two_mins = find_two_mins(leafs, num_of_nodes);
node min_n1 = two_mins[0];
node min_n2 = two_mins[1];
// 3) Create a parent node with probability equals to sum of its children probabilities
// add a parent node to leafs
node *new_node = (node *) malloc(sizeof(node));
new_node->probab = min_n1.probab + min_n2.probab;
new_node->left = &min_n1;
new_node->right = &min_n2;
leafs = add_node(leafs, new_node, num);
num += 1;
leafs = remove_node(leafs, &min_n1, num);
num -= 1;
leafs = remove_node(leafs, &min_n2, num);
num -= 1;
num_of_nodes -= 1;
root = &new_node;
}
return root;
函数 add_node() [似乎工作正常]
node *add_node(node *nodes, node *n, int num){
nodes = realloc(nodes, (num+1)*sizeof(node));
nodes[num] = *n;
return nodes;
函数 remove_node() [似乎工作正常]
node *remove_node(node *nodes, node *n, int num){
int i;
int index = 0;
for(i=0; i<num; i+=1){
if(nodes_are_equal(nodes[i], *n)) index = i;
}
for(i=index; i<num-1; i+=1){
nodes[i] = nodes[i+1];
}
nodes = realloc(nodes, (num-1)*sizeof(node));
return nodes;
更新 2
我在 huffman_tree() 函数中改变了一些东西。
函数 find_two_mins() 不再存在,而是被另一个函数 find_min() 的两次调用替换,该函数一次只找到一个最小节点。此外,此函数将指针指向动态分配的节点,并在找到最小值后将其返回。
node *root;
while(num_of_nodes > 1){
// 2) Find two min nodes
node *min_n1= (node *)malloc(sizeof(node));
node*min_n2= (node *)malloc(sizeof(node));
*min_n1= *find_min(leafs, num, min_n1);
leafs = remove_node(leafs, min_n1, num);
num -= 1;
*min_n2= *find_min(leafs, num, min_n2);
leafs = remove_node(leafs, min_n2, num);
num -= 1;
printf("\nTwo Min Nodes: %lf\t%lf", min_n1->probab, min_n2->probab);
printf("\nSum Of All: %lf", s);
// 3) Create parent node of two min nodes
node *new_node = (node *) malloc(sizeof(node));
new_node->probab= min_n1->probab+ min_n2->probab;
new_node->left = min_n1;
new_node->right = min_n2;
leafs = add_node(leafs, new_node, num);
num += 1;
free(min_n1);
free(min_n2);
num_of_nodes -= 1;
root = new_node;
printf("root=%p\n", root);
printf("*root=%p\n", *root);
}
return root;
这是 find_min() 函数:
node *find_min(node *nodes, int num, node *min_node){
double min_probab = nodes[0].probab;
*min_node= nodes[0];
int i;
for(i=0; i<num; i+=1){
if(nodes[i].probab< min_probab){
min_probab = nodes[i].probab;
*min_node = nodes[i];
}
}
return min_node;
问题似乎与此输出有关:
printf("root=%p\n", root);
printf("*root=%p\n", *root);
因为它输出“root = 003A17F0”和“*root = 00000000”