Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
0 views

Binary Tree VB.NET code

The document contains a Visual Basic module that implements a binary tree data structure with functionalities to initialize the tree, insert nodes, and search for nodes. It defines a structure for tree nodes and provides methods for managing the tree, including a main subroutine that allows user interaction through a console menu. Users can insert new nodes or search for existing nodes until they choose to exit the program.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Binary Tree VB.NET code

The document contains a Visual Basic module that implements a binary tree data structure with functionalities to initialize the tree, insert nodes, and search for nodes. It defines a structure for tree nodes and provides methods for managing the tree, including a main subroutine that allows user interaction through a console menu. Users can insert new nodes or search for existing nodes until they choose to exit the program.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Module Module1

Const NullPointer = 0

Structure TreeNode
Dim Data As String
Dim LeftPointer As Integer
Dim RightPointer As Integer
End Structure

Dim RootPointer As Integer


Dim FreePtr As Integer
Dim Tree(7) As TreeNode

Sub InitializeTree()
RootPointer = NullPointer
FreePtr = 1
For index = 1 To 6
Tree(index).LeftPointer = index + 1
Next
Tree(7).LeftPointer = NullPointer
End Sub

Sub InsertNode(ByVal NewItem)


Dim NewNodePtr, ThisNodePtr, PreviousNodePtr As Integer
Dim TurnedLeft As Boolean
If FreePtr <> NullPointer Then
NewNodePtr = FreePtr
FreePtr = Tree(FreePtr).LeftPointer
Tree(NewNodePtr).Data = NewItem
Tree(NewNodePtr).LeftPointer = NullPointer
Tree(NewNodePtr).RightPointer = NullPointer

' check if empty tree


If RootPointer = NullPointer Then
RootPointer = NewNodePtr
Else
ThisNodePtr = RootPointer
While ThisNodePtr <> NullPointer
PreviousNodePtr = ThisNodePtr
If Tree(ThisNodePtr).Data > NewItem Then
TurnedLeft = True
ThisNodePtr = Tree(ThisNodePtr).LeftPointer
Else
TurnedLeft = False
ThisNodePtr = Tree(ThisNodePtr).RightPointer
End If
End While
If TurnedLeft Then
Tree(PreviousNodePtr).LeftPointer = NewNodePtr
Else
Tree(PreviousNodePtr).RightPointer = NewNodePtr
End If
End If

End If
End Sub
Function FindNode(ByVal SearchItem As String) As Integer
Dim ThisNodePtr As Integer
ThisNodePtr = RootPointer
While ThisNodePtr <> NullPointer And Tree(ThisNodePtr).Data <> SearchItem
If Tree(ThisNodePtr).Data > SearchItem Then

ThisNodePtr = Tree(ThisNodePtr).LeftPointer
Else
ThisNodePtr = Tree(ThisNodePtr).RightPointer

End If
End While
Return ThisNodePtr
End Function
Sub Main()
InitializeTree()

Dim menuOption As Integer = 0


Dim element As String
Console.WriteLine("To Close the program enter -1 for the option")
While menuOption <> -1 ' to close program -1 is to be entered
Console.WriteLine()
Console.WriteLine("1. Search Node")
Console.WriteLine("2. Insert Node")

Console.WriteLine()
Console.Write("Option: ")

If Not Int32.TryParse(Console.ReadLine(), menuOption) Then 'in case


the readline is a string
menuOption = 99 ' intentionally set to make select case print
error
End If

Console.WriteLine()

Select Case menuOption


Case 1
Console.Write("Node to be searched: ")
element = Console.ReadLine()
element = Tree(FindNode(element)).Data
Console.WriteLine(element)
Case 2
Console.Write("Node to be Inserted: ")
element = Console.ReadLine()
InsertNode(element)
Case Else
Console.WriteLine("Wrong option selected")
End Select
End While
End Sub

End Module

You might also like