1- using Test
2-
31# This is for the PriorityQueue
42using DataStructures
53
@@ -15,6 +13,8 @@ struct Branch
1513end
1614
1715const Node = Union{Leaf, Branch}
16+ isbranch (branch:: Branch ) = true
17+ isbranch (other:: T ) where {T} = false
1818
1919function codebook_recurse! (leaf:: Leaf , code:: String ,
2020 dict:: Dict{Char,String} )
3333# This outputs encoding Dict to be used for encoding
3434function create_codebook (n:: Node )
3535 codebook = Dict {Char,String} ()
36- if isa (n, Leaf)
37- codebook[n. key]= " 0"
38- else
39- codebook_recurse! (n, " " , codebook)
40- end
36+ codebook_recurse! (n, " " , codebook)
4137 return codebook
4238end
4339
@@ -89,19 +85,14 @@ function decode(huffman_tree::Node, bitstring::String)
8985 current = huffman_tree
9086 final_string = " "
9187 for i in bitstring
92- if isa (huffman_tree, Branch)
93- if (i == ' 1' )
94- current = current. left
95- else
96- current = current. right
97- end
98-
99- if (! isa (current, Branch))
100- final_string *= string (current. key)
101- current = huffman_tree
102- end
88+ if (i == ' 1' )
89+ current = current. left
10390 else
104- final_string *= string (huffman_tree. key)
91+ current = current. right
92+ end
93+ if (! isbranch (current))
94+ final_string = final_string * string (current. key)
95+ current = huffman_tree
10596 end
10697 end
10798
@@ -116,8 +107,4 @@ function two_pass_huffman(phrase::String)
116107 return final_string
117108end
118109
119- @testset " b-string tests" begin
120- @test two_pass_huffman (" b" ) == " b"
121- @test two_pass_huffman (" bbbbbbbb" ) == " bbbbbbbb"
122- @test two_pass_huffman (" bibbity bobbity" ) == " bibbity bobbity"
123- end
110+ two_pass_huffman (" bibbity bobbity" )
0 commit comments