Top View of Tree šŸŽÆ

beginner
13 min

Top View of Tree šŸŽÆ

Welcome to our comprehensive guide on the Top View of a Tree! In this lesson, we'll explore a fascinating concept in Data Structures and Algorithms. We'll walk you through the process of understanding and implementing the top view of a tree, with practical examples and explanations. By the end of this lesson, you'll have a solid understanding of this concept, which can be useful in various real-world projects. šŸ’”

What is a Tree? šŸ“

Before diving into the top view, let's quickly review what a tree is. A tree is a type of data structure that consists of nodes, where each node has a unique value and can be connected to zero or more other nodes, forming a hierarchical structure.

Understanding the Top View šŸ’”

The top view of a tree is a horizontal cross-section of the tree taken from the top. It is also known as the left view or right view, depending on whether we're considering the left or rightmost node at each height. In this lesson, we'll focus on the left top view.

Example Tree šŸ“

Let's consider the following example tree:

1 / \ 2 3

Implementing the Top View šŸ’”

To implement the top view, we'll use a Morris Traversal, which is a recursive, in-place, and efficient method to traverse a binary tree. Here's a step-by-step guide on how to implement the top view using Morris Traversal:

  1. Initialize an empty stack S and a node current to the root of the tree.

  2. While the stack is not empty or current is not null:

    a. If current is not null, push current to the stack S and set current to the left child of current.

    b. If the stack is not empty, pop the top element temp from the stack S.

    i. Print the value of temp as the left top view element.

    ii. Set current to the right child of temp and move to step 2a.

  3. If the tree is not empty, the left top view has been printed.

Working with Code Examples šŸ’”

Now that we understand the concept, let's put it into practice with some code examples.

Example 1: Implementing Top View of a Single Tree šŸ“

In this example, we'll implement the top view for a single tree:

python
class Node: def __init__(self, key): self.left = None self.right = None self.val = key def printTopView(root): if root is None: return # Create an empty dictionary to store the data result = {} # Morris traversal to get the top view current = root while current: if current.left is None: # If current's left is None, print the current node and move to right print(current.val, end=" ") current = current.right else: # Find the lowest node in the left subtree predecessor = current.left while predecessor and predecessor.right != current: predecessor = predecessor.right # Reverse the direction of the current node for the next iteration if predecessor: predecessor.right = None current = predecessor else: # If there's no predecessor, move to left current = current.left # Driver code root = Node(1) root.left = Node(2) root.right = Node(3) print("Top View is: ") printTopView(root)

Example 2: Implementing Top View of a Balanced Binary Tree šŸ“

In this example, we'll implement the top view for a balanced binary tree:

python
class Node: def __init__(self, key): self.left = None self.right = None self.val = key def printTopView(root): if root is None: return # Create an empty dictionary to store the data result = {} # Morris traversal to get the top view current = root while current: if current.left is None: # If current's left is None, print the current node and move to right if current.val not in result: print(current.val, end=" ") result[current.val] = 1 current = current.right else: # Find the lowest node in the left subtree predecessor = current.left while predecessor and predecessor.right != current: predecessor = predecessor.right # Reverse the direction of the current node for the next iteration if predecessor: predecessor.right = None current = predecessor else: # If there's no predecessor, move to left current = current.left # Driver code root = Node(1) root.left = Node(2) root.right = Node(3) root.left.left = Node(4) root.left.right = Node(5) root.right.right = Node(6) print("Top View is: ") printTopView(root)

Quiz Time šŸ’”

Quick Quiz
Question 1 of 1

Which data structure forms the basis of our top view implementation?

Wrapping Up āœ…

Congratulations on learning about the top view of a tree! We've covered the basics, explored example code, and even took a short quiz. Keep practicing and you'll be on your way to mastering this fascinating concept. Happy coding! šŸ’”