Ternary Tree

本文介绍了Ternary Search Trees,一种结合了Trie树时间效率和二叉搜索树空间效率的数据结构。它在许多典型搜索问题上比哈希更快,并支持更广泛的有用问题和操作。每个节点有三个子节点:较小(左)、相等(中)、较大(右)。在搜索过程中,根据字符与键的关系选择相应链接。三叉搜索树的操作时间复杂度类似于二叉搜索树,插入、删除和搜索操作的时间与树的高度成比例。
摘要由CSDN通过智能技术生成

  前一篇文章介绍了Trie树,它实现简单但空间效率低。如果要支持26个英文字母,每个节点就要保存26个指针,由于节点数组中保存的空指针占用了太多内存,让我来看看Ternary Tree。
  When you have to store a set of strings, what data structure should you use? You could use hash tables, which sprinkle the strings throughout an array. Access is fast, but information about relative order is lost. Another option is the use of binary search trees, which store strings in order, and are fairly fast. Or you could use digital search tries, which are lightning fast, but use lots of space.
  In this article, we’ll examine ternary search trees, which combine the time efficiency of digital tries with the space efficiency of binary search trees. The resulting structure is faster than hashing for many typical search problems, and supports a broader range of useful problems and operations. Ternary searches are faster than hashing and more powerful, too.
  三叉搜索树Ternary Tree,结合了字典树的时间效率和二叉搜索树的空间效率优点。为了避免多余的指针占用内存,每个Trie节点不再用数组来表示,而是表示成“树中有树”。Trie节点里每个非空指针都会在三叉搜索树里得到属于它自己的节点。
  Each node has 3 children: smaller (left), equal (middle), larger (right).

  Follow links corresponding to each character in the key.
   ・If less, take left link; if greater, take right link.
   ・If equal, take the middle link and move to the next key character.
  Search hit. Node where search ends has a non-null value.
  Search miss. Reach a null link or node where search ends has null value.


// C program to demonstrate Ternary Search Tree (TST) insert, travese 
// and search operations
#include <stdio.h&
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

One2zeror

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值