Skip to content

Commit 0bf5059

Browse files
committed
Revert "fixing huffman encoding for Julia and adding Test"
This reverts commit 558a57a.
1 parent c6257fa commit 0bf5059

File tree

1 file changed

+11
-24
lines changed

1 file changed

+11
-24
lines changed

Diff for: contents/huffman_encoding/code/julia/huffman.jl

+11-24
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
using Test
2-
31
# This is for the PriorityQueue
42
using DataStructures
53

@@ -15,6 +13,8 @@ struct Branch
1513
end
1614

1715
const Node = Union{Leaf, Branch}
16+
isbranch(branch::Branch) = true
17+
isbranch(other::T) where {T} = false
1818

1919
function codebook_recurse!(leaf::Leaf, code::String,
2020
dict::Dict{Char,String})
@@ -33,11 +33,7 @@ end
3333
# This outputs encoding Dict to be used for encoding
3434
function 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
4238
end
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
117108
end
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

Comments
 (0)