三元搜索树2025年3月17日 | 阅读19分钟 引言三元搜索树(Ternary Search Tree)是一种字典树(trie)数据结构,它作为 trie 的一种低内存替代方案,常用于拼写检查和查找附近邻居等各种应用。 三元搜索树(TST)是一种复杂而高效的数据结构,它结合了二叉搜索树和 trie 结构的优点。它由 Jon Bentley 和 Robert Sedgewick 在他们 1998 年的开创性论文《Ternary Search Trees》中提出。 要理解三元搜索树,我们必须先掌握 trie 的概念。Trie,也称为数字树(digital tree)、基数树(radix tree)或前缀树(prefix tree),是一种搜索树,一种有序树数据结构,用于使用字符串作为键来存储动态集合或关联数组。 三元搜索树的结构三元搜索树本质上是一种 trie,其中每个节点有三个子节点,而不是二叉树中传统的两个。节点设计用于按排序顺序存储键中的单个字符。这种独特的结构使得搜索、插入和删除操作非常高效。 1. 节点结构
2. 平衡结构
让我们举一个例子来说明三元搜索树的结构。 在此示例中,TST 表示字符串“CAT”、“ME”、“MET”、“MEX”、“METAL”和“METEOR”。树中的每个节点对应于这些字符串中的一个字符。 三元搜索树的表示在 trie(标准)数据结构中,每个节点包含 26 个子节点指针,但在三元搜索树中,每个节点仅包含 3 个指针。
除了上述三个指针外,每个节点还有一个字段用于指示数据(字典情况下的字符)和一个字段用于指示字符串的结尾。 因此,它类似于 BST,BST 将数据按特定顺序保存。另一方面,三元搜索树中的数据分散在节点之间。例如,您需要四个节点来存储术语“Bird”。 三元搜索树相对于 trie 的一个优点是,三元搜索树占用的空间更少(每个节点只有三个指针,而标准 trie 有 26 个)。此外,三元搜索树可用于任何可以用哈希表存储字符串的场合。 当单词在字母表中分布均匀时,Trie 非常适合,可以最有效地利用空间。当要存储的字符串都具有相同的相同前缀时,三元搜索树更受欢迎;三元搜索树在空间方面最经济。 三元搜索树上的操作1. 插入 插入操作涉及遍历树,根据需要创建新节点以容纳键的字符。 该过程确保每个节点都按排序顺序插入,从而保持 TST 的固有结构。 2. 搜索 搜索操作涉及基于键的字符遍历树。 在每一步,算法都会将当前字符与当前节点中存储的字符进行比较。 当整个键被遍历并且找到相应的数据时,搜索成功。 3. 删除 TST 中的删除是一项复杂的操作,涉及递归遍历和重构以保持树的平衡。 必须小心处理各种情况,例如具有一个、两个或三个子节点的节点。 性质
实施说明
程序输出 ![]() Java 代码现在我们来编写一个 Java 程序来实现三元搜索树。 输出 上面的 Java 代码产生以下输出。 Ternary Search Tree Test Select one of the operations for Ternary Search Tree:: 1. To insert a new word in the Ternary Search Tree. 2. To search an already existing word in the Ternary Search Tree. 3. To delete a word from the Ternary Search Tree 4. To check if the Ternary Tree is empty. 5. To make the Ternary Search Tree empty. 1 Enter the word to insert red Ternary Search Tree : [red] Do you want to continue (Type y or n) y Select one of the operations for Ternary Search Tree:: 1. To insert a new word in the Ternary Search Tree. 2. To search an already existing word in the Ternary Search Tree. 3. To delete a word from the Ternary Search Tree 4. To check if the Ternary Tree is empty. 5. To make the Ternary Search Tree empty. 1 Enter the word to insert blur e Ternary Search Tree : [blue, red] Do you want to continue (Type y or n) y Select one of the operations for Ternary Search Tree:: 1. To insert a new word in the Ternary Search Tree. 2. To search an already existing word in the Ternary Search Tree. 3. To delete a word from the Ternary Search Tree 4. To check if the Ternary Tree is empty. 5. To make the Ternary Search Tree empty. 1 Enter the word to insert green Ternary Search Tree : [blue, green, red] Do you want to continue (Type y or n) y Select one of the operations for Ternary Search Tree:: 1. To insert a new word in the Ternary Search Tree. 2. To search an already existing word in the Ternary Search Tree. 3. To delete a word from the Ternary Search Tree 4. To check if the Ternary Tree is empty. 5. To make the Ternary Search Tree empty. 1 Enter the word to insert yellow Ternary Search Tree : [blue, green, red, yellow] Do you want to continue (Type y or n) y Select one of the operations for Ternary Search Tree:: 1. To insert a new word in the Ternary Search Tree. 2. To search an already existing word in the Ternary Search Tree. 3. To delete a word from the Ternary Search Tree 4. To check if the Ternary Tree is empty. 5. To make the Ternary Search Tree empty. 1 Enter the word to insert pink Ternary Search Tree : [blue, green, pink, red, yellow] Do you want to continue (Type y or n) y Select one of the operations for Ternary Search Tree:: 1. To insert a new word in the Ternary Search Tree. 2. To search an already existing word in the Ternary Search Tree. 3. To delete a word from the Ternary Search Tree 4. To check if the Ternary Tree is empty. 5. To make the Ternary Search Tree empty. 1 Enter the word to insert black Ternary Search Tree : [black, blue, green, pink, red, yellow] Do you want to continue (Type y or n) y Select one of the operations for Ternary Search Tree:: 1. To insert a new word in the Ternary Search Tree. 2. To search an already existing word in the Ternary Search Tree. 3. To delete a word from the Ternary Search Tree 4. To check if the Ternary Tree is empty. 5. To make the Ternary Search Tree empty. 1 Enter the word to insert grey Ternary Search Tree : [black, blue, green, grey, pink, red, yellow] Do you want to continue (Type y or n) y Select one of the operations for Ternary Search Tree:: 1. To insert a new word in the Ternary Search Tree. 2. To search an already existing word in the Ternary Search Tree. 3. To delete a word from the Ternary Search Tree 4. To check if the Ternary Tree is empty. 5. To make the Ternary Search Tree empty. 1 Enter the word to insert mustard Ternary Search Tree : [black, blue, green, grey, mustard, pink, red, yellow] Do you want to continue (Type y or n) y Select one of the operations for Ternary Search Tree:: 1. To insert a new word in the Ternary Search Tree. 2. To search an already existing word in the Ternary Search Tree. 3. To delete a word from the Ternary Search Tree 4. To check if the Ternary Tree is empty. 5. To make the Ternary Search Tree empty. 1 Enter the word to insert White Ternary Search Tree : [White, black, blue, green, grey, mustard, pink, red, yellow] Do you want to continue (Type y or n) y Select one of the operations for Ternary Search Tree:: 1. To insert a new word in the Ternary Search Tree. 2. To search an already existing word in the Ternary Search Tree. 3. To delete a word from the Ternary Search Tree 4. To check if the Ternary Tree is empty. 5. To make the Ternary Search Tree empty. 1 Enter the word to insert purple Ternary Search Tree : [White, black, blue, green, grey, mustard, pink, purple, red, yellow] Do you want to continue (Type y or n) y Select one of the operations for Ternary Search Tree:: 1. To insert a new word in the Ternary Search Tree. 2. To search an already existing word in the Ternary Search Tree. 3. To delete a word from the Ternary Search Tree 4. To check if the Ternary Tree is empty. 5. To make the Ternary Search Tree empty. 2 Enter the word to search black Search result: true Ternary Search Tree : [White, black, blue, green, grey, mustard, pink, purple, red, yellow] Do you want to continue (Type y or n) y Select one of the operations for Ternary Search Tree:: 1. To insert a new word in the Ternary Search Tree. 2. To search an already existing word in the Ternary Search Tree. 3. To delete a word from the Ternary Search Tree 4. To check if the Ternary Tree is empty. 5. To make the Ternary Search Tree empty. 3 Enter the word to delete blue mustard Ternary Search Tree : [White, black, blue, green, grey, pink, purple, red, yellow] Do you want to continue (Type y or n) y Select one of the operations for Ternary Search Tree:: 1. To insert a new word in the Ternary Search Tree. 2. To search an already existing word in the Ternary Search Tree. 3. To delete a word from the Ternary Search Tree 4. To check if the Ternary Tree is empty. 5. To make the Ternary Search Tree empty. 4 Empty Status: false Ternary Search Tree : [White, black, blue, green, grey, pink, purple, red, yellow] Do you want to continue (Type y or n) y Select one of the operations for Ternary Search Tree:: 1. To insert a new word in the Ternary Search Tree. 2. To search an already existing word in the Ternary Search Tree. 3. To delete a word from the Ternary Search Tree 4. To check if the Ternary Tree is empty. 5. To make the Ternary Search Tree empty. 3 Enter the word to delete black Ternary Search Tree : [White, blue, green, grey, pink, purple, red, yellow] Do you want to continue (Type y or n) y Select one of the operations for Ternary Search Tree:: 1. To insert a new word in the Ternary Search Tree. 2. To search an already existing word in the Ternary Search Tree. 3. To delete a word from the Ternary Search Tree 4. To check if the Ternary Tree is empty. 5. To make the Ternary Search Tree empty. 5 Ternary Search Tree cleared Ternary Search Tree : [] Do you want to continue (Type y or n) y Select one of the operations for Ternary Search Tree:: 1. To insert a new word in the Ternary Search Tree. 2. To search an already existing word in the Ternary Search Tree. 3. To delete a word from the Ternary Search Tree 4. To check if the Ternary Search Tree is empty. 5. To make the Ternary Search Tree empty. 4 Empty Status: true Ternary Search Tree : [] Do you want to continue (Type y or n) n 在上面的 Java 代码中,我们实现了三元搜索树数据结构和各种函数,例如在三元搜索树中添加新数据、从三元搜索树中删除现有数据、检查三元搜索树是否为空,还有一个清空树的操作,该操作将删除三元搜索树中的所有内容,以及对三元搜索树的遍历。 为了对三元搜索树数据结构执行所有这些操作,编写了不同的函数,然后根据对三元搜索树数据结构的操作需求调用这些函数。 C++ 代码让我们编写三元搜索树的 C++ 代码。 输出 上面的 C++ 代码产生以下输出。 Select one of the operations for Ternary Search Tree:: 1. To insert a new word in the Ternary Search Tree. 2. To search an already existing word in the Ternary Search Tree. 3. To Traverse the Ternary Search Tree. 1 Enter the word to insert orange Do you want to continue (Type y or n) y Select one of the operations for Ternary Search Tree:: 1. To insert a new word in the Ternary Search Tree. 2. To search an already existing word in the Ternary Search Tree. 3. To Traverse the Ternary Search Tree. 1 Enter the word to insert banana Do you want to continue (Type y or n) y Select one of the operations for Ternary Search Tree:: 1. To insert a new word in the Ternary Search Tree. 2. To search an already existing word in the Ternary Search Tree. 3. To Traverse the Ternary Search Tree. 1 Enter the word to insert grapes Do you want to continue (Type y or n) y Select one of the operations for Ternary Search Tree:: 1. To insert a new word in the Ternary Search Tree. 2. To search an already existing word in the Ternary Search Tree. 3. To Traverse the Ternary Search Tree. 1 Enter the word to insert apple Do you want to continue (Type y or n) y Select one of the operations for Ternary Search Tree:: 1. To insert a new word in the Ternary Search Tree. 2. To search an already existing word in the Ternary Search Tree. 3. To Traverse the Ternary Search Tree. 1 Enter the word to insert watermelon Do you want to continue (Type y or n) y Select one of the operations for Ternary Search Tree:: 1. To insert a new word in the Ternary Search Tree. 2. To search an already existing word in the Ternary Search Tree. 3. To Traverse the Ternary Search Tree. 1 Enter the word to insert guava Do you want to continue (Type y or n) y Select one of the operations for Ternary Search Tree:: 1. To insert a new word in the Ternary Search Tree. 2. To search an already existing word in the Ternary Search Tree. 3. To Traverse the Ternary Search Tree. 1 Enter the word to insert Kiwi Do you want to continue (Type y or n) y Select one of the operations for Ternary Search Tree:: 1. To insert a new word in the Ternary Search Tree. 2. To search an already existing word in the Ternary Search Tree. 3. To Traverse the Ternary Search Tree. 3 Contents of the Ternary Search Tree are:: Kiwi apple Banana Watermelon guava grapes orange Do you want to continue (Type y or n) y Select one of the operations for Ternary Search Tree:: 1. To insert a new word in the Ternary Search Tree. 2. To search an already existing word in the Ternary Search Tree. 3. To Traverse the Ternary Search Tree. 2 Enter the word to search Kiwi Kiwi found in Ternary Search Tree Do you want to continue (Type y or n) y Select one of the operations for Ternary Search Tree:: 1. To insert a new word in the Ternary Search Tree. 2. To search an already existing word in the Ternary Search Tree. 3. To Traverse the Ternary Search Tree. 2 Enter the word to search mango mango not found in Ternary Search Tree Do you want to continue (Type y or n) n 在上面的 C++ 代码中,我们实现了三元搜索树数据结构和各种函数,例如添加新数据、删除现有数据以及对三元搜索树的遍历。为了对三元搜索树数据结构执行所有这些操作,编写了不同的函数,然后根据对三元搜索树数据结构的操作需求调用这些函数。 实际应用1. 拼写检查 TST 被广泛用于拼写检查算法,以高效地搜索和建议正确的拼写。 2. IP 路由 三元搜索树用于 IP 路由表中,以快速定位给定 IP 地址的最长匹配前缀。 3. 自动完成 TST 为搜索引擎和文本编辑器中的自动完成功能提供支持,提供快速准确的建议。 4. 符号表 TST 作为高效符号表的基础,有助于符号的快速检索和修改。 下一个主题股票跨度问题 |
我们请求您订阅我们的新闻通讯以获取最新更新。