Skip to content

Added functionality to calculate the diameter of given binary tree #10526

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Oct 16, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions data_structures/binary_tree/diameter_of_binary_tree.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
'''
The diameter/width of a tree is defined as the number of nodes on the longest path between two end nodes.
'''

# A binary tree Node

class Node:
def __init__(self, data):
self.data = data
self.left = None
self.right = None

# utility class to pass height object
class Height:
def __init__(self):
self.h = 0


# Function to calculate diameter of the given binary tree
def calculate_diameter(root:Node, height:int) -> int:

# to store height of left and right subtree
lh = Height()
rh = Height()

# base condition- when binary tree is empty
if root is None:
height.h = 0
return 0

# ldiameter --> diameter of left subtree
# rdiameter --> diameter of right subtree

# height of left subtree and right subtree is obtained from lh and rh and returned value of function is stored in ldiameter and rdiameter

ldiameter = calculate_diameter(root.left, lh)
rdiameter = calculate_diameter(root.right, rh)

# height of tree will be max of left subtree
# height and right subtree height plus1

height.h = max(lh.h, rh.h) + 1

# return maximum of the following
# 1)left diameter
# 2)right diameter
# 3)left height + right height + 1
return max(lh.h + rh.h + 1, max(ldiameter, rdiameter))


def diameter(root:Node) -> int:
height = Height()
return calculate_diameter(root, height)


if __name__ == "__main__":
root = Node(1)
root.left = Node(2)
root.right = Node(3)
root.left.left = Node(4)
root.left.right = Node(5)

"""
Constructed binary tree is
1
/ \
2 3
/ \
4 5
"""

print("The diameter of the binary tree is:", end=" ")
print(diameter(root))