【发布时间】:2012-04-18 08:59:16
【问题描述】:
我有一个使用 AVL 树快速搜索 IP 地址的大型系统:
struct avl_node
{
struct avl_node *left;
struct avl_node *right;
...
void *info; /* point to nhlfe_entry describing nexthop */
}
struct nhlfe_entry
{
u_int32_t nhlfe_ix;
u_char opcode;
...
struct nhlfe_key key;
}
/* defines a search key. */
struct nhlfe_key
{
struct in_addr nh_addr;
u_int32_t oif_ix;
u_int32_t out_label;
}
所以搜索是基于'struct nhlfe_key',即比较函数在 AVL 树如下所示:
static int
mpls_cmp_nhlfe_ipv4_key (void *data1, void* data2)
{
struct nhlfe_entry *nh1, *nh2;
struct nhlfe_key *key1, *key2;
int ret;
nh1 = (struct nhlfe_entry *) data1;
nh2 = (struct nhlfe_entry *) data2;
key1 = (struct nhlfe_key *) nh1->nkey;
key2 = (struct nhlfe_key *) nh2->nkey;
ret = memcmp (&key1->nh_addr, &key2->nh_addr, sizeof (struct in_addr));
if (ret != 0)
return ret;
if (key1->oif_ix > key2->oif_ix)
return 1;
else if (key1->oif_ix < key2->oif_ix)
return -1;
if (key1->out_label > key2->out_label)
return 1;
else if (key1->out_label < key2->out_label)
return -1;
return 0;
}
现在,我要做的是添加对多个下一跃点的支持,即 我在 nhlfe_entry 中添加了一个链表:
struct nhlfe_entry
{
u_int32_t nhlfe_ix;
u_char opcode;
...
struct list *nhkey_list;
}
每个 'struct list' 都是 struct listnode 嵌入 'void *data' 指针 调用者的私人数据,这是'struct nhlfe_key'。
所以我的问题是——如何根据多个元素生成密钥 列表以存储/搜索树中的节点(因为否则现在之后 引入一个列表,就不可能有一个仅基于 one 的键 下一跳地址)。此外,他们同样的问题 申请搜索。
另外,在列表中添加新节点后,是否需要重新构建树, 因为我认为这个操作会改变密钥,因此树可能 变得不平衡? (或自然正确实现的 AVL 树 不需要重建?)
我正在考虑在每个列表节点上生成 CRC,然后进行总结。这可以保证密钥的唯一性吗? (缺点是每当我添加/删除列表节点时,我必须重新生成密钥,从树中删除节点并使用新密钥重新添加。
谢谢!
【问题讨论】:
标签: c data-structures avl-tree