Ternary Tree

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

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

  前一篇文章介绍了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>
<think>嗯,用户在使用ternary库时遇到了AttributeError:module 'ternary' hasno attribute'figure'的错误,而且他们提到已经安装了版本1.0.8。我需要仔细分析可能的原因和解决方案。首先,用户之前的问题已经涉及到ternary库的figure属性缺失,可能他们尝试了之前的建议但问题仍然存在。现在需要结合他们提供的站内引用,特别是引用[2]和[3]中提到的matplotlib的类似错误,来寻找线索。首先,用户确认安装了python-ternary1.0.8,但依然无法导入figure。可能的原因包括: 1. **命名冲突**:用户可能有一个名为ternary.py的文件,导致导入时优先加载了这个文件而不是库。这会导致无法找到正确的figure属性。需要检查文件名。2. **安装问题**:虽然用户已经安装了1.0.8,但可能存在安装损坏或部分文件缺失的情况。重新安装或升级可能有效。3.**导入方式错误**:用户可能没有正确导入figure,或者库的结构在特定版本中有变化。例如,某些版本可能需要从子模块导入figure,而不是直接从ternary。4.**环境问题**:可能存在多个Python环境,用户安装的库不在当前使用的环境中,导致实际运行的代码找不到正确的库。 根据引用[2]和[3],matplotlib出现类似错误时,通常是由于导入方式错误或命名冲突。例如,用户可能错误地将matplotlib导入为plt,然后调用plt.figure(),但正确的做法是使用matplotlib.pyplot。类似地,ternary库可能需要特定的导入方式。接下来,我需要验证这些可能性。首先,用户应检查是否有ternary.py文件在当前目录,这可能导致冲突。然后,确保正确安装了python-ternary,并且版本正确。如果这些都确认无误,可能需要检查库的文档,确认figure函数是否存在以及正确的导入方式。 在ternary库的文档中,figure函数通常是从ternary模块直接导入的,例如:from ternaryimport figure但可能在1.0.8版本中,这个函数被移动到了其他子模块,或者被重命名了。用户可能需要查阅该版本的文档或源代码来确认。如果用户无法访问文档,可以尝试在Python环境中使用dir(ternary)来查看可用的属性和函数,确认是否存在figure。 另外,用户提供的引用[4]提到Ternary Tree,可能与问题无关,但需要注意是否混淆了三元树库和绘图库。确保用户安装的是python-ternary,而不是其他类似名称的库。 最后,如果所有方法都无效,可能需要使用替代方法创建图形,例如直接使用matplotlib的Figure类,并结合ternary的绘图方法。例如,手动创建Figure和Axes对象,而不是依赖ternary的figure函数。</think>### 解决`ternary 1.0.8`的`cannot import name 'figure'`错误 #### **1. 问题根源分析** 尽管已安装`python-ternary 1.0.8`,但出现该错误的原因可能是: 1. **模块导入方式变更**:新版库可能调整了API结构,`figure`方法可能被移动到子模块[^1] 2. **命名覆盖冲突**:当前目录存在名为`ternary.py`的文件,覆盖了库的导入 3. **环境残留问题**:旧版库文件未完全卸载导致功能异常 #### **2. 分步解决方案** **步骤 1:排查命名冲突** ```bash # 检查当前目录是否包含ternary.py ls -l | grep 'ternary.py' ``` 若存在同名文件,请重命名或删除后重启Python环境。 **步骤 2:验证库安装完整性** ```bash # 确认库名称及版本 pip show python-ternary | grep -E 'Name|Version' ``` 应显示: ``` Name: python-ternary Version: 1.0.8 ``` 若名称错误,执行: ```bash pip uninstall ternary # 卸载错误库 pip install python-ternary==1.0.8 ``` **步骤 3:尝试替代导入方式** ```python # 方式一:从子模块导入 from ternary.ternary_axes import TernaryAxesSubplot # 方式二:使用面向对象接口 import ternary fig, tax = ternary.plot.figure(scale=1.0) # 注意调用层级变化[^1] ``` **步骤 4:手动构建坐标系** ```python import matplotlib.pyplot as plt from ternary.ternary_axes import TernaryAxesSubplot fig = plt.figure(figsize=(8, 6)) tax = TernaryAxesSubplot(fig, 111, scale=1.0) tax.boundary(linewidth=1.0) tax.gridlines(multiple=0.2, color="blue") fig.add_subplot(tax) plt.show() ``` #### **3. 数学验证** 三元坐标系中点的归一化公式: $$ a' = \frac{a}{a+b+c}, \quad b' = \frac{b}{a+b+c}, \quad c' = \frac{c}{a+b+c} $$ 其中$(a,b,c)$为原始数据,归一化后满足$a'+b'+c'=1$,确保点位于标准三元坐标系内[^4] #### **4. 版本兼容性测试代码** ```python import ternary def test_figure_import(): try: from ternary import figure print("figure函数存在,导入成功") except ImportError: print("使用备用方案:") fig = ternary.plot.figure(scale=1.0) print("通过ternary.plot子模块访问成功") test_figure_import() ``` --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

One2zeror

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

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

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

打赏作者

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

抵扣说明:

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

余额充值