简体   繁体   English

遍历霍夫曼树

[英]Traversing a Huffman Tree

So Currently I have a program that creates a huffman tree. 所以目前我有一个创建霍夫曼树的程序。 the tree is made up of "Hnodes" with these fields: right (points to right child) left (points to left child) code (string of integers, ideally the 0's and 1's that will be the huffman code of this node) character (the character contained in the node). 树由具有以下字段的“ Hnodes”组成:右(指向右子节点)左(指向左子节点)代码(整数字符串,理想情况下,此节点的霍夫曼代码为0和1)字符(节点中包含的字符)。

I have created the huffman tree by adding nodes from a linked list - i know the tree was created correctly. 我已经通过从链表中添加节点来创建了霍夫曼树-我知道树是正确创建的。 As i created the tree, i told the node when i gave it a parent node, that if it was the parent's "right", its code string was 1, if left 0. However obviously after the entire tree is created, each node is only going to have either a 0 or 1, but not yet a string like 00100101. My question is, now that I have this tree, can can I traverse it? 当我创建树时,我告诉节点当我给它一个父节点时,如果它是父节点的“右”,则其代码字符串为1,如果为0,则很明显。但是显然在创建整个树之后,每个节点都是只能有一个0或1,但还不能像00100101这样的字符串。我的问题是,现在我有了这棵树,可以遍历它吗? I understand the thought would be to give each child its parent's code + the child's own code, but I do not understand how to loop through the tree to accomplish this. 我知道这样的想法是给每个孩子自己的父母的代码+孩子自己的代码,但是我不知道如何遍历树来完成此操作。

Thank you in advance. 先感谢您。

Maybe this? 也许这个吗?

  ExpandBinaryPaths(node, prefix)
  1. if node is null then return 
  2. else then
  3.    node.binary = prefix concat node.binary
  4.    ExpandBinaryPaths(node.left, node.binary)
  5.    ExpandBinaryPaths(node.right, node.binary)
  6.    return

The idea is you would call this on the root with no prefix... ExpandBinaryPaths(root, ""). 想法是,您可以在不带前缀的根目录上调用它... ExpandBinaryPaths(root,“”)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM