前一篇文章介绍了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 wordsunsigned 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 emptyif (!(*root))*root = newNode(*word);// If current character of word is smaller than root's character,// then insert this word in left subtree of rootif ((*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 rootelse 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 wordelse(*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 subtreetraverseTSTUtil(root->left, buffer, depth);// Store the character of this nodebuffer[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 subtreetraverseTSTUtil(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. Trie树 与 三分树(Ternary Trees)

    总结 为什么用Trie树? 词频统计 可能有人要说了,词频统计简单啊,一个hash或者一个堆就可以打完收工,但问题来了,如果内存有限呢?还能这么玩吗?所以这里我们就可以用trie树来压缩下空间,因为公 ...

  2. left-child right-sibling representation of tree - 左孩子右兄弟表示树

    left-child right-sibling representation of tree - 左孩子右兄弟表示树 child-sibling representation, left-child ...

  3. Ternary Search Trees 三分树

    Efficient auto-complete with a ternary search tree 分类: 算法和数据结构学习 2012-04-18 18:03  125人阅读  评论(0)  收藏 ...

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

    过去的一年见证了人类百年不遇的大事记,也见证了多种视频应用的厚积薄发.而因此所带来的视频数据量的爆发式增长更加加剧了对高效编解码这样的底层硬核技术的急迫需求.正是在这样的大环境下,在ITU-T VCE ...

  5. 【今日CV 计算机视觉论文速览 第111期】Fri, 3 May 2019

    今日CS.CV 计算机视觉论文速览 Fri, 3 May 2019 Totally 29 papers ?上期速览✈更多精彩请移步主页 Interesting: ?****Single Image P ...

  6. H.264/H.265/H.266三代视频编码的图像划分

    在看文章的时候看到这篇在知网上面的作者写的这篇文章.可以很清晰的介绍了H.264/AVC.H.265/HEVC.H.266/VVC 视频编码标准中图像划分技术的演进过程,分析不同编码标准图像划分技术的 ...

  7. 语义计算_语义多态性如何在量子计算中起作用

    语义计算 A few months ago, I published an article about an obscure mathematical concept which can be use ...

  8. H.266/VVC的编码结构和块划分

    一.将一帧图像划分成CTUs VVC中一帧图像分成许多编码树单元(CTU).CTU的概念与HEVC的相同.对于一帧具有三通道的图像,CTU由一个N×N的亮度样本块和两个相应的色度样本块组成.图1显示了 ...

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

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

最新文章

  1. 当科学遇上众包:9个值得关注的前沿科技算力众包平台
  2. 251f与ips屏显示器对比_1千多元预算,2020年PS平面设计/摄影后期显示器推荐/选购指南(2k+高色域屏)...
  3. JAVA实现判断树的子结构及树的镜像问题(《剑指offer》)
  4. UVa11292 - Dragon of Loowater(贪心)
  5. esp8266make相关文件改进
  6. DevExpress RichEditControl 上下翻页功能 z
  7. vs编译obj给delphi用
  8. abaqus生成adams柔性体_1:ABAQUS有限元分析-ABAQUS软件介绍
  9. 【原】人生的程序公式
  10. idea快捷键整合-无鼠标操作idea
  11. asBroadcastStream
  12. .NET Framework发展史
  13. 定时任务corn表达式设置
  14. 【备忘录】UTM坐标系与经纬度转换 MATLAB C语言
  15. 教你用Python爬图虫网图片
  16. 九死一生!阿里投资的Bigbasket,靠什么做到印度生鲜电商老大?
  17. bzoj 4568 [Scoi2016]幸运数字
  18. Android闹钟程序
  19. 业绩差距拉大 11家消费金融公司座次洗牌
  20. linux服务器被攻击怎么办

热门文章

  1. 为什么使用Stelnet与sftp协议,而不用telnet与ftp协议
  2. 有关DLNA的一个讲座
  3. 北京交通大学计算机全球排名,2017美国研究生计算机专业排名 - 2017北京交通大学计算机考研成绩310分,能调剂到什么大学...
  4. 讯时网关路由规则小结
  5. 矩阵的entries
  6. 强化学习的学习之路(十)_2021-01-10:K臂老虎机介绍及其Python实现
  7. AI安全领域的“雨山机车大赛”,改变了什么?
  8. 小红书推广效果怎么样?如何进行小红书推广?
  9. 人工智能课程今秋走入高中课堂
  10. lol计算机内存,电脑内存快满了,在玩LOL是弹出内存不足。然后清理了下内存设置了下虚拟内存后电脑出现滴咚的声音并卡机...