Skip to content

Commit

Permalink
binary_tree_traversals.py: Simplify with dataclasses (TheAlgorithms#4336
Browse files Browse the repository at this point in the history
)

* binary_tree_traversals.py: Simplify with dataclasses

* Update data_structures/binary_tree/binary_tree_traversals.py

Co-authored-by: Dhruv Manilawala <dhruvmanila@gmail.com>

* Optional["Node"]

Co-authored-by: Dhruv Manilawala <dhruvmanila@gmail.com>
  • Loading branch information
cclauss and dhruvmanila committed Apr 26, 2021
1 parent 90dd73c commit 74f8348
Showing 1 changed file with 7 additions and 14 deletions.
21 changes: 7 additions & 14 deletions data_structures/binary_tree/binary_tree_traversals.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down

0 comments on commit 74f8348

Please sign in to comment.