1
- using Test
2
-
3
1
# This is for the PriorityQueue
4
2
using DataStructures
5
3
@@ -15,6 +13,8 @@ struct Branch
15
13
end
16
14
17
15
const Node = Union{Leaf, Branch}
16
+ isbranch (branch:: Branch ) = true
17
+ isbranch (other:: T ) where {T} = false
18
18
19
19
function codebook_recurse! (leaf:: Leaf , code:: String ,
20
20
dict:: Dict{Char,String} )
33
33
# This outputs encoding Dict to be used for encoding
34
34
function create_codebook (n:: Node )
35
35
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)
41
37
return codebook
42
38
end
43
39
@@ -89,19 +85,14 @@ function decode(huffman_tree::Node, bitstring::String)
89
85
current = huffman_tree
90
86
final_string = " "
91
87
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
103
90
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
105
96
end
106
97
end
107
98
@@ -116,8 +107,4 @@ function two_pass_huffman(phrase::String)
116
107
return final_string
117
108
end
118
109
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