Vertical Order Traversal

Dear Sciaku Learner you are not logged in or not enrolled in this course.

Please Click on login or enroll now button.

If you have any query feel free to chat us!

Happy Coding! Happy Learning!

Lecture 181:-Vertical Order Traversal

Vertical order traversal of a binary tree involves printing the nodes of the tree in vertical order based on their horizontal distance from the root. Nodes at the same horizontal distance are printed on the same vertical line.

Here's a Python implementation of the vertical order traversal of a binary tree:

pythonCopy code

from collections import defaultdict, deque class TreeNode: def __init__(self, data): self.data = data self.left = None self.right = None def vertical_order_traversal(root): if root is None: return # Dictionary to store nodes at each horizontal distance vertical_order = defaultdict(list) # Queue for BFS traversal queue = deque([(root, 0)]) while queue: node, distance = queue.popleft() vertical_order[distance].append(node.data) if node.left: queue.append((node.left, distance - 1)) if node.right: queue.append((node.right, distance + 1)) # Print nodes in vertical order for distance in sorted(vertical_order): print(" ".join(map(str, vertical_order[distance]))) # Example usage root = TreeNode(10) root.left = TreeNode(7) root.right = TreeNode(4) root.left.left = TreeNode(3) root.left.right = TreeNode(11) root.right.left = TreeNode(14) root.right.right = TreeNode(6) print("Vertical Order Traversal:") vertical_order_traversal(root)

In this example, the TreeNode class represents nodes of the binary tree. The vertical_order_traversal function performs the vertical order traversal of the binary tree using a breadth-first search (BFS) approach. It uses a dictionary vertical_order to store nodes at each horizontal distance from the root. The BFS traversal is done using a queue, where each element in the queue is a tuple containing the node and its distance from the root.

After completing the traversal, the nodes are printed in vertical order based on their horizontal distance.

Keep in mind that vertical order traversal is a specific type of tree traversal and might not be as commonly used as other traversal methods. However, it can be useful in certain scenarios, such as when you need to display nodes of a binary tree in a different order for visualization purposes.

25. Tree - Assignments

Comments: 2

profile
@mk.info.work
17-Feb-2024, 10:20 PM

SCIAKU Team please upload 1st video of TREE please please please, please

profile
@na3744
23-Feb-2024, 02:52 AM

I bought this course, it worth it!

profile
@mk.info.work
15-Nov-2023, 10:25 PM

Hi i want to buy this course but you dont have master card payment method please let me know how i can buy it

profile
@sciaku1
11-Jan-2024, 03:23 PM

Dear mk.info.work, Now we have all types of payment options. If you need to purchase just checkout our official website

Frequently Asked Questions (FAQs)

How do I register on Sciaku.com?
How can I enroll in a course on Sciaku.com?
Are there free courses available on Sciaku.com?
How do I purchase a paid course on Sciaku.com?
What payment methods are accepted on Sciaku.com?
How will I access the course content after purchasing a course?
How long do I have access to a purchased course on Sciaku.com?
How do I contact the admin for assistance or support?
Can I get a refund for a course I've purchased?
How does the admin grant access to a course after payment?