From 6a757a14212ae58a3b8264a1f9d645f04fabdcd5 Mon Sep 17 00:00:00 2001 From: miguelhx Date: Thu, 23 Sep 2021 10:12:36 -0700 Subject: [PATCH 1/7] Initial file setup for 4.3 --- .../chapter04/p03_list_of_depths/miguelHx.py | 134 ++++++++++++++++++ 1 file changed, 134 insertions(+) create mode 100644 Python/chapter04/p03_list_of_depths/miguelHx.py diff --git a/Python/chapter04/p03_list_of_depths/miguelHx.py b/Python/chapter04/p03_list_of_depths/miguelHx.py new file mode 100644 index 00000000..d3cffb21 --- /dev/null +++ b/Python/chapter04/p03_list_of_depths/miguelHx.py @@ -0,0 +1,134 @@ +"""Python Version 3.9.2 +4.3 - List of Depths: +Given a binary tree, design an algorithm which creates +a linked list of all the nodes at each depth +(e.g., if you have a tree with depth D, you'll have D linked lists). +""" +import unittest + +from abc import abstractmethod +from dataclasses import dataclass +from typing import Generic, TypeVar +from typing import Optional, Protocol +from typing import Generator, List, Iterator + + +T = TypeVar('T', bound='Comparable') + +class Comparable(Protocol): + @abstractmethod + def __lt__(self, other: T) -> bool: + pass + + @abstractmethod + def __gt__(self, other: T) -> bool: + pass + + @abstractmethod + def __eq__(self, other: object) -> bool: + pass + +@dataclass +class BSTNode(Generic[T]): + val: T + left_child: 'Optional[BSTNode]' = None + right_child: 'Optional[BSTNode]' = None + + def __str__(self): + return f'Node ({self.id}), Left ID: {self.left_child.id}, Right ID: {self.right_child.id}' + +class BSTIterator(Iterator[T]): + + def __init__(self, root: Optional[BSTNode]): + self.gen = self.in_order_traversal_generator(root) + + def in_order_traversal_generator(self, node: Optional[BSTNode]) -> Generator: + if not node: + raise StopIteration + if node.left_child: + yield from self.in_order_traversal_generator(node.left_child) + yield node.val + if node.right_child: + yield from self.in_order_traversal_generator(node.right_child) + + def __next__(self) -> T: + return next(self.gen) + +@dataclass +class BinarySearchTree: + root: 'Optional[BSTNode]' = None + + def insert(self, value: T) -> None: + if not self.root: + self.root = BSTNode(value) + else: + self._insert(value, self.root) + + def _insert(self, value: T, curr_node: BSTNode) -> None: + if value < curr_node.val: + if not curr_node.left_child: + # insert here + curr_node.left_child = BSTNode(value) + else: + # otherwise, keep searching left subtree + self._insert(value, curr_node.left_child) + elif value > curr_node.val: + if not curr_node.right_child: + # insert here + curr_node.right_child = BSTNode(value) + else: + # otherwise, keep searching right subtree + self._insert(value, curr_node.right_child) + else: + raise ValueError(f'Value {value} already exists in tree.') + + def height(self) -> int: + return self._height(self.root) + + def _height(self, node: Optional[BSTNode]) -> int: + if not node: + return 0 + else: + return 1 + max(self._height(node.left_child), self._height(node.right_child)) + + def print_tree(self): + if self.root: + self._print_tree(self.root) + + def _print_tree(self, curr_node: Optional[BSTNode]) -> None: + if curr_node: + self._print_tree(curr_node.left_child) + print(curr_node.val) + self._print_tree(curr_node.right_child) + + def __iter__(self) -> BSTIterator: + return BSTIterator(self.root) + + +class TestBinarySearchTree(unittest.TestCase): + + def test_binary_search_tree_creation_height_3(self): + bst = BinarySearchTree() + bst.insert(8) + bst.insert(4) + bst.insert(10) + bst.insert(2) + bst.insert(6) + bst.insert(20) + self.assertEqual(list(bst), [2, 4, 6, 8, 10, 20]) + self.assertEqual(bst.height(), 3) + + def test_binary_search_tree_creation_height_4(self): + bst = BinarySearchTree() + bst.insert(8) + bst.insert(2) + bst.insert(10) + bst.insert(4) + bst.insert(6) + bst.insert(20) + self.assertEqual(list(bst), [2, 4, 6, 8, 10, 20]) + self.assertEqual(bst.height(), 4) + + +if __name__ == '__main__': + unittest.main() From 30af52fb9300bbd746b82f6d472b799d5de15655 Mon Sep 17 00:00:00 2001 From: miguelhx Date: Thu, 23 Sep 2021 11:14:58 -0700 Subject: [PATCH 2/7] Setup basic test case, function signature, update BST data structure to be a regular Binary Tree rather than a Binary Search Tree --- .../chapter04/p03_list_of_depths/miguelHx.py | 105 ++++++++++++------ 1 file changed, 72 insertions(+), 33 deletions(-) diff --git a/Python/chapter04/p03_list_of_depths/miguelHx.py b/Python/chapter04/p03_list_of_depths/miguelHx.py index d3cffb21..d972bc8c 100644 --- a/Python/chapter04/p03_list_of_depths/miguelHx.py +++ b/Python/chapter04/p03_list_of_depths/miguelHx.py @@ -7,9 +7,10 @@ import unittest from abc import abstractmethod +from collections import deque from dataclasses import dataclass from typing import Generic, TypeVar -from typing import Optional, Protocol +from typing import Optional, Protocol, Deque from typing import Generator, List, Iterator @@ -29,20 +30,20 @@ def __eq__(self, other: object) -> bool: pass @dataclass -class BSTNode(Generic[T]): +class BTNode(Generic[T]): val: T - left_child: 'Optional[BSTNode]' = None - right_child: 'Optional[BSTNode]' = None + left_child: 'Optional[BTNode]' = None + right_child: 'Optional[BTNode]' = None def __str__(self): return f'Node ({self.id}), Left ID: {self.left_child.id}, Right ID: {self.right_child.id}' class BSTIterator(Iterator[T]): - def __init__(self, root: Optional[BSTNode]): + def __init__(self, root: Optional[BTNode]): self.gen = self.in_order_traversal_generator(root) - def in_order_traversal_generator(self, node: Optional[BSTNode]) -> Generator: + def in_order_traversal_generator(self, node: Optional[BTNode]) -> Generator: if not node: raise StopIteration if node.left_child: @@ -55,27 +56,27 @@ def __next__(self) -> T: return next(self.gen) @dataclass -class BinarySearchTree: - root: 'Optional[BSTNode]' = None +class BinaryTree: + root: 'Optional[BTNode]' = None def insert(self, value: T) -> None: if not self.root: - self.root = BSTNode(value) + self.root = BTNode(value) else: self._insert(value, self.root) - def _insert(self, value: T, curr_node: BSTNode) -> None: + def _insert(self, value: T, curr_node: BTNode) -> None: if value < curr_node.val: if not curr_node.left_child: # insert here - curr_node.left_child = BSTNode(value) + curr_node.left_child = BTNode(value) else: # otherwise, keep searching left subtree self._insert(value, curr_node.left_child) elif value > curr_node.val: if not curr_node.right_child: # insert here - curr_node.right_child = BSTNode(value) + curr_node.right_child = BTNode(value) else: # otherwise, keep searching right subtree self._insert(value, curr_node.right_child) @@ -85,7 +86,7 @@ def _insert(self, value: T, curr_node: BSTNode) -> None: def height(self) -> int: return self._height(self.root) - def _height(self, node: Optional[BSTNode]) -> int: + def _height(self, node: Optional[BTNode]) -> int: if not node: return 0 else: @@ -95,7 +96,7 @@ def print_tree(self): if self.root: self._print_tree(self.root) - def _print_tree(self, curr_node: Optional[BSTNode]) -> None: + def _print_tree(self, curr_node: Optional[BTNode]) -> None: if curr_node: self._print_tree(curr_node.left_child) print(curr_node.val) @@ -105,29 +106,67 @@ def __iter__(self) -> BSTIterator: return BSTIterator(self.root) -class TestBinarySearchTree(unittest.TestCase): +def list_of_depths(bt: BinaryTree) -> List[Deque[BTNode]]: + """Given a binary tree, design an algorithm which creates + a linked list of all the nodes at each depth + (e.g., if you have a tree with depth D, you'll have D linked lists). + Args: + bst (BinaryTree): input binary tree + + Returns: + List[Deque[BTNode]]: list of nodes at each depth + """ + return [] + + + +class TestBinaryTree(unittest.TestCase): def test_binary_search_tree_creation_height_3(self): - bst = BinarySearchTree() - bst.insert(8) - bst.insert(4) - bst.insert(10) - bst.insert(2) - bst.insert(6) - bst.insert(20) - self.assertEqual(list(bst), [2, 4, 6, 8, 10, 20]) - self.assertEqual(bst.height(), 3) + bt = BinaryTree() + bt.insert(8) + bt.insert(4) + bt.insert(10) + bt.insert(2) + bt.insert(6) + bt.insert(20) + self.assertEqual(list(bt), [2, 4, 6, 8, 10, 20]) + self.assertEqual(bt.height(), 3) def test_binary_search_tree_creation_height_4(self): - bst = BinarySearchTree() - bst.insert(8) - bst.insert(2) - bst.insert(10) - bst.insert(4) - bst.insert(6) - bst.insert(20) - self.assertEqual(list(bst), [2, 4, 6, 8, 10, 20]) - self.assertEqual(bst.height(), 4) + bt = BinaryTree() + bt.insert(8) + bt.insert(2) + bt.insert(10) + bt.insert(4) + bt.insert(6) + bt.insert(20) + self.assertEqual(list(bt), [2, 4, 6, 8, 10, 20]) + self.assertEqual(bt.height(), 4) + + +class TestListOfDepths(unittest.TestCase): + + def test_list_of_depths_full_btree_height_3(self): + bt = BinaryTree() + bt.insert(8) + bt.insert(4) + bt.insert(10) + bt.insert(2) + bt.insert(6) + bt.insert(20) + self.assertEqual(list(bt), [2, 4, 6, 8, 10, 20]) + self.assertEqual(bt.height(), 3) + + expected_result = [ + deque([8]), + deque([4, 10]), + deque([2, 6, 9, 10]) + ] + + result = list_of_depths(bt) + + self.assertEqual(result, expected_result) if __name__ == '__main__': From a2a3681e060864cb985ccbc3921fc5eecb3fb9bf Mon Sep 17 00:00:00 2001 From: miguelhx Date: Thu, 23 Sep 2021 11:42:11 -0700 Subject: [PATCH 3/7] implement list of depths --- .../chapter04/p03_list_of_depths/miguelHx.py | 31 ++++++++++++++++--- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/Python/chapter04/p03_list_of_depths/miguelHx.py b/Python/chapter04/p03_list_of_depths/miguelHx.py index d972bc8c..c9158ca7 100644 --- a/Python/chapter04/p03_list_of_depths/miguelHx.py +++ b/Python/chapter04/p03_list_of_depths/miguelHx.py @@ -9,7 +9,7 @@ from abc import abstractmethod from collections import deque from dataclasses import dataclass -from typing import Generic, TypeVar +from typing import Generic, TypeVar, Dict from typing import Optional, Protocol, Deque from typing import Generator, List, Iterator @@ -35,6 +35,10 @@ class BTNode(Generic[T]): left_child: 'Optional[BTNode]' = None right_child: 'Optional[BTNode]' = None + @property + def children(self) -> List[BTNode]: + return [left_child, right_child] + def __str__(self): return f'Node ({self.id}), Left ID: {self.left_child.id}, Right ID: {self.right_child.id}' @@ -106,7 +110,7 @@ def __iter__(self) -> BSTIterator: return BSTIterator(self.root) -def list_of_depths(bt: BinaryTree) -> List[Deque[BTNode]]: +def list_of_depths(bt: BinaryTree) -> Dict[int, Deque[BTNode]]: """Given a binary tree, design an algorithm which creates a linked list of all the nodes at each depth (e.g., if you have a tree with depth D, you'll have D linked lists). @@ -116,8 +120,27 @@ def list_of_depths(bt: BinaryTree) -> List[Deque[BTNode]]: Returns: List[Deque[BTNode]]: list of nodes at each depth """ - return [] - + # first, what is depth of tree? + total_depth = bt.height() + depth_list_map: Dict[int, Deque[BTNode]] = { + 0: deque([bt.root]) + } + # initialize + for d in range(1, total_depth): + depth_list_map[d] = deque() + queue: Deque[BTNode] = deque(bt.root) + # root is depth 0 + curr_depth = 1 + while queue: + bt_node = queue.popLeft() + for n in bt_node.children: + if not n: + continue + # otherwise, + queue.append(n) + depth_list_map[curr_depth].append(n) + curr_depth += 1 + return depth_list_map class TestBinaryTree(unittest.TestCase): From 37ee22e3fceb9bf3e0c77476fb35b98543c6c36d Mon Sep 17 00:00:00 2001 From: miguelhx Date: Thu, 23 Sep 2021 12:03:26 -0700 Subject: [PATCH 4/7] 1st iteration implementation complete --- .../chapter04/p03_list_of_depths/miguelHx.py | 51 ++++++++++--------- 1 file changed, 27 insertions(+), 24 deletions(-) diff --git a/Python/chapter04/p03_list_of_depths/miguelHx.py b/Python/chapter04/p03_list_of_depths/miguelHx.py index c9158ca7..d2c231b5 100644 --- a/Python/chapter04/p03_list_of_depths/miguelHx.py +++ b/Python/chapter04/p03_list_of_depths/miguelHx.py @@ -32,12 +32,13 @@ def __eq__(self, other: object) -> bool: @dataclass class BTNode(Generic[T]): val: T + depth: int = 0 left_child: 'Optional[BTNode]' = None right_child: 'Optional[BTNode]' = None @property - def children(self) -> List[BTNode]: - return [left_child, right_child] + def children(self) -> 'List[Optional[BTNode]]': + return [self.left_child, self.right_child] def __str__(self): return f'Node ({self.id}), Left ID: {self.left_child.id}, Right ID: {self.right_child.id}' @@ -67,23 +68,23 @@ def insert(self, value: T) -> None: if not self.root: self.root = BTNode(value) else: - self._insert(value, self.root) + self._insert(value, self.root, 1) - def _insert(self, value: T, curr_node: BTNode) -> None: + def _insert(self, value: T, curr_node: BTNode, curr_depth: int) -> None: if value < curr_node.val: if not curr_node.left_child: # insert here - curr_node.left_child = BTNode(value) + curr_node.left_child = BTNode(value, curr_depth) else: # otherwise, keep searching left subtree - self._insert(value, curr_node.left_child) + self._insert(value, curr_node.left_child, curr_depth + 1) elif value > curr_node.val: if not curr_node.right_child: # insert here - curr_node.right_child = BTNode(value) + curr_node.right_child = BTNode(value, curr_depth) else: # otherwise, keep searching right subtree - self._insert(value, curr_node.right_child) + self._insert(value, curr_node.right_child, curr_depth + 1) else: raise ValueError(f'Value {value} already exists in tree.') @@ -110,36 +111,40 @@ def __iter__(self) -> BSTIterator: return BSTIterator(self.root) -def list_of_depths(bt: BinaryTree) -> Dict[int, Deque[BTNode]]: +def list_of_depths(bt: BinaryTree) -> Dict[int, Deque[T]]: """Given a binary tree, design an algorithm which creates a linked list of all the nodes at each depth (e.g., if you have a tree with depth D, you'll have D linked lists). + + Note: The original problem statement said to return a list of nodes for + each depth. However, I am instead creating a list of node vals for each depth. + Args: bst (BinaryTree): input binary tree Returns: List[Deque[BTNode]]: list of nodes at each depth """ + if not bt.root: + return {} # first, what is depth of tree? total_depth = bt.height() depth_list_map: Dict[int, Deque[BTNode]] = { - 0: deque([bt.root]) + 0: deque([bt.root.val]) } # initialize for d in range(1, total_depth): depth_list_map[d] = deque() - queue: Deque[BTNode] = deque(bt.root) + queue: Deque[BTNode] = deque([bt.root]) # root is depth 0 - curr_depth = 1 while queue: - bt_node = queue.popLeft() + bt_node = queue.popleft() for n in bt_node.children: if not n: continue # otherwise, queue.append(n) - depth_list_map[curr_depth].append(n) - curr_depth += 1 + depth_list_map[n.depth].append(n.val) return depth_list_map @@ -175,20 +180,18 @@ def test_list_of_depths_full_btree_height_3(self): bt.insert(8) bt.insert(4) bt.insert(10) + bt.insert(9) bt.insert(2) bt.insert(6) bt.insert(20) - self.assertEqual(list(bt), [2, 4, 6, 8, 10, 20]) + self.assertEqual(list(bt), [2, 4, 6, 8, 9, 10, 20]) self.assertEqual(bt.height(), 3) - - expected_result = [ - deque([8]), - deque([4, 10]), - deque([2, 6, 9, 10]) - ] - + expected_result = { + 0: deque([8]), + 1: deque([4, 10]), + 2: deque([2, 6, 9, 20]) + } result = list_of_depths(bt) - self.assertEqual(result, expected_result) From 7baca049880f271e9ff0b4ed2cf5381dba137ac2 Mon Sep 17 00:00:00 2001 From: miguelhx Date: Thu, 23 Sep 2021 12:05:11 -0700 Subject: [PATCH 5/7] Fix last mypy error --- Python/chapter04/p03_list_of_depths/miguelHx.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Python/chapter04/p03_list_of_depths/miguelHx.py b/Python/chapter04/p03_list_of_depths/miguelHx.py index d2c231b5..8e2bb117 100644 --- a/Python/chapter04/p03_list_of_depths/miguelHx.py +++ b/Python/chapter04/p03_list_of_depths/miguelHx.py @@ -129,7 +129,7 @@ def list_of_depths(bt: BinaryTree) -> Dict[int, Deque[T]]: return {} # first, what is depth of tree? total_depth = bt.height() - depth_list_map: Dict[int, Deque[BTNode]] = { + depth_list_map: Dict[int, Deque[T]] = { 0: deque([bt.root.val]) } # initialize From 0bc05c2388ff4fe1c04f3c69935339938c28ca02 Mon Sep 17 00:00:00 2001 From: miguelhx Date: Thu, 23 Sep 2021 12:26:27 -0700 Subject: [PATCH 6/7] Fix strict mypy errors --- .../chapter04/p03_list_of_depths/miguelHx.py | 47 ++++++++++--------- 1 file changed, 25 insertions(+), 22 deletions(-) diff --git a/Python/chapter04/p03_list_of_depths/miguelHx.py b/Python/chapter04/p03_list_of_depths/miguelHx.py index 8e2bb117..61456636 100644 --- a/Python/chapter04/p03_list_of_depths/miguelHx.py +++ b/Python/chapter04/p03_list_of_depths/miguelHx.py @@ -33,22 +33,25 @@ def __eq__(self, other: object) -> bool: class BTNode(Generic[T]): val: T depth: int = 0 - left_child: 'Optional[BTNode]' = None - right_child: 'Optional[BTNode]' = None + left_child: 'Optional[BTNode[T]]' = None + right_child: 'Optional[BTNode[T]]' = None @property - def children(self) -> 'List[Optional[BTNode]]': + def children(self) -> 'List[Optional[BTNode[T]]]': return [self.left_child, self.right_child] - def __str__(self): - return f'Node ({self.id}), Left ID: {self.left_child.id}, Right ID: {self.right_child.id}' + def children_as_str(self) -> str: + return ', '.join(str(child.val) if child else '' for child in self.children) + + def __str__(self) -> str: + return f'Node ({self.val}), children: {self.children_as_str()}' class BSTIterator(Iterator[T]): - def __init__(self, root: Optional[BTNode]): + def __init__(self, root: Optional[BTNode[T]]): self.gen = self.in_order_traversal_generator(root) - def in_order_traversal_generator(self, node: Optional[BTNode]) -> Generator: + def in_order_traversal_generator(self, node: Optional[BTNode[T]]) -> Generator[T, Optional[BTNode[T]], None]: if not node: raise StopIteration if node.left_child: @@ -61,8 +64,8 @@ def __next__(self) -> T: return next(self.gen) @dataclass -class BinaryTree: - root: 'Optional[BTNode]' = None +class BinaryTree(Generic[T]): + root: 'Optional[BTNode[T]]' = None def insert(self, value: T) -> None: if not self.root: @@ -70,7 +73,7 @@ def insert(self, value: T) -> None: else: self._insert(value, self.root, 1) - def _insert(self, value: T, curr_node: BTNode, curr_depth: int) -> None: + def _insert(self, value: T, curr_node: BTNode[T], curr_depth: int) -> None: if value < curr_node.val: if not curr_node.left_child: # insert here @@ -91,27 +94,27 @@ def _insert(self, value: T, curr_node: BTNode, curr_depth: int) -> None: def height(self) -> int: return self._height(self.root) - def _height(self, node: Optional[BTNode]) -> int: + def _height(self, node: Optional[BTNode[T]]) -> int: if not node: return 0 else: return 1 + max(self._height(node.left_child), self._height(node.right_child)) - def print_tree(self): + def print_tree(self) -> None: if self.root: self._print_tree(self.root) - def _print_tree(self, curr_node: Optional[BTNode]) -> None: + def _print_tree(self, curr_node: Optional[BTNode[T]]) -> None: if curr_node: self._print_tree(curr_node.left_child) print(curr_node.val) self._print_tree(curr_node.right_child) - def __iter__(self) -> BSTIterator: + def __iter__(self) -> BSTIterator[T]: return BSTIterator(self.root) -def list_of_depths(bt: BinaryTree) -> Dict[int, Deque[T]]: +def list_of_depths(bt: BinaryTree[T]) -> Dict[int, Deque[T]]: """Given a binary tree, design an algorithm which creates a linked list of all the nodes at each depth (e.g., if you have a tree with depth D, you'll have D linked lists). @@ -135,7 +138,7 @@ def list_of_depths(bt: BinaryTree) -> Dict[int, Deque[T]]: # initialize for d in range(1, total_depth): depth_list_map[d] = deque() - queue: Deque[BTNode] = deque([bt.root]) + queue: Deque[BTNode[T]] = deque([bt.root]) # root is depth 0 while queue: bt_node = queue.popleft() @@ -150,8 +153,8 @@ def list_of_depths(bt: BinaryTree) -> Dict[int, Deque[T]]: class TestBinaryTree(unittest.TestCase): - def test_binary_search_tree_creation_height_3(self): - bt = BinaryTree() + def test_binary_search_tree_creation_height_3(self) -> None: + bt: BinaryTree = BinaryTree() bt.insert(8) bt.insert(4) bt.insert(10) @@ -161,8 +164,8 @@ def test_binary_search_tree_creation_height_3(self): self.assertEqual(list(bt), [2, 4, 6, 8, 10, 20]) self.assertEqual(bt.height(), 3) - def test_binary_search_tree_creation_height_4(self): - bt = BinaryTree() + def test_binary_search_tree_creation_height_4(self) -> None: + bt: BinaryTree = BinaryTree() bt.insert(8) bt.insert(2) bt.insert(10) @@ -175,8 +178,8 @@ def test_binary_search_tree_creation_height_4(self): class TestListOfDepths(unittest.TestCase): - def test_list_of_depths_full_btree_height_3(self): - bt = BinaryTree() + def test_list_of_depths_full_btree_height_3(self) -> None: + bt: BinaryTree = BinaryTree() bt.insert(8) bt.insert(4) bt.insert(10) From 4d56071ac5802de496a9fa24085b69fe91f46b7e Mon Sep 17 00:00:00 2001 From: miguelhx Date: Mon, 27 Sep 2021 18:18:44 -0700 Subject: [PATCH 7/7] BST -> bt --- Python/chapter04/p03_list_of_depths/miguelHx.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Python/chapter04/p03_list_of_depths/miguelHx.py b/Python/chapter04/p03_list_of_depths/miguelHx.py index 61456636..cd4d45d2 100644 --- a/Python/chapter04/p03_list_of_depths/miguelHx.py +++ b/Python/chapter04/p03_list_of_depths/miguelHx.py @@ -46,7 +46,7 @@ def children_as_str(self) -> str: def __str__(self) -> str: return f'Node ({self.val}), children: {self.children_as_str()}' -class BSTIterator(Iterator[T]): +class BTIterator(Iterator[T]): def __init__(self, root: Optional[BTNode[T]]): self.gen = self.in_order_traversal_generator(root) @@ -110,8 +110,8 @@ def _print_tree(self, curr_node: Optional[BTNode[T]]) -> None: print(curr_node.val) self._print_tree(curr_node.right_child) - def __iter__(self) -> BSTIterator[T]: - return BSTIterator(self.root) + def __iter__(self) -> BTIterator[T]: + return BTIterator(self.root) def list_of_depths(bt: BinaryTree[T]) -> Dict[int, Deque[T]]: @@ -123,7 +123,7 @@ def list_of_depths(bt: BinaryTree[T]) -> Dict[int, Deque[T]]: each depth. However, I am instead creating a list of node vals for each depth. Args: - bst (BinaryTree): input binary tree + bt (BinaryTree): input binary tree Returns: List[Deque[BTNode]]: list of nodes at each depth