Struktur
Data
Kasus 80: Pengurutan Bubble
Public Class
Form1
'Variabel-variabel global
Dim arrayKu As New ArrayList
Dim kounter As Integer
= 1
Dim strArray As String
Private Sub
PengurutanBubble()
Dim luar, dalam, temp As
Integer
For luar = arrayKu.Count - 1 To
1 Step -1
For dalam = 0 To
luar - 1
'jika angka pertama lebih
besar dari angka kedua
If
(CInt(arrayKu(dalam)) > CInt(arrayKu(dalam + 1))) Then
'maka melewatkan angka
pertama kepada temp
temp =
CInt(arrayKu(dalam))
'angka kedua menggantikan
angka pertama
arrayKu(dalam) =
arrayKu(dalam + 1)
'menggeser angka pertama
untuk menempati angka kedua
arrayKu(dalam + 1) = temp
End If
'menampilkan tiap baris array
pada kotak list
strArray = ""
For i As Integer
= 0 To arrayKu.Count - 1
strArray +=
CInt(arrayKu(i)) & " "
kounter += 1
Next
kotakList.Items.Add("Inner " & dalam & ", Outer
" & luar & ", " & strArray)
Next
Next
End Sub
Private Sub
tombolUrut_Click(sender As Object, e As EventArgs)
Handles tombolUrut.Click
MuatArray()
PengurutanBubble()
kotakList.Items.Add("======HASIL
PENGURUTAN======")
'membersihkan array
arrayKu.Clear()
End Sub
Private Sub MuatArray()
'Menciptakan array dengan isi
angka-angka acak
Dim kounter As Integer
= 0
Do While kounter < 6
arrayKu.Add(BacaAcak(1, 11))
kounter += 1
Loop
End Sub
Public Function BacaAcak(ByVal Min As Integer, ByVal Maks As
Integer) As Integer
Static Generator As
System.Random = New System.Random()
Return Generator.Next(Min,
Maks)
End Function
End Class
|
Kasus 81: Pengurutan Seleksi
Public Class
Form1
'Variabel-variabel global
Dim arrayKu As New ArrayList
Dim strArray As String
Dim luar, dalam, temp As Integer
Public Sub
PengurutanSeleksi()
'Loop luar
bergerak dari elemen pertama ke elemen berikutnya sampai elemen terakhir
For luar = 0 To
arrayKu.Count - 2
'loop dalam bergerak dari elemen
kedua array ke elemen terakhir,
'mencari
nilai-nilai yang lebih kecil dari elemen yang sedang ditunjuk oleh
'loop
luar. Pada tiap iterasi pada loop dalam, nilai yang paling minimum
'pada array ditugaskan pada
tempat yang sesuai.
For dalam = luar + 1 To
arrayKu.Count - 1
If
(CInt(arrayKu(dalam)) < CInt(arrayKu(luar))) Then
'Ya lebih kecil, maka
kedua angka saling ditukar
temp =
CInt(arrayKu(luar))
arrayKu(luar) =
arrayKu(dalam)
arrayKu(dalam) = temp
TampilTukar()
End If
TampilTanpaTukar()
Next
Next
End Sub
Private Sub
TampilTanpaTukar()
'Menampilkan
strArray = ""
For i As Integer
= 0 To arrayKu.Count - 1
strArray += CInt(arrayKu(i))
& " "
Next
kotakList.Items.Add("Outer (" & luar & ") " &
", Inner (" & dalam & ") " &
strArray
& " " & CInt(arrayKu(luar)) & " lebih kecil
dari " &
CInt(arrayKu(dalam)))
End Sub
Private Sub TampilTukar()
'Menampilkan
strArray = ""
For i As Integer
= 0 To arrayKu.Count - 1
strArray += CInt(arrayKu(i))
& " "
Next
kotakList.Items.Add("Outer
(" & luar & ") " & ", Inner ("
& dalam & ") " &
strArray & " " & CInt(arrayKu(dalam)) & " lebih
besar dari " &
CInt(arrayKu(luar)) & " Tukar! ")
End Sub
Private Sub
tombolUrut_Click(sender As Object, e As EventArgs)
Handles tombolUrut.Click
kotakList.Items.Clear()
MuatArray()
PengurutanSeleksi()
'membersihkan array untuk penggunaan
berikutnya
arrayKu.Clear()
End Sub
Private Sub MuatArray()
'menciptakan array dengan isi
angka-angka acak
Dim kounter As Integer
= 0
Do While kounter < 6
arrayKu.Add(BacaAcak(1, 11))
kounter += 1
Loop
End Sub
Public Function BacaAcak(ByVal Min As Integer, ByVal Maks As Integer)
As Integer
Static Generator As
System.Random = New System.Random()
Return Generator.Next(Min,
Maks)
End Function
End Class
|
Kasus 82: Pencarian Sekuensial
Public Class
Form1
Dim arrayKu As New ArrayList
Dim angkaAcak As Integer
Private Sub tombolCari_Click(sender As
Object, e As EventArgs)
Handles tombolCari.Click
kotakList.Items.Clear()
' Menginisialisasi pembangkit angka
acak
Randomize()
MuatArray()
' Menghitung sebuah angka aca antara
1 dan 10
angkaAcak = CInt(1 + 10 * Rnd())
Dim strArray As String
For i = 0 To arrayKu.Count - 1
'string memuat angka-angka acak
untuk ditampilkan
strArray += CStr(arrayKu(i))
& " "
'menunjukkan jika ada kecocokan
If CInt(arrayKu(i)) = angkaAcak
Then
kotakList.Items.Add("Angka acak " & angkaAcak & " ada di dalam Array")
End If
Next
kotakList.Items.Add("====HASIL
====")
kotakList.Items.Add("Angka acak
adalah " & angkaAcak)
kotakList.Items.Add("Array
adalah " & strArray)
arrayKu.Clear()
End Sub
Private Sub MuatArray()
'Menciptakan array yang memuat
angka-angka acak
Dim kounter As Integer
= 0
Do While kounter <
26
arrayKu.Add(BacaAcak(1, 11))
kounter += 1
Loop
End Sub
Public Function BacaAcak(ByVal Min As Integer, ByVal Maks As
Integer) As Integer
Static Pembangkit As
System.Random = New System.Random()
Return Pembangkit.Next(Min,
Maks)
End Function
End Class
|
Kasus 83: Pencarian Sekuensial Yang
Lebih Cepat
Public Class
Form1
Dim arrayKu As New ArrayList
Dim angkaAcak As Integer
Dim nomorPosisi As Integer
= 0
Private Sub
tombolCari_Click(sender As Object, e As EventArgs)
Handles tombolCari.Click
kotakList.Items.Clear()
' Menginisialisasi pembangkit angka
acak
Randomize()
MuatArray()
' Menghitung sebuah angka aca antara
1 dan 10
angkaAcak = CInt(1 + 10 *
Rnd())
Dim strArray As String
For i = 0 To arrayKu.Count -
1
'string memuat angka-angka acak
untuk ditampilkan
strArray += CStr(arrayKu(i))
& " "
'menunjukkan jika ada kecocokan
If CInt(arrayKu(i)) =
angkaAcak Then
kotakList.Items.Add("Angka acak " & angkaAcak & " ada di dalam Array")
End If
'Menukar angka-angka pada array
Tukar(i)
Next
kotakList.Items.Add("====HASIL
====")
kotakList.Items.Add("Angka
acak adalah " & angkaAcak)
kotakList.Items.Add("Array
adalah " & strArray)
arrayKu.Clear()
End Sub
Private Sub Tukar(ByRef
cocok As Integer)
Dim temp As Integer
temp = CInt(arrayKu(cocok))
arrayKu(cocok) = arrayKu(nomorPosisi)
arrayKu(nomorPosisi) = temp
'memberi ruang baru
nomorPosisi += 1
'menampilkan array baru hasil
penukaran
Dim strArray As String
For i = 0 To
arrayKu.Count - 1
'string memuat angka-angka untuk
ditampilkan
strArray += CInt(arrayKu(i))
& " "
Next
kotakList.Items.Add("Array
baru hasil penukaran adalah " & strArray)
End Sub
Private Sub MuatArray()
'Menciptakan array yang memuat
angka-angka acak
Dim kounter As Integer
= 0
Do While kounter <
26
arrayKu.Add(BacaAcak(1, 11))
kounter += 1
Loop
End Sub
Public Function BacaAcak(ByVal Min As Integer, ByVal Maks As
Integer) As Integer
Static Pembangkit As
System.Random = New System.Random()
Return Pembangkit.Next(Min,
Maks)
End Function
End Class
|
Kasus 84: Pengurutan Quick
Public Class
Form1
Dim arrayKu As New
ArrayList
Public Sub
PengurutanQuick()
'0 = elemen pertama pada array,
elemen terakhir pada array
PengurutanQuickRekursif(0,
arrayKu.Count - 1)
End Sub
Public Sub
PengurutanQuickRekursif(ByVal pertama As Integer, ByVal
terakhir
As Integer)
'loop rekursif untuk membagi array
If ((terakhir - pertama) <=
0) Then
Return
Else
'membuat sebuah pivot dari entri
terakhir pada array
Dim pivot As Integer
= CInt(arrayKu(terakhir))
Dim part As Integer
= Me.Partisi(pertama, terakhir)
'pengurutan Quick secara rekursif
PengurutanQuickRekursif(pertama,
part - 1)
PengurutanQuickRekursif(part + 1,
terakhir)
End If
End Sub
Public Function Partisi(ByVal
pertama As Integer, ByVal terakhir As Integer)
As Integer
'mendapatkan pivot
Dim nilaiPivot As Integer
= CInt(arrayKu(pertama))
'kotakList.Items.Add("Nilai
pivot adalah " & nilaiPivot)
Dim ygPertama As Integer
= pertama
Dim okSisi As Boolean
'meenjelajah array
pertama += 1
Do
'menetapkan True
okSisi = True
While (okSisi) 'selama
True
'jika elemen pertama lebih dari nilai pivot
If
(CInt(arrayKu(pertama)) > nilaiPivot) Then
okSisi = False
Else
pertama += 1
okSisi = (pertama <=
terakhir) 'true
End If
End While
okSisi = (pertama <= terakhir)
While (okSisi) 'selama
True
If
(CInt(arrayKu(terakhir)) <= nilaiPivot) Then
okSisi = False
Else
terakhir -= 1
okSisi = (pertama <=
terakhir)
End If
End While
'mesin utama untuk menukar
elemen-elemen
If (pertama < terakhir)
Then
Tukar(pertama, terakhir)
Tampil()
pertama += 1
terakhir -= 1
End If
Loop While (pertama
<= terakhir)
Tukar(ygPertama, terakhir)
Tampil()
Return terakhir
End Function
Public Sub Tukar(ByVal
item1 As Integer, ByVal item2 As Integer)
Dim temp As Integer
= CInt(arrayKu(item1))
arrayKu(item1) = arrayKu(item2)
arrayKu(item2) = temp
End Sub
Sub Tampil()
'menampilkan array baru hasil
penukaran
Dim strArray As String
For i = 0 To
arrayKu.Count - 1
'string memuat angka-angka untuk
ditampilkan
strArray += CStr(arrayKu(i))
& " "
Next
kotakList.Items.Add("Array
hasil penukaran: " & strArray)
End Sub
Private Sub
tombolUrut_Click(sender As Object, e As EventArgs)
Handles tombolUrut.Click
kotakList.Items.Clear()
' Menginisialisasi pembangkit angka
acak
Randomize()
MuatArray()
PengurutanQuick()
End Sub
Private Sub MuatArray()
'Menciptakan array yang memuat
angka-angka acak
Dim kounter As Integer
= 0
Do While kounter <
26
arrayKu.Add(BacaAcak(1, 11))
kounter += 1
Loop
End Sub
Public Function BacaAcak(ByVal
Min As Integer, ByVal Maks As Integer)
As Integer
Static Pembangkit As
System.Random = New System.Random()
Return Pembangkit.Next(Min,
Maks)
End Function
End Class
|
Kasus 85: Pengurutan Penyisipan
Public Class
Form1
'Variabel-variabel global
Dim arrayKu As New ArrayList
Dim strArray As String
Dim luar, dalam, temp As Integer
Dim kounter As Integer
= 1
Private Sub
tombolUrut_Click(sender As Object, e As EventArgs)
Handles tombolUrut.Click
MuatArray()
kotakList.Items.Add("======HASIL
PENGURUTAN======")
PengurutanPenyisipan()
'membersihkan array
arrayKu.Clear()
End Sub
Private Sub PengurutanPenyisipan()
Dim dalam, luar, temp As
Integer
For luar = 1 To arrayKu.Count - 1
temp = CInt(arrayKu(luar))
dalam = luar
While (dalam > 0 AndAlso
(CInt(arrayKu(dalam - 1)) >= temp))
arrayKu(dalam) =
arrayKu(dalam - 1)
dalam -= 1
End While
arrayKu(dalam) = temp
'menampilkan tiap baris array
pada kotak list
strArray = ""
For i As Integer = 0 To
arrayKu.Count - 1
strArray += CInt(arrayKu(i))
& " "
kounter += 1
Next
kotakList.Items.Add("Inner " & dalam & ", Outer
" & luar & ", " & strArray)
Next
End Sub
Private Sub MuatArray()
'Menciptakan array dengan isi
angka-angka acak
Dim kounter As Integer
= 0
Do While kounter <
26
arrayKu.Add(BacaAcak(1, 99))
kounter += 1
Loop
End Sub
Public Function BacaAcak(ByVal
Min As Integer, ByVal Maks As Integer)
As Integer
Static Generator As
System.Random = New System.Random()
Return Generator.Next(Min,
Maks)
End Function
End Class
|
Kasus 86: Pencarian Biner
Public Class
Form1
Dim arrayKu As New ArrayList
Dim angkaAcak As Integer
Private Sub
tombolCari_Click(sender As Object, e As EventArgs)
Handles tombolCari.Click
kotakList.Items.Clear()
' Menginisialisasi pembangkit angka
acak
Randomize()
MuatArray()
' Menghitung sebuah angka aca antara
1 dan 10
angkaAcak = CInt(1 + 99 * Rnd())
Dim strArray As String
Dim indeksPencarian As Integer
For i = 0 To
arrayKu.Count - 1
'string memuat angka-angka acak
untuk ditampilkan
strArray += CStr(arrayKu(i))
& " "
Next
kotakList.Items.Add("====HASIL====")
kotakList.Items.Add("Angka
acak adalah " & angkaAcak)
kotakList.Items.Add("Array
adalah " & strArray)
indeksPencarian =
pencarianBiner(angkaAcak)
If indeksPencarian = -1 Then
kotakList.Items.Add("Angka acak " & angkaAcak & " tidak ada di dalam
Array")
Else
kotakList.Items.Add("Angka acak " & angkaAcak & " ada di dalam Array" &
"pada indeks
" & indeksPencarian)
End If
arrayKu.Clear()
End Sub
Private Sub MuatArray()
'Menciptakan array yang memuat
angka-angka acak
Dim kounter As Integer
= 0
Do While kounter <
26
arrayKu.Add(BacaAcak(1, 99))
kounter += 1
Loop
End Sub
Public Function BacaAcak(ByVal
Min As Integer, ByVal Maks As Integer)
As Integer
Static Pembangkit As
System.Random = New System.Random()
Return Pembangkit.Next(Min,
Maks)
End Function
Public Function
pencarianBiner(ByVal nilai As Integer) As Integer
Dim batasAtas, batasBawah,
tengah As Integer
batasAtas = arrayKu.Count - 1
batasBawah = 0
While (batasBawah <=
batasAtas)
tengah = (batasAtas + batasBawah)
\ 2
If (CInt(arrayKu(tengah))
= nilai) Then
Return tengah
ElseIf (nilai <
CInt(arrayKu(tengah))) Then
batasAtas = tengah - 1
Else
batasBawah = tengah + 1
End If
End While
Return -1
End Function
End Class
|
Kasus 87: Pencarian Biner Rekursif
Public Class
Form1
Dim arrayKu As New ArrayList
Dim angkaAcak As Integer
Private Sub
tombolCari_Click(sender As Object, e As EventArgs)
Handles tombolCari.Click
kotakList.Items.Clear()
' Menginisialisasi pembangkit angka
acak
Randomize()
MuatArray()
' Menghitung sebuah angka aca antara
1 dan 25
angkaAcak = CInt(1 + 25 *
Rnd())
Dim strArray As String
Dim indeksPencarian As Integer
For i = 0 To
arrayKu.Count - 1
'string memuat angka-angka acak
untuk ditampilkan
strArray += CStr(arrayKu(i))
& " "
Next
kotakList.Items.Add("====HASIL====")
kotakList.Items.Add("Angka acak
adalah " & angkaAcak)
kotakList.Items.Add("Array
adalah " & strArray)
indeksPencarian =
pencarianBinerRekursif(angkaAcak, 0, arrayKu.Count - 1)
If
CInt(arrayKu(indeksPencarian)) <> angkaAcak Then
kotakList.Items.Add("Angka acak " & angkaAcak & " tidak ada di dalam
Array")
Else
kotakList.Items.Add("Angka
acak " & angkaAcak & " ada di dalam Array"
&
" pada indeks
" & indeksPencarian)
End If
arrayKu.Clear()
End Sub
Private Sub MuatArray()
'Menciptakan array yang memuat
angka-angka acak
Dim kounter As Integer
= 0
Do While kounter <
26
arrayKu.Add(BacaAcak(1, 26))
kounter += 1
Loop
End Sub
Public Function BacaAcak(ByVal
Min As Integer, ByVal Maks As Integer)
As Integer
Static Pembangkit As
System.Random = New System.Random()
Return Pembangkit.Next(Min,
Maks)
End Function
Public Function
pencarianBinerRekursif(ByVal nilai As Integer, ByVal
bawah As
Integer, ByVal
atas As Integer) As Integer
If (bawah > atas) Then
Return -1
ElseIf (bawah < atas) Then
Dim tengah As Integer
tengah = CInt((atas + bawah) / 2)
If (nilai < CInt(arrayKu(tengah)))
Then
pencarianBinerRekursif(nilai,
bawah, tengah - 1)
ElseIf (nilai = CInt(arrayKu(tengah)))
Then
Return tengah
Else
pencarianBinerRekursif(nilai,
tengah + 1, atas)
End If
End If
End Function
End Class
|
Kasus 88: Pengurutan Shell
Public Class
Form1
'Variabel-variabel global
Dim arrayKu As New ArrayList
Dim strArray As String
Dim luar, dalam, temp As Integer
Dim kounter As Integer
= 1
Private Sub
tombolUrut_Click(sender As Object, e As EventArgs)
Handles tombolUrut.Click
MuatArray()
kotakList.Items.Add("======HASIL
PENGURUTAN======")
PengurutanShell()
'membersihkan array
arrayKu.Clear()
End Sub
Public
Sub PengurutanShell()
Dim dalam, luar, temp As Integer
Dim h As Integer = 1
While (h <= arrayKu.Count / 3)
h = h * 3 + 1
End While
While (h > 0)
For luar = h To
arrayKu.Count - 1
temp = CInt(arrayKu(luar))
dalam = luar
While (dalam > h - 1 AndAlso CInt(arrayKu(dalam - h)) >= temp)
arrayKu(dalam) =
arrayKu(dalam - h)
dalam -= h
End While
arrayKu(dalam) = temp
'menampilkan tiap baris array
pada kotak list
strArray = ""
For i As Integer
= 0 To arrayKu.Count - 1
strArray +=
CInt(arrayKu(i)) & " "
kounter += 1
Next
kotakList.Items.Add("Inner " & dalam & ", Outer
" & luar & ", " & strArray)
Next
h = CInt((h - 1) / 3)
End While
End
Sub
Private Sub MuatArray()
'Menciptakan array dengan isi
angka-angka acak
Dim kounter As Integer
= 0
Do While kounter <
26
arrayKu.Add(BacaAcak(1, 99))
kounter += 1
Loop
End Sub
Public Function BacaAcak(ByVal
Min As Integer, ByVal Maks As Integer)
As Integer
Static Generator As
System.Random = New System.Random()
Return Generator.Next(Min,
Maks)
End Function
End Class
|
Kasus 89: Pengurutan Merge
Public Class
Form1
'Variabel-variabel global
Dim arrayKu As New ArrayList
Dim strArray As String
Dim luar, dalam, temp As Integer
Dim kounter As Integer
= 1
Dim tempArray As New
ArrayList
Private Sub
tombolUrut_Click(sender As Object, e As EventArgs)
Handles tombolUrut.Click
MuatArray()
kotakList.Items.Add("======HASIL
PENGURUTAN======")
pengurutanMerge()
'membersihkan array
arrayKu.Clear()
End Sub
Public Sub
pengurutanMerge()
Dim tempArray(arrayKu.Count) As
Integer
recMergeSort(tempArray, 0,
arrayKu.Count - 1)
End Sub
Public Sub recMergeSort(ByVal
tempArray() As Integer, ByVal lbound As Integer,
ByVal
ubound As Integer)
If (lbound = ubound) Then
Return
Else
Dim mid As Integer
= (lbound + ubound) \ 2
recMergeSort(tempArray, lbound,
mid)
recMergeSort(tempArray, mid + 1,
ubound)
merge(tempArray, lbound, mid + 1,
ubound)
'menampilkan tiap baris array
pada kotak list
strArray = ""
For i As Integer
= 0 To arrayKu.Count - 1
strArray += CInt(arrayKu(i))
& " "
kounter += 1
Next
kotakList.Items.Add("Inner " & dalam & ", Outer " & luar &
", " & strArray)
End If
End Sub
Public Sub merge(ByVal
tempArray() As Integer, ByVal lowp As Integer,
ByVal highp As Integer,
ByVal ubound As Integer)
Dim j As Integer
= 0
Dim lbound As Integer
= lowp
Dim mid As Integer
= highp - 1
Dim n As Integer
= ubound - lbound + 1
While (lowp <= mid And
highp <= ubound)
If (CInt(arrayKu(lowp))
< CInt(arrayKu(highp))) Then
tempArray(j) =
CInt(arrayKu(lowp))
j += 1
lowp += 1
Else
tempArray(j) =
CInt(arrayKu(highp))
j += 1
highp += 1
End If
End While
While (lowp <= mid)
tempArray(j) =
CInt(arrayKu(lowp))
j += 1
lowp += 1
End While
While (highp <= ubound)
tempArray(j) =
CInt(arrayKu(highp))
j += 1
highp += 1
End While
For j = 0 To n - 1
arrayKu(lbound + j) =
tempArray(j)
Next
End Sub
Private Sub MuatArray()
'Menciptakan array dengan isi
angka-angka acak
Dim kounter As Integer
= 0
Do While kounter <
26
arrayKu.Add(BacaAcak(1, 99))
kounter += 1
Loop
End Sub
Public Function BacaAcak(ByVal
Min As Integer, ByVal Maks As Integer)
As Integer
Static Generator As
System.Random = New System.Random()
Return Generator.Next(Min,
Maks)
End Function
End Class
|
Kasus 90: Tiga Jenis Penjelajahan Pada
Pohon Pencarian Biner
Public Class
Form1
Private Sub
tombolJelajah_Click(sender As Object, e As EventArgs)
Handles tombolJelajah.Click
Dim angka As New
PohonPencarianBiner()
Dim stringHasil1 As String
= ""
Dim stringHasil2 As String
= ""
Dim stringHasil3 As String
= ""
angka.Sisip(23)
angka.Sisip(45)
angka.Sisip(16)
angka.Sisip(37)
angka.Sisip(3)
angka.Sisip(99)
angka.Sisip(22)
kotakListInorder.Items.Add("Penjelajahan
Inorder: ")
stringHasil1 =
angka.PenjelajahanInOrder(angka.akar)
kotakListInorder.Items.Add(stringHasil1)
kotakListPreorder.Items.Add("Penjelajahan
Preorder: ")
stringHasil2 =
angka.PenjelajahanPreOrder(angka.akar)
kotakListPreorder.Items.Add(stringHasil2)
kotakListPostorder.Items.Add("Penjelajahan
Postorder: ")
stringHasil3 =
angka.PenjelajahanPostOrder(angka.akar)
kotakListPostorder.Items.Add(stringHasil3)
End Sub
End Class
Public Class
Simpul
Public iData As Integer
Public Kiri As Simpul
Public Kanan As Simpul
End Class
Public Class
PohonPencarianBiner
Public akar As Simpul
Dim stringKu1 As String
= ""
Dim stringKu2 As String
= ""
Dim stringKu3 As String
= ""
Public Sub New()
akar = Nothing
End Sub
Public Sub Sisip(ByVal
i As Integer)
Dim simpulBaru As New
Simpul()
simpulBaru.iData = i
If (akar Is Nothing) Then
akar = simpulBaru
Else
Dim skrg As Simpul
= akar
Dim induk As Simpul
While (True)
induk = skrg
If (i < skrg.iData)
Then
skrg = skrg.Kiri
If (skrg Is
Nothing) Then
induk.Kiri = simpulBaru
Exit While
End If
Else
skrg = skrg.Kanan
If (skrg Is
Nothing) Then
induk.Kanan =
simpulBaru
Exit While
End If
End If
End While
End If
End Sub
Public Function
PenjelajahanInOrder(ByVal akar As Simpul) As String
If (Not (akar Is
Nothing)) Then
PenjelajahanInOrder(akar.Kiri)
stringKu1 &=
akar.iData.ToString() & "
"
PenjelajahanInOrder(akar.Kanan)
End If
Return stringKu1
End Function
Public Function
PenjelajahanPreOrder(ByVal akar As Simpul) As String
If (Not (akar Is
Nothing)) Then
stringKu2 &=
akar.iData.ToString() & "
"
PenjelajahanPreOrder(akar.Kiri)
PenjelajahanPreOrder(akar.Kanan)
End If
Return stringKu2
End Function
Public Function
PenjelajahanPostOrder(ByVal akar As Simpul) As String
If (Not (akar Is
Nothing)) Then
PenjelajahanPostOrder(akar.Kiri)
PenjelajahanPostOrder(akar.Kanan)
stringKu3 &=
akar.iData.ToString() & " "
End If
Return stringKu3
End Function
End Class
|
Kasus 91: Pohon Pencarian Biner:
Mencari Nilai Maks dan Nilai Min
Public Class
Form1
'Variabel-variabel global
Dim arrayKu As New ArrayList
Private Sub
tombolJelajah_Click(sender As Object, e As EventArgs)
Handles tombolJelajah.Click
kotakListInorder.Items.Add("")
kotakListPreorder.Items.Add("")
kotakListPostorder.Items.Add("")
Dim angka As New
PohonPencarianBiner()
Dim stringHasil1 As String
= ""
Dim stringHasil2 As String
= ""
Dim stringHasil3 As String
= ""
Dim nilaiMaks As Integer
Dim nilaiMin As Integer
MuatArray()
For i As Integer
= 0 To arrayKu.Count - 1
angka.Sisip(CInt(arrayKu(i)))
Next
kotakListInorder.Items.Add("Penjelajahan
Inorder: ")
stringHasil1 =
angka.PenjelajahanInOrder(angka.akar)
kotakListInorder.Items.Add(stringHasil1)
kotakListPreorder.Items.Add("Penjelajahan
Preorder: ")
stringHasil2 =
angka.PenjelajahanPreOrder(angka.akar)
kotakListPreorder.Items.Add(stringHasil2)
kotakListPostorder.Items.Add("Penjelajahan
Postorder: ")
stringHasil3 =
angka.PenjelajahanPostOrder(angka.akar)
kotakListPostorder.Items.Add(stringHasil3)
nilaiMaks = angka.CariMaks()
nilaiMin = angka.CariMin()
teksMaks.Text = nilaiMaks.ToString()
teksMin.Text = nilaiMin.ToString()
End Sub
Private Sub MuatArray()
'Menciptakan array dengan isi
angka-angka acak
Dim kounter As Integer
= 0
Do While kounter <
10
arrayKu.Add(BacaAcak(1, 99))
kounter += 1
Loop
End Sub
Public Function BacaAcak(ByVal
Min As Integer, ByVal Maks As Integer)
As Integer
Static Generator As
System.Random = New System.Random()
Return Generator.Next(Min,
Maks)
End Function
Private Sub
tombolKeluar_Click(sender As Object, e As EventArgs)
Handles tombolKeluar.Click
Me.Close()
End Sub
End Class
Public Class
Simpul
Public iData As Integer
Public Kiri As Simpul
Public Kanan As Simpul
End Class
Public Class
PohonPencarianBiner
Public akar As Simpul
Dim stringKu1 As String
= ""
Dim stringKu2 As String
= ""
Dim stringKu3 As String
= ""
Public Sub New()
akar = Nothing
End Sub
Public Sub Sisip(ByVal
i As Integer)
Dim simpulBaru As New
Simpul()
simpulBaru.iData = i
If (akar Is Nothing)
Then
akar = simpulBaru
Else
Dim skrg As Simpul
= akar
Dim induk As Simpul
While (True)
induk = skrg
If (i < skrg.iData)
Then
skrg = skrg.Kiri
If (skrg Is
Nothing) Then
induk.Kiri =
simpulBaru
Exit While
End If
Else
skrg = skrg.Kanan
If (skrg Is
Nothing) Then
induk.Kanan =
simpulBaru
Exit While
End If
End If
End While
End If
End Sub
Public Function
PenjelajahanInOrder(ByVal akar As Simpul) As String
If (Not (akar Is
Nothing)) Then
PenjelajahanInOrder(akar.Kiri)
stringKu1 &=
akar.iData.ToString() & " "
PenjelajahanInOrder(akar.Kanan)
End If
Return stringKu1
End Function
Public Function
PenjelajahanPreOrder(ByVal akar As Simpul) As String
If (Not (akar Is
Nothing)) Then
stringKu2 &=
akar.iData.ToString() & "
"
PenjelajahanPreOrder(akar.Kiri)
PenjelajahanPreOrder(akar.Kanan)
End If
Return stringKu2
End Function
Public Function
PenjelajahanPostOrder(ByVal akar As Simpul) As String
If (Not (akar Is
Nothing)) Then
PenjelajahanPostOrder(akar.Kiri)
PenjelajahanPostOrder(akar.Kanan)
stringKu3 &=
akar.iData.ToString() & " "
End If
Return stringKu3
End Function
Public
Function CariMin() As Integer
Dim skrg As Simpul = akar
While (Not (skrg.Kiri Is Nothing))
skrg = skrg.Kiri
End While
Return skrg.iData
End
Function
Public
Function CariMaks() As Integer
Dim skrg As Simpul = akar
While (Not (skrg.Kanan Is Nothing))
skrg = skrg.Kanan
End While
Return skrg.iData
End
Function
End Class
|
No comments:
Post a Comment