java - Traversing a Huffman Tree -
so have program creates huffman tree. tree made of "hnodes" these fields: right (points right child) left (points left child) code (string of integers, ideally 0's , 1's huffman code of node) character (the character contained in node).
i have created huffman tree adding nodes linked list - know tree created correctly. created tree, told node when gave parent node, if parent's "right", code string 1, if left 0. after entire tree created, each node going have either 0 or 1, not yet string 00100101. question is, have tree, can can traverse it? understand thought give each child parent's code + child's own code, not understand how loop through tree accomplish this.
thank in advance.
maybe this?
expandbinarypaths(node, prefix) 1. if node null return 2. else 3. node.binary = prefix concat node.binary 4. expandbinarypaths(node.left, node.binary) 5. expandbinarypaths(node.right, node.binary) 6. return
the idea call on root no prefix... expandbinarypaths(root, "").
Comments
Post a Comment