From 667bf6600e2d598a18a706e6b4b347c540cfdcad Mon Sep 17 00:00:00 2001 From: Vinayak Upadhyay Date: Sun, 15 Oct 2023 16:47:00 +0530 Subject: [PATCH 1/6] Added code to find diameter of given binary tree --- .../binary_tree/diameter_of_binary_tree.py | 73 +++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 data_structures/binary_tree/diameter_of_binary_tree.py diff --git a/data_structures/binary_tree/diameter_of_binary_tree.py b/data_structures/binary_tree/diameter_of_binary_tree.py new file mode 100644 index 000000000000..517c3e136b96 --- /dev/null +++ b/data_structures/binary_tree/diameter_of_binary_tree.py @@ -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, height): + + # 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): + 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)) From f866744b82d7d1fba955b590cc66b358f69833ce Mon Sep 17 00:00:00 2001 From: Vinayak Upadhyay Date: Sun, 15 Oct 2023 16:56:03 +0530 Subject: [PATCH 2/6] Modified diameter_of_binary_tree file --- data_structures/binary_tree/diameter_of_binary_tree.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/data_structures/binary_tree/diameter_of_binary_tree.py b/data_structures/binary_tree/diameter_of_binary_tree.py index 517c3e136b96..6a76d187895e 100644 --- a/data_structures/binary_tree/diameter_of_binary_tree.py +++ b/data_structures/binary_tree/diameter_of_binary_tree.py @@ -12,12 +12,12 @@ def __init__(self, data): # utility class to pass height object class Height: - def __init(self): + def __init__(self): self.h = 0 # Function to calculate diameter of the given binary tree -def calculate_diameter(root, height): +def calculate_diameter(root:Node, height:int) -> int: # to store height of left and right subtree lh = Height() @@ -48,7 +48,7 @@ def calculate_diameter(root, height): return max(lh.h + rh.h + 1, max(ldiameter, rdiameter)) -def diameter(root): +def diameter(root:Node) -> int: height = Height() return calculate_diameter(root, height) From 52d99d2d928ee5cf32dcd19cdeeac684cd37b615 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 15 Oct 2023 11:33:48 +0000 Subject: [PATCH 3/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- .../binary_tree/diameter_of_binary_tree.py | 83 ++++++++++--------- 1 file changed, 42 insertions(+), 41 deletions(-) diff --git a/data_structures/binary_tree/diameter_of_binary_tree.py b/data_structures/binary_tree/diameter_of_binary_tree.py index 6a76d187895e..4e42cc854570 100644 --- a/data_structures/binary_tree/diameter_of_binary_tree.py +++ b/data_structures/binary_tree/diameter_of_binary_tree.py @@ -1,67 +1,68 @@ -''' -The diameter/width of a tree is defined as the number of nodes on the longest path between two end nodes. -''' +""" +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 + 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() +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 + # 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 + # 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 + # 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) + 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 of tree will be max of left subtree + # height and right subtree height plus1 - height.h = max(lh.h, rh.h) + 1 + 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)) + # 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) +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 + 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 @@ -69,5 +70,5 @@ def diameter(root:Node) -> int: 4 5 """ - print("The diameter of the binary tree is:", end=" ") - print(diameter(root)) + print("The diameter of the binary tree is:", end=" ") + print(diameter(root)) From ac76e17035fa3cc42c9163b27ffb0d87fc492a8c Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Mon, 16 Oct 2023 18:39:27 +0200 Subject: [PATCH 4/6] Update diameter_of_binary_tree.py --- .../binary_tree/diameter_of_binary_tree.py | 113 +++++++++--------- 1 file changed, 56 insertions(+), 57 deletions(-) diff --git a/data_structures/binary_tree/diameter_of_binary_tree.py b/data_structures/binary_tree/diameter_of_binary_tree.py index 4e42cc854570..18c84e4f7d1c 100644 --- a/data_structures/binary_tree/diameter_of_binary_tree.py +++ b/data_structures/binary_tree/diameter_of_binary_tree.py @@ -1,74 +1,73 @@ """ -The diameter/width of a tree is defined as the number of nodes on the longest path between two end nodes. +The diameter/width of a tree is defined as the number of nodes on the longest path +between two end nodes. """ +from __future__ import annotations -# A binary tree Node +from dataclasses import dataclass +@dataclass class Node: - def __init__(self, data): - self.data = data - self.left = None - self.right = None + data: int + left: Node | None = None + right: Node | None = None + + def depth(self) -> int: + """ + >>> root = Node(1) + >>> root.depth() + 1 + >>> root.left = Node(2) + >>> root.depth() + 2 + >>> root.left.depth() + 1 + >>> root.right = Node(3) + >>> root.depth() + 2 + """ + left_depth = self.left.depth() if self.left else 0 + right_depth = self.right.depth() if self.right else 0 + return max(left_depth, right_depth) + 1 + + def diameter(self) -> int: + """ + >>> root = Node(1) + >>> root.diameter() + 1 + >>> root.left = Node(2) + >>> root.diameter() + 2 + >>> root.left.diameter() + 1 + >>> root.right = Node(3) + >>> root.diameter() + 3 + """ + left_depth = self.left.depth() if self.left else 0 + right_depth = self.right.depth() if self.right else 0 + return left_depth + right_depth + 1 -# 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__": + from doctest import testmod + + testmod() 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 + r""" + Constructed binary tree is 1 - / \ - 2 3 - / \ + / \ + 2 3 + / \ 4 5 -""" - - print("The diameter of the binary tree is:", end=" ") - print(diameter(root)) + """ + print(f"{root.diameter() = }") + print(f"{root.left.diameter() = }") + print(f"{root.right.diameter() = }") From 4f016c3f3f604db23e6613ca960a8e00a6e34475 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 16 Oct 2023 16:40:01 +0000 Subject: [PATCH 5/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- data_structures/binary_tree/diameter_of_binary_tree.py | 1 - 1 file changed, 1 deletion(-) diff --git a/data_structures/binary_tree/diameter_of_binary_tree.py b/data_structures/binary_tree/diameter_of_binary_tree.py index 18c84e4f7d1c..904f800a90b8 100644 --- a/data_structures/binary_tree/diameter_of_binary_tree.py +++ b/data_structures/binary_tree/diameter_of_binary_tree.py @@ -50,7 +50,6 @@ def diameter(self) -> int: return left_depth + right_depth + 1 - if __name__ == "__main__": from doctest import testmod From 607f61abc618c3a00fbd65a08afec77e40187696 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Mon, 16 Oct 2023 18:43:59 +0200 Subject: [PATCH 6/6] Update diameter_of_binary_tree.py --- data_structures/binary_tree/diameter_of_binary_tree.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/data_structures/binary_tree/diameter_of_binary_tree.py b/data_structures/binary_tree/diameter_of_binary_tree.py index 904f800a90b8..bbe70b028d24 100644 --- a/data_structures/binary_tree/diameter_of_binary_tree.py +++ b/data_structures/binary_tree/diameter_of_binary_tree.py @@ -61,12 +61,12 @@ def diameter(self) -> int: root.left.right = Node(5) r""" Constructed binary tree is - 1 + 1 / \ 2 3 / \ - 4 5 + 4 5 """ - print(f"{root.diameter() = }") - print(f"{root.left.diameter() = }") - print(f"{root.right.diameter() = }") + print(f"{root.diameter() = }") # 4 + print(f"{root.left.diameter() = }") # 3 + print(f"{root.right.diameter() = }") # 1