-
-
Notifications
You must be signed in to change notification settings - Fork 50.4k
build binary-tree using traversals #13768
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
1ec0ff0
a7bb97c
5702627
dff30d0
ee149d6
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 |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| # Binary Tree Node class | ||
| class Node: | ||
| def __init__(self, data): | ||
| self.data = data | ||
| self.left = None | ||
| self.right = None | ||
|
|
||
|
|
||
| # Normal inorder traversal | ||
| def inorder(root, out): | ||
|
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. As there is no test file in this pull request nor any test function or class in the file Please provide return type hint for the function: Please provide type hint for the parameter: Please provide type hint for the parameter: |
||
| """Perform inorder traversal and append values to 'out' list.""" | ||
| if root is None: | ||
| return | ||
| inorder(root.left, out) | ||
| out.append(root.data) | ||
| inorder(root.right, out) | ||
|
|
||
|
|
||
| # === Build tree using PREORDER + INORDER === | ||
| def build_tree_from_pre(preorder, pre_start, pre_end, | ||
|
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. As there is no test file in this pull request nor any test function or class in the file Please provide return type hint for the function: Please provide type hint for the parameter: Please provide type hint for the parameter: Please provide type hint for the parameter: |
||
| inorder_seq, in_start, in_end, in_map): | ||
|
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. Please provide type hint for the parameter: Please provide type hint for the parameter: Please provide type hint for the parameter: Please provide type hint for the parameter: |
||
| """Recursive helper to build tree from preorder and inorder traversal.""" | ||
| if pre_start > pre_end or in_start > in_end: | ||
| return None | ||
|
|
||
| # Root is the first element in current preorder segment | ||
| root_val = preorder[pre_start] | ||
| root = Node(root_val) | ||
|
|
||
| # Find the root index in inorder traversal | ||
| in_root_index = in_map[root_val] | ||
| nums_left = in_root_index - in_start | ||
|
|
||
| # Recursively build left and right subtrees | ||
| root.left = build_tree_from_pre( | ||
| preorder, pre_start + 1, pre_start + nums_left, | ||
| inorder_seq, in_start, in_root_index - 1, in_map) | ||
|
|
||
| root.right = build_tree_from_pre( | ||
| preorder, pre_start + nums_left + 1, pre_end, | ||
| inorder_seq, in_root_index + 1, in_end, in_map) | ||
|
|
||
| return root | ||
|
|
||
|
|
||
| def build_tree_pre(inorder_seq, preorder_seq): | ||
|
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. As there is no test file in this pull request nor any test function or class in the file Please provide return type hint for the function: Please provide type hint for the parameter: Please provide type hint for the parameter: |
||
| """Wrapper function for building tree from preorder + inorder.""" | ||
| in_map = {val: i for i, val in enumerate(inorder_seq)} | ||
| return build_tree_from_pre( | ||
| preorder_seq, 0, len(preorder_seq) - 1, | ||
| inorder_seq, 0, len(inorder_seq) - 1, in_map) | ||
|
|
||
|
|
||
| # === Build tree using POSTORDER + INORDER === | ||
| def build_tree_from_post(postorder, post_start, post_end, | ||
|
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. As there is no test file in this pull request nor any test function or class in the file Please provide return type hint for the function: Please provide type hint for the parameter: Please provide type hint for the parameter: Please provide type hint for the parameter: |
||
| inorder_seq, in_start, in_end, in_map): | ||
|
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. Please provide type hint for the parameter: Please provide type hint for the parameter: Please provide type hint for the parameter: Please provide type hint for the parameter: |
||
| """Recursive helper to build tree from postorder and inorder traversal.""" | ||
| if post_start > post_end or in_start > in_end: | ||
| return None | ||
|
|
||
| # Root is the last element in current postorder segment | ||
| root_val = postorder[post_end] | ||
| root = Node(root_val) | ||
|
|
||
| # Find the root index in inorder traversal | ||
| in_root_index = in_map[root_val] | ||
| nums_left = in_root_index - in_start | ||
|
|
||
| # Recursively build left and right subtrees | ||
| root.left = build_tree_from_post( | ||
| postorder, post_start, post_start + nums_left - 1, | ||
| inorder_seq, in_start, in_root_index - 1, in_map) | ||
|
|
||
| root.right = build_tree_from_post( | ||
| postorder, post_start + nums_left, post_end - 1, | ||
| inorder_seq, in_root_index + 1, in_end, in_map) | ||
|
|
||
| return root | ||
|
|
||
|
|
||
| def build_tree_post(inorder_seq, postorder_seq): | ||
|
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. As there is no test file in this pull request nor any test function or class in the file Please provide return type hint for the function: Please provide type hint for the parameter: Please provide type hint for the parameter: |
||
| """Wrapper function for building tree from postorder + inorder.""" | ||
| in_map = {val: i for i, val in enumerate(inorder_seq)} | ||
| return build_tree_from_post( | ||
| postorder_seq, 0, len(postorder_seq) - 1, | ||
| inorder_seq, 0, len(inorder_seq) - 1, in_map) | ||
|
|
||
|
|
||
| # === Example === | ||
| if __name__ == "__main__": | ||
| inorder_seq = [1, 2, 3, 4, 5] | ||
| preorder_seq = [3, 2, 1, 4, 5] | ||
| postorder_seq = [1, 2, 5, 4, 3] | ||
|
|
||
| print("=== Build BST from Preorder & Inorder ===") | ||
| tree_pre = build_tree_pre(inorder_seq, preorder_seq) | ||
| out = [] | ||
| inorder(tree_pre, out) | ||
| print("Inorder:", *out) | ||
|
|
||
| print("=== Build BST from Postorder & Inorder ===") | ||
| tree_post = build_tree_post(inorder_seq, postorder_seq) | ||
| out = [] | ||
| inorder(tree_post, out) | ||
| print("Inorder:", *out) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please provide return type hint for the function:
__init__. If the function does not return a value, please provide the type hint as:def function() -> None:Please provide type hint for the parameter:
data