Trie_tree

本文介绍了一种使用Trie树进行数据插入和搜索的方法。详细展示了Trie树节点结构,并提供了具体的insert和trie_search函数实现代码。通过这些函数可以高效地在Trie树中插入新的记录和查找目标键。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Error_code Trie::insert(const Record &new_record)
{
Error_code result=success;
if(root==NULL)
root=new Trie_node;


int position=0;
char next_char;
Trie_node *location=root;
while(location!=NULL&&(next_char=new_record.key_letter(position))!='\0')
{
int next_location=alphabetic_order(next_char);
if(location->branch[next_location]==NULL)
location->branch[next_location]=new Trie_node;
location=location->branch[next_location];
position++;
}
if(location->data!=NULL)
result=duplicate_error;
else
location->data=new Record(new_record);
return result;
}


Error_code Trie::trie_search(const Key &target, Record &x) const
{
int position=0;
char next_char;
Trie_node *location=root;
while(location!=NULL&&(next_char=target.key_letter(position))!='\0')
{
location=location->branch[alphabetic_order(next_char)];
position++;
}
if(location!=NULL&&location->data!=NULL)
{
      x=*(location->data);
 return success;
}
else 
return not_present;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值