Skip to content

binary_tree_traversals.py: Simplify with dataclasses #4336

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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