[英]Height trie tree in C
我需要计算 C 中的特里树的高度
节点结构如下:
struct trie_cel {
char type; // 'I': internal / 'P': letter
struct trie_cel *child [HEIGHT_ALPHABET]; // use the function CHAR_TO_INDEX to get child node of each letter
};
typedef struct trie_cel no;
我正在尝试使用递归
if(r == NULL) return -1;
if(!r) return 0;
int alt = 0;
int heightM = 0;
no** i = r->child;
no** fim = i + (sizeof(r->child) / sizeof(no *));
while(i != end){
alt = height(r->child[i])+1;
if(alt > heightM){
heightM = alt;
}
}
return heightM;
但是,我的代码出现以下问题,有人可以帮助我吗?
trie.cpp: In function ‘int altura(no*)’:
trie.cpp:146:32: error: invalid types ‘trie_cel* [27][no** {aka trie_cel**}]’ for array subscript
alt = height(r->child[i])+1;
^
chmod: cannot access 'vpl_execution': No such file or directory
我能够使用“int”进行计算,但我想学习如何使用指针进行计算
也许直接使用指针'height(*i)'。 或者使用指针算法转换为 integer 类型:'i - r->child'
我相信这是您应该采取的方法:
int trie_height(no *root) {
if (root == NULL) return -1;
int i, h = 0;
for (i = 0; i < HEIGHT_ALPHABET; i++) {
int res = 1 + trie_height(root->child[i]);
if (res > h)
h = res;
}
return h;
}
由于child
是一个数组,因此除了 integer 之外,您不能对它的元素进行寻址。 i
在您的代码中不是 integer,所以它不起作用。 此外,您在分配fim
后什么也不做,所以它在那里没用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.