So I have sat down and written some code out and looked at my book for some reference and read and read and all of the code looks ok except for a few things which are heapArray, currsize, Heap(SIZE) at bottom of program...if someone can get me in the right direction to getting these to work (since they don't exist obviously) I would be most greatful.
VB.NET:
Module Module1
Public Class CArray
Private arr() As Integer
Private numelements As Integer
Public Sub New(ByVal number As Integer)
ReDim Preserve arr(number)
numelements = 0
End Sub
Public Sub insert(ByVal item As Integer)
If (numelements > arr.GetUpperBound(0)) Then
ReDim Preserve arr(numelements * 1.25)
End If
arr(numelements) = item
numelements += 1
End Sub
Public Sub showArray()
Dim index As Integer
For index = 0 To numelements - 1
Console.WriteLine(arr(index) & " ")
Next
Console.WriteLine()
End Sub
End Class
Class Node
Public data As Integer
Public Sub New(ByVal key As Integer)
data = key
End Sub
End Class
Public Sub shiftup(ByVal index As Integer)
Dim parent As Integer = (index - 1) \ 2
Dim bottom As Node = heapArray(index)
While (index > 0 And heapArray(parent).data < _
bottom.data)
heapArray(index) = heapArray(parent)
index = parent
parent = (parent - 1) / 2
End While
End Sub
Public Function insert(ByVal key As Integer) As Boolean
If (currSize = maxSize) Then
Return False
End If
Dim newNode As New Node(key)
heapArray(currSize) = newNode
shiftup(currSize)
currSize += 1
Return True
End Function
Public Function Remove() As Node
Dim root As Node = heapArray(0)
currSize -= 1
heapArray(0) = heapArray(currSize)
ShiftDown(0)
Return root
End Function
Public Sub ShiftDown(ByVal index As Integer)
Dim largerChild As Integer
Dim top As Node = heapArray(index)
While (index < (currSize \ 2))
Dim leftChild As Integer = 2 * index + 1
Dim rightChild As Integer = leftChild + 1
If (rightChild < currSize And heapArray(leftChild). _
data < heapArray(rightChild).data) Then
largerChild = rightChild
Else
largerChild = leftChild
End If
If (top.data >= heapArray(largerChild).data_) Then
Exit While
End If
heapArray(index) = heapArray(largerChild)
index = largerChild
End While
heapArray(index) = top
End Sub
Sub Main()
Const SIZE As Integer = 9
Dim aHeap As New Heap(SIZE)
Dim sortedHeap(SIZE) As Node
Dim index As Integer
For index = 0 To SIZE - 1
Dim aNode As New Node(Int(100 * Rnd() + 1))
aHeap.InsertAt(index, aNode)
aHeap.incSize()
Next
Console.WriteLine("Random: ")
aHeap.showArray()
Console.WriteLine()
Console.WriteLine("Heap: ")
For index = SIZE \ 2 - 1 To 0 Step -1
aHeap.ShiftDown(index)
Next
aHeap.ShowArray()
For index = SIZE - 1 To 0 Step -1
Dim bigNode As Node = aHeap.Remove()
aHeap.InsertAt(index, bigNode)
Next
Console.WriteLine()
Console.WriteLine("Sorted: ")
aHeap.showArray()
Console.Read()
End Sub
End Module