Analisis Perbandingan Algoritma Huffman Dan Algoritma Sequtur Dalam Kompresi Data Text
A- 1 -
LISTING PROGRAM KOMPRESI DAN DEKOMPRESI
Kompresi Huffman
Imports System.IO
Public Class frmK_Huffman
Public Stopwatch As Stopwatch = New Stopwatch
Public Log As String
Public Pjg_PT, Pjg_CT As Integer
Public Panjang_KS, JlhBit_PT, JlhBit_CT As Integer
Public Karakter_Set As String
Public THuff As List(Of clsHuff)
Public PT, CT, PTB, CTB As String
Public PBytes(), CBytes() As Byte
Public Input, Output As String
' ======================================================== FUNGSI UMUM
======================================================== '
Public Sub StopWatchStart()
Stopwatch.Reset()
Stopwatch.Start()
Cursor.Current = Cursors.WaitCursor
txtWaktuProses.Text = ""
End Sub
Public Sub StopWatchStop()
Stopwatch.Stop()
txtWaktuProses.Text = Stopwatch.Elapsed.ToString()
Cursor.Current = Cursors.Arrow
End Sub
LISTING PROGRAM
Public Function toBiner(ByVal desimal As Integer) As String
Dim biner As String = ""
Dim buf As String = ""
Dim hasilBagi As Integer = desimal
Dim p, sisa As Integer
While (hasilBagi > 1)
If (hasilBagi Mod 2 = 1) Then
hasilBagi -= 1
hasilBagi = hasilBagi / 2
biner = "1" & biner
Else
hasilBagi = hasilBagi / 2
biner = "0" & biner
End If
End While
biner = hasilBagi & biner
p = biner.Length / 8
sisa = (8 - (biner.Length Mod 8)) Mod 8
For i = 1 To sisa
buf &= "0"
Next
biner = buf & biner
Universitas Sumatera Utara
A- 2 -
Return biner
End Function
Public Function toDecimal(ByVal biner As String) As Integer
Dim result As Integer = 0
Dim num As Integer = 0
For i = 1 To biner.Length
num = Convert.ToInt16(biner(i - 1)) - 48
If num 0 Then
result += Convert.ToInt16(Math.Pow(2, biner.Length - i)) * num
End If
Next
Return result
End Function
' ======================================================== HUFFMAN
======================================================== '
Public Function BuatKarakterSet(ByVal Teks As String) As String
Dim R As String = ""
Dim Yes As Boolean
For i As Integer = 0 To Teks.Length - 1
Yes = False
For j As Integer = 0 To R.Length - 1
If Teks(i) = R(j) Then
Yes = True
Continue For
End If
Next
If Not Yes Then
R &= Teks(i)
End If
Next
Return R
End Function
Public Function HitungFreqKarakter(ByVal Teks As String, ByVal KS As String) As
List(Of clsHuff)
Dim R As List(Of clsHuff) = New List(Of clsHuff)
Dim T As Integer
For j As Integer = 0 To KS.Length - 1
T = 0
For i As Integer = 0 To Teks.Length - 1
If KS(j) = Teks(i) Then
T += 1
End If
Next
R.Add(New clsHuff(KS(j), T))
Next
Return R
End Function
Public Function UrutkanKS(ByVal L As List(Of clsHuff)) As List(Of clsHuff)
Dim Kar As Char
Dim Freq As Integer
Universitas Sumatera Utara
A- 3 -
For j As Integer = 0 To L.Count - 2
For i As Integer = j + 1 To L.Count - 1
If L(j).Freq > L(i).Freq Then
Kar = L(j).Kar
Freq = L(j).Freq
L(j).Kar = L(i).Kar
L(j).Freq = L(i).Freq
L(i).Kar = Kar
L(i).Freq = Freq
End If
Next
Next
Return L
End Function
Public Function HuffmanTree(ByVal L As List(Of clsHuff)) As List(Of clsHuff)
Dim i As Integer = 0
Dim Freq As Integer
Dim Index As Integer
Dim L2 As List(Of clsHuff) = New List(Of clsHuff)
While (i + 2 L(i).Freq Then
Kar = L(j).Kar
Freq = L(j).Freq
Universitas Sumatera Utara
A- 17 -
L(j).Kar = L(i).Kar
L(j).Freq = L(i).Freq
L(i).Kar = Kar
L(i).Freq = Freq
End If
Next
Next
Return L
End Function
Public Function HuffmanTree(ByVal L As List(Of clsHuff)) As List(Of clsHuff)
Dim i As Integer = 0
Dim Freq As Integer
Dim Index As Integer
Dim L2 As List(Of clsHuff) = New List(Of clsHuff)
While (i + 2
LISTING PROGRAM KOMPRESI DAN DEKOMPRESI
Kompresi Huffman
Imports System.IO
Public Class frmK_Huffman
Public Stopwatch As Stopwatch = New Stopwatch
Public Log As String
Public Pjg_PT, Pjg_CT As Integer
Public Panjang_KS, JlhBit_PT, JlhBit_CT As Integer
Public Karakter_Set As String
Public THuff As List(Of clsHuff)
Public PT, CT, PTB, CTB As String
Public PBytes(), CBytes() As Byte
Public Input, Output As String
' ======================================================== FUNGSI UMUM
======================================================== '
Public Sub StopWatchStart()
Stopwatch.Reset()
Stopwatch.Start()
Cursor.Current = Cursors.WaitCursor
txtWaktuProses.Text = ""
End Sub
Public Sub StopWatchStop()
Stopwatch.Stop()
txtWaktuProses.Text = Stopwatch.Elapsed.ToString()
Cursor.Current = Cursors.Arrow
End Sub
LISTING PROGRAM
Public Function toBiner(ByVal desimal As Integer) As String
Dim biner As String = ""
Dim buf As String = ""
Dim hasilBagi As Integer = desimal
Dim p, sisa As Integer
While (hasilBagi > 1)
If (hasilBagi Mod 2 = 1) Then
hasilBagi -= 1
hasilBagi = hasilBagi / 2
biner = "1" & biner
Else
hasilBagi = hasilBagi / 2
biner = "0" & biner
End If
End While
biner = hasilBagi & biner
p = biner.Length / 8
sisa = (8 - (biner.Length Mod 8)) Mod 8
For i = 1 To sisa
buf &= "0"
Next
biner = buf & biner
Universitas Sumatera Utara
A- 2 -
Return biner
End Function
Public Function toDecimal(ByVal biner As String) As Integer
Dim result As Integer = 0
Dim num As Integer = 0
For i = 1 To biner.Length
num = Convert.ToInt16(biner(i - 1)) - 48
If num 0 Then
result += Convert.ToInt16(Math.Pow(2, biner.Length - i)) * num
End If
Next
Return result
End Function
' ======================================================== HUFFMAN
======================================================== '
Public Function BuatKarakterSet(ByVal Teks As String) As String
Dim R As String = ""
Dim Yes As Boolean
For i As Integer = 0 To Teks.Length - 1
Yes = False
For j As Integer = 0 To R.Length - 1
If Teks(i) = R(j) Then
Yes = True
Continue For
End If
Next
If Not Yes Then
R &= Teks(i)
End If
Next
Return R
End Function
Public Function HitungFreqKarakter(ByVal Teks As String, ByVal KS As String) As
List(Of clsHuff)
Dim R As List(Of clsHuff) = New List(Of clsHuff)
Dim T As Integer
For j As Integer = 0 To KS.Length - 1
T = 0
For i As Integer = 0 To Teks.Length - 1
If KS(j) = Teks(i) Then
T += 1
End If
Next
R.Add(New clsHuff(KS(j), T))
Next
Return R
End Function
Public Function UrutkanKS(ByVal L As List(Of clsHuff)) As List(Of clsHuff)
Dim Kar As Char
Dim Freq As Integer
Universitas Sumatera Utara
A- 3 -
For j As Integer = 0 To L.Count - 2
For i As Integer = j + 1 To L.Count - 1
If L(j).Freq > L(i).Freq Then
Kar = L(j).Kar
Freq = L(j).Freq
L(j).Kar = L(i).Kar
L(j).Freq = L(i).Freq
L(i).Kar = Kar
L(i).Freq = Freq
End If
Next
Next
Return L
End Function
Public Function HuffmanTree(ByVal L As List(Of clsHuff)) As List(Of clsHuff)
Dim i As Integer = 0
Dim Freq As Integer
Dim Index As Integer
Dim L2 As List(Of clsHuff) = New List(Of clsHuff)
While (i + 2 L(i).Freq Then
Kar = L(j).Kar
Freq = L(j).Freq
Universitas Sumatera Utara
A- 17 -
L(j).Kar = L(i).Kar
L(j).Freq = L(i).Freq
L(i).Kar = Kar
L(i).Freq = Freq
End If
Next
Next
Return L
End Function
Public Function HuffmanTree(ByVal L As List(Of clsHuff)) As List(Of clsHuff)
Dim i As Integer = 0
Dim Freq As Integer
Dim Index As Integer
Dim L2 As List(Of clsHuff) = New List(Of clsHuff)
While (i + 2