From 69457357e8c6a3530034aca9707e22ce769da067 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Mon, 26 Apr 2021 06:45:26 +0200 Subject: [PATCH] binary_tree_traversals.py: Simplify with dataclasses (#4336) * binary_tree_traversals.py: Simplify with dataclasses * Update data_structures/binary_tree/binary_tree_traversals.py Co-authored-by: Dhruv Manilawala * Optional["Node"] Co-authored-by: Dhruv Manilawala --- .../binary_tree/binary_tree_traversals.py | 21 +++++++------------ 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/data_structures/binary_tree/binary_tree_traversals.py b/data_structures/binary_tree/binary_tree_traversals.py index 7c0ee1dbb..7857880da 100644 --- a/data_structures/binary_tree/binary_tree_traversals.py +++ b/data_structures/binary_tree/binary_tree_traversals.py @@ -1,24 +1,17 @@ # https://en.wikipedia.org/wiki/Tree_traversal +from dataclasses import dataclass +from typing import Optional +@dataclass class Node: - """ - A Node has data variable and pointers to its left and right nodes. - """ - - def __init__(self, data): - self.left = None - self.right = None - self.data = data + data: int + left: Optional["Node"] = None + right: Optional["Node"] = None def make_tree() -> Node: - root = Node(1) - root.left = Node(2) - root.right = Node(3) - root.left.left = Node(4) - root.left.right = Node(5) - return root + return Node(1, Node(2, Node(4), Node(5)), Node(3)) def preorder(root: Node):