- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
所以场景是你认识的人给你一棵哈夫曼树,但它不是最优的(我知道所有哈夫曼树都是最优的,只是假设它不是最优的,但确实遵循只有叶子有值的哈夫曼风格)。
该函数应该在不改变树的实际“形状”的情况下,借助字典将每个符号映射到它在您正在压缩的假设文本中出现的次数,尽可能地改进树。该函数通过交换节点来实现这一点。所以最终的结果不一定是最优的树,但会尽可能地改进。例如......
Class Node:
def __init__(self, item = None, left = None, right = None):
self.item = item
self.left = left
self.right = right
def __repr__(self):
return 'Node({}, {}, {})'.format(self.item, self.left, self.right)
字典 = {54: 12, 101: 34, 29: 22, 65: 3, 20: 13}
你的 friend 给你...
节点(无,节点(无,节点(20),节点(54)),节点(无,节点(65),节点(无,节点(101),节点(29)))
或者...
None
/ | \
None | None
/ \ | / \
20 54 | 65 None
| / \
| 101 29
想要的结果是......
节点(无,节点(无,节点(20),节点(29)),节点(无,节点(101),节点(无,节点(65),节点(54)))
或者...
None
/ | \
None | None
/ \ | / \
20 29 | 101 None
| / \
| 65 54
如何定位叶节点,然后找到它应该在的位置,交换它,然后对所有其他叶节点执行此操作,同时确保树的形状相同,无论它是否是最佳的?这也是Python中的。
最佳答案
来自basic technique在构造哈夫曼树时,值概率最小的节点是第一个链接到父节点的节点。这些节点在霍夫曼树中出现得比其中的任何其他节点更深。由此,我们可以推断出这样一个事实:树越深入,遇到的值就越少。
这个类比对于开发优化函数至关重要,因为我们不需要执行各种交换,当我们可以通过以下方式第一次就正确时:获取树中按深度排序的所有项目的列表,并且它们的匹配值按顺序排列;当有叶子时,将它们插入各自的深度。这是我编码的解决方案:
def optimize_tree(tree, dictionary):
def grab_items(tree):
if tree.item:
return [tree.item]
else:
return grab_items(tree.left) + grab_items(tree.right)
def grab_depth_info(tree):
def _grab_depth_info(tree,depth):
if tree.item:
return {depth:1}
else:
depth_info_list = [_grab_depth_info(child,depth+1) for child in [tree.left, tree.right]]
depth_info = depth_info_list[0]
for depth in depth_info_list[1]:
if depth in depth_info:
depth_info[depth] += depth_info_list[1][depth]
else:
depth_info[depth] = depth_info_list[1][depth]
return depth_info
return _grab_depth_info(tree,0)
def make_inverse_dictionary(dictionary):
inv_dictionary = {}
for key in dictionary:
if dictionary[key] in inv_dictionary:
inv_dictionary[dictionary[key]].append(key)
else:
inv_dictionary[dictionary[key]] = [key]
for key in inv_dictionary:
inv_dictionary[key].sort()
return inv_dictionary
def get_depth_to_items(depth_info,actual_values):
depth_to_items = {}
for depth in depth_info:
depth_to_items[depth] = []
for i in range(depth_info[depth]):
depth_to_items[depth].append(actual_values[i])
depth_to_items[depth].sort()
del actual_values[:depth+1]
return depth_to_items
def update_tree(tree,depth_to_items,reference):
def _update_tree(tree,depth,depth_to_items,reference):
if tree.item:
tree.item = reference[depth_to_items[depth].pop(0)].pop(0)
else:
for child in [tree.left,tree.right]:
_update_tree(child,depth+1,depth_to_items,reference)
_update_tree(tree,0,depth_to_items,reference)
items = grab_items(tree)
depth_info = grab_depth_info(tree)
actual_values = [dictionary[item] for item in items]
actual_values.sort(reverse=True)
inv_dictionary = make_inverse_dictionary(dictionary)
depth_to_items = get_depth_to_items(depth_info,actual_values)
update_tree(tree,depth_to_items,inv_dictionary)
optimize_tree
函数要求用户传入两个参数:
tree
:哈夫曼树的根节点。字典
:将符号映射到其频率的字典。该函数首先定义四个内部函数:
grab_items
是一个函数,它接受树并返回其中所有项目的列表。grab_depth_info
返回一个字典,其中键是深度级别,值是该级别的节点数。make_inverse_dictionary
返回给定字典的逆字典。 (它可以处理值可以映射到两个键的情况。)get_depth_to_items
返回一个字典,其中键是深度级别,值是实际值列表(来自字典),这些值应该位于该级别以便优化树.update_tree
将项目插入到应有的位置,以便优化树。注意:grab_depth_info
和 update_tree
中定义了一个内部函数,以便它们的功能可以递归地工作。
以下算法需要这四个内部函数:
get_depth_to_items
函数,以获取深度级别字典到 inorder 值列表。update_tree
函数,该函数将使用其内部函数递归地访问树并使用倒排字典中的原始键更新项目属性。使用该算法的结果将使您传入的树处于最优化的状态,而不会改变它的实际形状。
<小时/>我可以通过执行以下代码行来确认这是否有效:
tree = Node(None, Node(None, Node(20), Node(29)), Node(None, Node(101), Node(None, Node(65), Node(54))))
dictionary = {54: 12, 101: 34, 29: 22, 65: 3, 20: 13}
optimize_tree(tree,dictionary)
print(tree)
其输出是:
Node(None, Node(None, Node(20, None, None), Node(29, None, None)), Node(None, Node(101, None, None), Node(None, Node(65, None, None), Node(54, None, None))))
关于python - 优化二叉树函数,霍夫曼树,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38918619/
关于 B 树与 B+ 树,网上有一个比较经典的问题:为什么 MongoDb 使用 B 树,而 MySQL 索引使用 B+ 树? 但实际上 MongoDb 真的用的是 B 树吗?
如何将 R* Tree 实现为持久(基于磁盘)树?保存 R* 树索引或保存叶值的文件的体系结构是什么? 注意:此外,如何在这种持久性 R* 树中执行插入、更新和删除操作? 注意事项二:我已经实现了一个
目前,我正在努力用 Java 表示我用 SML 编写的 AST 树,这样我就可以随时用 Java 遍历它。 我想知道是否应该在 Java 中创建一个 Node 类,其中包含我想要表示的数据,以及一个数
我之前用过这个库http://www.cs.umd.edu/~mount/ANN/ .但是,它们不提供范围查询实现。我猜是否有一个 C++ 范围查询实现(圆形或矩形),用于查询二维数据。 谢谢。 最佳
在进一步分析为什么MySQL数据库索引选择使用B+树之前,我相信很多小伙伴对数据结构中的树还是有些许模糊的,因此我们由浅入深一步步探讨树的演进过程,在一步步引出B树以及为什么MySQL数据库索引选择
操作系统的那棵“树” 今天从一颗 开始,我们看看如何从小树苗长成一颗苍天大树。 运转CPU CPU运转起来很简单,就是不断的从内存取值执行。 CPU没有好好运转 IO是个耗费时间的活,如果CPU在取值
我想为海洋生物学类(class)制作一个简单的系统发育树作为教育示例。我有一个具有分类等级的物种列表: Group <- c("Benthos","Benthos","Benthos","Be
我从这段代码中删除节点时遇到问题,如果我插入数字 12 并尝试删除它,它不会删除它,我尝试调试,似乎当它尝试删除时,它出错了树的。但是,如果我尝试删除它已经插入主节点的节点,它将删除它,或者我插入数字
B+ 树的叶节点链接在一起。将 B+ 树的指针结构视为有向图,它不是循环的。但是忽略指针的方向并将其视为链接在一起的无向叶节点会在图中创建循环。 在 Haskell 中,如何将叶子构造为父内部节点的子
我在 GWT 中使用树控件。我有一个自定义小部件,我将其添加为 TreeItem: Tree testTree = new Tree(); testTree.addItem(myWidget); 我想
它有点像混合树/链表结构。这是我定义结构的方式 struct node { nodeP sibling; nodeP child; nodeP parent; char
我编写了使用队列遍历树的代码,但是下面的出队函数生成错误,head = p->next 是否有问题?我不明白为什么这部分是错误的。 void Levelorder(void) { node *tmp,
例如,我想解析以下数组: var array1 = ["a.b.c.d", "a.e.f.g", "a.h", "a.i.j", "a.b.k"] 进入: var json1 = { "nod
问题 -> 给定一棵二叉树和一个和,确定该树是否具有从根到叶的路径,使得沿路径的所有值相加等于给定的和。 我的解决方案 -> public class Solution { public bo
我有一个创建 java 树的任务,它包含三列:运动名称、运动类别中的运动计数和上次更新。类似的东西显示在下面的图像上: 如您所见,有 4 种运动:水上运动、球类运动、跳伞运动和舞蹈运动。当我展开 sk
我想在 H2 数据库中实现 B+ Tree,但我想知道,B+ Tree 功能在 H2 数据库中可用吗? 最佳答案 H2 已经使用了 B+ 树(PageBtree 类)。 关于mysql - H2数据库
假设我们有 5 个字符串数组: String[] array1 = {"hello", "i", "cat"}; String[] array2 = {"hello", "i", "am"}; Str
我正在处理树。每个节点都有带有 Tree * 值的对象。我读取的数据如下所示: 1 2 2 ... 这意味着,将 1 作为 0 的子节点,将 2 作为 1 的子节点,将 3 作为 o 2 的子节点。在
我正在寻找一个好的 JavaScript 树/树网格包。现在——在你回答之前: 它需要能够在大量节点上正常运行。可能有 1,000 个兄弟节点。它需要能够在 2 或 3 秒内绘制到 1,000 个节点
下面的代码块究竟是如何工作的?更具体地说,程序如何知道返回哪个选项? return ancestor (node1->left(), node2) || ancestor
我是一名优秀的程序员,十分优秀!