-
Notifications
You must be signed in to change notification settings - Fork 173
zigzag traversal in a binary tree #120
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
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
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 | ||
|
||
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 | ||
|
||
def zigzag_iterative(root: Node): | ||
""" | ||
ZigZag traverse by iterative method: Print node left to right and right to left, alternatively. | ||
""" | ||
if root == None: | ||
return | ||
|
||
# two stacks to store alternate levels | ||
s1 = [] # For levels to be printed from right to left | ||
s2 = [] # For levels to be printed from left to right | ||
|
||
# append first level to first stack 's1' | ||
s1.append(root) | ||
|
||
# Keep printing while any of the stacks has some nodes | ||
while not len(s1) == 0 or not len(s2) == 0: | ||
|
||
# Print nodes of current level from s1 and append nodes of next level to s2 | ||
while not len(s1) == 0: | ||
temp = s1[-1] | ||
s1.pop() | ||
print(temp.data, end = " ") | ||
|
||
# Note that is left is appended before right | ||
if temp.left: | ||
s2.append(temp.left) | ||
if temp.right: | ||
s2.append(temp.right) | ||
|
||
# Print nodes of current level from s2 and append nodes of next level to s1 | ||
while not len(s2) == 0: | ||
temp = s2[-1] | ||
s2.pop() | ||
print(temp.data, end = " ") | ||
|
||
# Note that is rightt is appended before left | ||
if temp.right: | ||
s1.append(temp.right) | ||
if temp.left: | ||
s1.append(temp.left) | ||
|
||
def main(): # Main function for testing. | ||
""" | ||
Create binary tree. | ||
""" | ||
root = make_tree() | ||
print("\nZigzag order traversal(iterative) is: ") | ||
zigzag_iterative(root) | ||
print() | ||
|
||
|
||
if __name__ == "__main__": | ||
import doctest | ||
|
||
doctest.testmod() | ||
main() | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Just one small change.
rightt
toright
. Writing well-documented code is extremely important. Try fixing any other issues of the same kind. Code LGTM.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.
sure, will do it