Posted on by Kalkicode
Code Binary Tree

Diagonal sum of a binary tree in vb.net

Vb program for Diagonal sum of a binary tree. Here more information.

' Include namespace system
Imports System
Imports System.Collections.Generic
Imports System.Collections
 
'  Vb.net program for
'  Diagonal sum of a binary tree

'  Binary Tree Node
Public Class TreeNode
    Public  data As Integer
    Public  left As TreeNode
    Public  right As TreeNode
    Public Sub New(ByVal data As Integer)
        '  Set node value
        Me.data = data
        Me.left = Nothing
        Me.right = Nothing
    End Sub
End Class
public Class BinaryTree
    Public  root As TreeNode
    Public Sub New()
        '  Set initial tree root
        Me.root = Nothing
    End Sub
    '  Find sum of diagonal elements
    Public Sub getDiagonalSum(ByVal node As TreeNode, 
                              ByVal distance As Integer, 
                              ByVal record As Dictionary(Of Integer, Integer))
        if (node IsNot Nothing) Then
            if (record.ContainsKey(distance)) Then
                '  Update value
                record(distance) = record(distance) + node.data
            Else
                '  Add new element
                record(distance) = node.data
            End IF
            Me.getDiagonalSum(node.left, distance + 1, record)
            Me.getDiagonalSum(node.right, distance, record)
        End If
    End Sub
    Public Sub diagonalSum()
        '  This is store result
        Dim record As Dictionary(Of Integer, Integer) = 
          New Dictionary(Of Integer, Integer)()
        Me.getDiagonalSum(Me.root, 0, record)
        Dim distance As Integer = 0
        '  This loop display diagonal sum
        while (distance < record.Count)
            '  Display sum
            Console.Write("  {0}",record(distance))
            distance += 1
        End While
    End Sub
    Public Shared Sub Main(ByVal args As String())
        '  Create new tree
        Dim tree As BinaryTree = New BinaryTree()
        ' Make A Binary Tree
		'   -------------------
        '          10
        '         / \ 
        '        /   \
        '       2     4
        '      /    /  \
        '     3    6    \
        '         / \    \
        '        1   7    5
        '           /    /
        '          9    3 
        '  Add tree nodes
        tree.root = New TreeNode(10)
        tree.root.left = New TreeNode(2)
        tree.root.left.left = New TreeNode(3)
        tree.root.right = New TreeNode(4)
        tree.root.right.right = New TreeNode(5)
        tree.root.right.left = New TreeNode(6)
        tree.root.right.left.left = New TreeNode(1)
        tree.root.right.right.left = New TreeNode(3)
        tree.root.right.left.right = New TreeNode(7)
        tree.root.right.left.right.left = New TreeNode(9)
        tree.diagonalSum()
    End Sub
End Class

Output

  19  18  13

Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment