-
-
Notifications
You must be signed in to change notification settings - Fork 46.8k
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,16 @@ | ||
# https://en.wikipedia.org/wiki/Tree_traversal | ||
from dataclasses import 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 | ||
@dataclass | ||
class Node(object): | ||
data: int | ||
left: "Node" = None | ||
right: "Node" = None | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I did not notice this the first time but shouldn't this be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My sense was that if neither Python nor mypy was complaining about it then I would not either. This would be the only location where we would set these values to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Python is never going to complain as it simply does not care about type hints. And no, as per this definition, the leaves of the trees (the last nodes) will have its left and right node There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, but that still won't allow the file to be type checked later on. The variable's correct type is |
||
|
||
|
||
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): | ||
|
Uh oh!
There was an error while loading. Please reload this page.