前一篇文章介绍了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>
#include <stdlib.h>
#define MAX 50 // A node of ternary search tree
struct Node
{
char data; // True if this character is last character of one of the words
unsigned isEndOfString: 1; struct Node *left, *eq, *right;
}; // A utility function to create a new ternary search tree node
struct Node* newNode(char data)
{
struct Node* temp = (struct Node*) malloc(sizeof( struct Node ));
temp->data = data;
temp->isEndOfString = 0;
temp->left = temp->eq = temp->right = NULL;
return temp;
} // Function to insert a new word in a Ternary Search Tree
void insert(struct Node** root, char *word)
{
// Base Case: Tree is empty
if (!(*root))
*root = newNode(*word); // If current character of word is smaller than root's character,
// then insert this word in left subtree of root
if ((*word) < (*root)->data)
insert(&( (*root)->left ), word); // If current character of word is greate than root's character,
// then insert this word in right subtree of root
else if ((*word) > (*root)->data)
insert(&( (*root)->right ), word); // If current character of word is same as root's character,
else
{
if (*(word+1))
insert(&( (*root)->eq ), word+1); // the last character of the word
else
(*root)->isEndOfString = 1;
}
} // A recursive function to traverse Ternary Search Tree
void traverseTSTUtil(struct Node* root, char* buffer, int depth)
{
if (root)
{
// First traverse the left subtree
traverseTSTUtil(root->left, buffer, depth); // Store the character of this node
buffer[depth] = root->data;
if (root->isEndOfString)
{
buffer[depth+1] = '\0';
printf( "%s\n", buffer);
} // Traverse the subtree using equal pointer (middle subtree)
traverseTSTUtil(root->eq, buffer, depth + 1); // Finally Traverse the right subtree
traverseTSTUtil(root->right, buffer, depth);
}
} // The main function to traverse a Ternary Search Tree.
// It mainly uses traverseTSTUtil()
void traverseTST(struct Node* root)
{
char buffer[MAX];
traverseTSTUtil(root, buffer, 0);
} // Function to search a given word in TST
int searchTST(struct Node *root, char *word)
{
if (!root)
return 0; if (*word < (root)->data)
return searchTST(root->left, word); else if (*word > (root)->data)
return searchTST(root->right, word); else
{
if (*(word+1) == '\0')
return root->isEndOfString; return searchTST(root->eq, word+1);
}
} // Driver program to test above functions
int main()
{
struct Node *root = NULL; insert(&root, "cat");
insert(&root, "cats");
insert(&root, "up");
insert(&root, "bug"); printf("Following is traversal of ternary search tree\n");
traverseTST(root); printf("\nFollowing are search results for cats, bu and cat respectively\n");
searchTST(root, "cats")? printf("Found\n"): printf("Not Found\n");
searchTST(root, "bu")? printf("Found\n"): printf("Not Found\n");
searchTST(root, "cat")? printf("Found\n"): printf("Not Found\n"); return 0;
}

Output:

Following is traversal of ternary search tree

bug

cat

cats

up

Following are search results for cats, bu and cat respectively

Found

Not Found

Found

Time Complexity: The time complexity of the ternary search tree operations is similar to that of binary search tree. i.e. the insertion, deletion and search operations take time proportional to the height of the ternary search tree. The space is proportional to the length of the string to be stored.

Hashing.

・Need to examine entire key.

・Search hits and misses cost about the same.

・Performance relies on hash function.

・Does not support ordered symbol table operations.

TSTs.

・Works only for string (or digital) keys.

・Only examines just enough key characters.

・Search miss may involve only a few characters.

・Supports ordered symbol table operations (plus extras!). Red-black BST.

・Performance guarantee: log N key compares.

・Supports ordered symbol table API.

Hash tables.

・Performance guarantee: constant number of probes.

・Requires good hash function for key type.

Tries. R-way, TST.

・Performance guarantee: log N characters accessed.

・Supports character-based operations.

Ternary Tree的更多相关文章

  1. 数据结构《17》---- 自动补齐之《二》----Ternary Search Tree

    一. 序言 上一篇文章中,给出了 trie 树的一个实现.可以看到,trie 树有一个巨大的弊病,内存占用过大. 本文给出另一种数据结构来解决上述问题---- Ternary Search Tree ...

  2. Trie和Ternary Search Tree介绍

    Trie树 Trie树,又称字典树,单词查找树或者前缀树,是一种用于快速检索的多叉树结构,如英文字母的字典树是一个26叉树,数字的字典树是一个10叉树. Trie树与二叉搜索树不同,键不是直接保存在节 ...

  3. 数据结构《17》---- 自己主动补齐之《二》----Ternary Search Tree

    一. 序言 上一篇文章中,给出了 trie 树的一个实现. 能够看到,trie 树有一个巨大的弊病,内存占用过大. 本文给出还有一种数据结构来解决上述问题---- Ternary Search Tre ...

  4. IK分词器原理与源码分析

    原文:http://3dobe.com/archives/44/ 引言 做搜索技术的不可能不接触分词器.个人认为为什么搜索引擎无法被数据库所替代的原因主要有两点,一个是在数据量比较大的时候,搜索引擎的 ...

  5. 原创:Solr Wiki 中关于Suggester(搜索推荐)的简单解读

       Solr Wiki Suggester Suggester - a flexible "autocomplete" component.(搜索推荐) A common nee ...

  6. 计算广告(5)----query意图识别

    目录: 一.简介: 1.用户意图识别概念 2.用户意图识别难点 3.用户意图识别分类 4.意图识别方法: (1)基于规则 (2)基于穷举 (3)基于分类模型 二.意图识别具体做法: 1.数据集 2.数 ...

  7. 编解码再进化:Ali266 与下一代视频技术

    过去的一年见证了人类百年不遇的大事记,也见证了多种视频应用的厚积薄发.而因此所带来的视频数据量的爆发式增长更加加剧了对高效编解码这样的底层硬核技术的急迫需求. 新视频编解码标准 VVC 定稿不久之后, ...

  8. Ternary Search Tree 应用--搜索框智能提示

    前面介绍了Ternary Search Tree和它的实现,那么可以用Ternary Search Tree来实现搜索框的只能提示,因为Ternary Search Tree的前缀匹配效率是非常高的, ...

  9. Trie(前缀树)和ternary trie和binary search tree

    1 什么是trie trie是一棵多叉树,假如存放的是由26个字母(不区分大小写)构成的字符串的话,那么就是一棵26叉树. trie树是一棵前缀树,因为每个结点只保存字符串中的一个字符,整个字符串保存 ...

随机推荐

  1. luoguP1401 城市(二分答案+最大流)

    题意 N(2<=n<=200)个城市,M(1<=m<=40000)条无向边,你要找T(1<=T<=200)条从城市1到城市N的路,使得最长的边的长度最小,边不能重复 ...

  2. 今日SGU 5.27

    SGU 122 题意:给你n个人,每个人有大于 N / 2(向上取整)的朋友,问你1这个人有一个书,每个人都想看,只能从朋友之间传递,然后最后回到了1这个人,问你 是否有解,然后有解输出路径 收获:哈 ...

  3. Android输入法扩展之远程输入法

    近年来,互联网电视開始火热,乐视TV,小米TV,近期爱奇艺也在大肆的招人做爱奇艺电视.当然还有更被关注的苹果电视.事实上,这个趋势非常正常,也非常合理,传统单纯的接收电视节目的电视已经太传统了.是该被 ...

  4. django 笔记2

    默默坚持 :路由系统 URL :视图 request.GET request.POST request.FILES #checkbox等多选的内容 request.POST.getlist() #上传 ...

  5. 用efibootmgr管理UEFI启动项,添加丢失的启动项

    UEFI用来替代传统BIOS引导操作系统,学会修改UEFI启动项也变得十分重要,UEFI全称为:“统一的可扩展固件接口”(Unified Extensible Firmware Interface), ...

  6. python单元测试-unittest

    python内部自带了一个单元测试的模块,pyUnit也就是我们说的:unittest 1.介绍下unittest的基本使用方法: 1)import unittest 2)定义一个继承自unittes ...

  7. tensorflow学习之路----保存和提取数据

    #保存数据注意他只能保存变量,不能保存神经网络的框架.#保存数据的作用:保存权重有利于下一次的训练,或者可以用这个数据进行识别#np.arange():arange函数用于创建等差数组,使用频率非常高 ...

  8. 今日SGU 5.14

    //SGU 131 还没完全想清楚 留坑 SGU 259 题意:一个机器处理n个任务,每个任务有时间t和传送时间l 收获:贪心 #include<bits/stdc++.h> #defin ...

  9. Android学习总结(1)——好的 Android 开发习惯

    Android编码规范 java代码中不出现中文,最多注释中可以出现中文: 局部变量命名.静态成员变量命名:只能包含字母,单词首字母出第一个都为大写,其他字母都为小写: 常量命名:只能包含字母和 ,字 ...

  10. uva103 - Stacking Boxes(DAG)

    题目:uva103 - Stacking Boxes(DAG) 题目大意:给出N个boxes, 而且给出这些箱子的维度.要求找一个最长的序列.可以使得以下的箱子一定可以有个维度序列大于上面的那个箱子的 ...