mirror of
https://hub.njuu.cf/TheAlgorithms/Python.git
synced 2023-10-11 13:06:12 +08:00
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 <dhruvmanila@gmail.com> * Optional["Node"] Co-authored-by: Dhruv Manilawala <dhruvmanila@gmail.com>
This commit is contained in:
parent
2ce6be009a
commit
69457357e8
@ -1,24 +1,17 @@
|
|||||||
# https://en.wikipedia.org/wiki/Tree_traversal
|
# https://en.wikipedia.org/wiki/Tree_traversal
|
||||||
|
from dataclasses import dataclass
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
class Node:
|
class Node:
|
||||||
"""
|
data: int
|
||||||
A Node has data variable and pointers to its left and right nodes.
|
left: Optional["Node"] = None
|
||||||
"""
|
right: Optional["Node"] = None
|
||||||
|
|
||||||
def __init__(self, data):
|
|
||||||
self.left = None
|
|
||||||
self.right = None
|
|
||||||
self.data = data
|
|
||||||
|
|
||||||
|
|
||||||
def make_tree() -> Node:
|
def make_tree() -> Node:
|
||||||
root = Node(1)
|
return Node(1, Node(2, Node(4), Node(5)), Node(3))
|
||||||
root.left = Node(2)
|
|
||||||
root.right = Node(3)
|
|
||||||
root.left.left = Node(4)
|
|
||||||
root.left.right = Node(5)
|
|
||||||
return root
|
|
||||||
|
|
||||||
|
|
||||||
def preorder(root: Node):
|
def preorder(root: Node):
|
||||||
|
Loading…
Reference in New Issue
Block a user