Sub Process_Globals
Public Serial1 As Serial
Type TreeNode (Value As Int, Left As TreeNode, Right As TreeNode)
Private nodes(50) As TreeNode
Private nodesIndex As Byte
Private rootNode As TreeNode
End Sub
Private Sub AppStart
Serial1.Initialize(115200)
For i = 0 To nodes.Length - 1
AddNode(Rnd(0, 5000))
Next
PrintNode(rootNode)
End Sub
Private Sub AddNode(value As Int)
Dim n As TreeNode = nodes(nodesIndex)
nodesIndex = nodesIndex + 1
n.Value = value
If rootNode = Null Then
rootNode = n
Else
InsertNode(rootNode, n)
End If
End Sub
Private Sub PrintNode(n As TreeNode)
If n.Left <> Null Then PrintNode(n.Left)
Log("Node: ", n.Value)
If n.Right <> Null Then PrintNode(n.Right)
End Sub
Private Sub InsertNode(Parent As TreeNode, NewNode As TreeNode)
If NewNode.Value > Parent.Value Then
If Parent.Right <> Null Then
InsertNode(Parent.Right, NewNode)
Else
Parent.Right = NewNode
End If
Else
If Parent.Left <> Null Then
InsertNode(Parent.Left, NewNode)
Else
Parent.Left = NewNode
End If
End If
End Sub