[英]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.