Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- note
- description: "Haffman application root class"
- date: "$Date$"
- revision: "$Revision$"
- class
- APPLICATION
- inherit
- ARGUMENTS
- create
- make
- feature {NONE} -- Initialization
- arr:LINKED_PRIORITY_QUEUE[PAIR]
- freq:ARRAY[INTEGER]
- make
- -- Run application.
- local
- s:STRING
- t:PAIR
- do
- create freq.make_filled (0, 0, 255)
- s:="abcdff"
- set_freq(s)
- make_tree
- t:=arr.item
- generate_codes(t, "")
- end
- generate_codes(ch:PAIR; code:STRING)
- do
- if ch.char /= -1 then
- print(ch.char.to_character_8.out + " = " + code + "%N")
- end
- if attached ch.left as l then
- generate_codes (l, code + "0")
- end
- if attached ch.right as r then
- generate_codes (r, code + "1")
- end
- end
- set_freq(s:STRING)
- do
- across
- s as char
- loop
- freq[char.item.code] := freq[char.item.code] + 1
- end
- end
- make_tree
- local
- t,t1,t2:PAIR
- i:INTEGER
- do
- create arr.make
- from
- i:=0
- until
- i>255
- loop
- if freq[i] > 0 then
- create t.make (i, freq[i])
- arr.extend (t)
- end
- i:= i + 1
- end
- from
- until
- arr.count = 1
- loop
- t := arr.item.twin
- arr.remove
- t1 := arr.item.twin
- arr.remove
- create t2.make (-1, t.cnt+t1.cnt)
- t2.set_left (t.char, t.cnt)
- t2.set_right (t1.char, t1.cnt)
- arr.extend (t2.twin)
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement