From 629848e3721d9354d25fad6cb4729e6afdbbf799 Mon Sep 17 00:00:00 2001 From: Sherman Hui <11592023+shermanhui@users.noreply.github.com> Date: Fri, 22 Oct 2021 07:07:05 -0700 Subject: [PATCH] [mypy] Fix type annotations in `data_structures/binary_tree` (#5518) * fix: fix mypy errors Update binary_search_tree `arr` argument to be typed as a list within `find_kth_smallest` function Update return type of `merge_two_binary_trees` as both inputs can be None which means that a None type value can be returned from this function * updating DIRECTORY.md Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> --- DIRECTORY.md | 1 + data_structures/binary_tree/binary_search_tree.py | 2 +- data_structures/binary_tree/merge_two_binary_trees.py | 4 +++- 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/DIRECTORY.md b/DIRECTORY.md index 10149eac5..950d8e2c0 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -272,6 +272,7 @@ ## Electronics * [Carrier Concentration](https://github.com/TheAlgorithms/Python/blob/master/electronics/carrier_concentration.py) + * [Coulombs Law](https://github.com/TheAlgorithms/Python/blob/master/electronics/coulombs_law.py) * [Electric Power](https://github.com/TheAlgorithms/Python/blob/master/electronics/electric_power.py) * [Ohms Law](https://github.com/TheAlgorithms/Python/blob/master/electronics/ohms_law.py) diff --git a/data_structures/binary_tree/binary_search_tree.py b/data_structures/binary_tree/binary_search_tree.py index a1ed1d0ac..ce490fd98 100644 --- a/data_structures/binary_tree/binary_search_tree.py +++ b/data_structures/binary_tree/binary_search_tree.py @@ -151,7 +151,7 @@ class BinarySearchTree: def find_kth_smallest(self, k: int, node: Node) -> int: """Return the kth smallest element in a binary search tree""" - arr = [] + arr: list = [] self.inorder(arr, node) # append all values to list using inorder traversal return arr[k - 1] diff --git a/data_structures/binary_tree/merge_two_binary_trees.py b/data_structures/binary_tree/merge_two_binary_trees.py index d169e0e75..748726894 100644 --- a/data_structures/binary_tree/merge_two_binary_trees.py +++ b/data_structures/binary_tree/merge_two_binary_trees.py @@ -7,6 +7,8 @@ will be used as the node of new tree. """ from __future__ import annotations +from typing import Optional + class Node: """ @@ -19,7 +21,7 @@ class Node: self.right: Node | None = None -def merge_two_binary_trees(tree1: Node | None, tree2: Node | None) -> Node: +def merge_two_binary_trees(tree1: Node | None, tree2: Node | None) -> Optional[Node]: """ Returns root node of the merged tree.