Binary trees are a fundamental data structure in computer science, commonly used for the efficient storage of data. One intriguing aspect of binary trees is their visual representation, which can be viewed from different perspectives. In this article, we will delve into the concept of the top view of a binary tree and explore various techniques to obtain it.
Top View of Binary Tree
The top view of a binary tree refers to the set of nodes visible when looking at the tree from the top, with the root node being the highest point. It provides an intuitive representation that captures the relative positions of nodes and their relationships in the tree structure.
Consider the following binary tree:
The top view of this tree would be [4, 2, 1, 3, 6], as these nodes are visible from the top.
Algorithmic to Find Top View of Binary Bree
To find the top view of a binary tree, we can employ a breadth-first search (BFS) or level-order traversal technique. The key idea is to assign horizontal distances to each node based on their positions relative to the root node.
By keeping track of the horizontal distances and maintaining a mapping of the leftmost and rightmost nodes at each horizontal level, we can extract the top view efficiently.
Here is the step-by-step algorithm:
- Start by creating an empty queue and enqueue the root node along with its horizontal distance, which is initially set to 0.
- While the queue is not empty, dequeue a node and its corresponding horizontal distance.
- If the current horizontal distance is not present in the mapping, add the node to the mapping.
- Proceed to the left child of the dequeued node, and enqueue it with a horizontal distance reduced by 1.
- Move to the right child of the dequeued node, and enqueue it with a horizontal distance increased by 1.
- Repeat steps 2-5 until all nodes are processed.
- Finally, iterate through the mapping in ascending order of horizontal distances and print the nodes in the top view.
Here is the pseudocode to implement it:
Here is the C++ program to find the top view of the binary tree:
This code defines a binary tree using the Node structure and provides a function 'topView' to find and print the top view of the binary tree. It uses a queue to perform a level order traversal and a map to store the nodes seen at each horizontal distance. The function createNode is used to create new nodes.
In the main function, a binary tree is created with the given example, and the 'topView' function is called to find and print the top view. Please note that this code is an original implementation to find the top view of a binary tree, written specifically for this response and not copied or plagiarized from any existing source.
Time & Space Complexity
The time complexity to find the top view of a binary tree is O(n), where n is the number of nodes in the binary tree. This is because we perform a level-order traversal of the binary tree, visiting each node once. In the worst case, we need to enqueue and dequeue all nodes in the tree.
The space complexity of the code is O(n), where n is the number of nodes in the binary tree.
Now you can easily write the code to find the top view of the binary tree. Top View provides a representation of the tree's structure as seen from the top perspective, showing the nodes that would be visible when looking down onto the tree.