forked from Bandashah/9618-Paper-4-VB
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathADT_BinaryTree_Using_Array.vb
More file actions
73 lines (68 loc) · 2.32 KB
/
ADT_BinaryTree_Using_Array.vb
File metadata and controls
73 lines (68 loc) · 2.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
'TASK 1.4 A2 Paper 4 PRM 2019 MJ
'Binary Tree Using Arrays
Module Module1
'Binary Tree ADT
Structure BT
Dim LeftPointer As Integer
Dim Data As String
Dim RightPointer As Integer
End Structure
'Array based on Binary Tree ADT
Dim dsBT(0 To 9) As BT
Sub Main()
Dim Count As Integer
Dim valBT As String
Dim moreDE As Boolean = False
'Initialise whole array. LP/RP to Null 0, Data to Null ""
For Count = 0 To 9
dsBT(Count).LeftPointer = 0
dsBT(Count).Data = ""
dsBT(Count).RightPointer = 0
Next
'Input game name and call insert function
For Count = 0 To 9
Console.Write("Enter Video Game Name " & Count + 1 & ": ")
valBT = Console.ReadLine()
insertBT(valBT)
Next
'Display binary tree after insertion
For Count = 0 To 9
Console.WriteLine(Count & Space(7) & dsBT(Count).LeftPointer & " " & dsBT(Count).Data & Space(27 - Len(dsBT(Count).Data)) & dsBT(Count).RightPointer)
Next
Console.ReadKey()
End Sub
Sub insertBT(ByVal val As String)
Dim currPos As Integer = 0
Dim tPos As Integer = 0
While True
If dsBT(currPos).Data = "" Then
dsBT(currPos).Data = val
Exit While
ElseIf val > dsBT(currPos).Data Then
If dsBT(currPos).RightPointer = 0 Then
tPos = currPos
While dsBT(tPos).Data <> ""
tPos = tPos + 1
End While
dsBT(tPos).Data = val
dsBT(currPos).RightPointer = tPos
Exit While
Else
currPos = dsBT(currPos).RightPointer
End If
Else
If dsBT(currPos).LeftPointer = 0 Then
tPos = currPos
While dsBT(tPos).Data <> ""
tPos = tPos + 1
End While
dsBT(tPos).Data = val
dsBT(currPos).LeftPointer = tPos
Exit While
Else
currPos = dsBT(currPos).LeftPointer
End If
End If
End While
End Sub
End Module