VIEWS OF TREE
VIEWS OF TREE
EXPLANATION
In a binary tree, a "view" refers to a specific perspective or
representation of the tree's nodes. Different views highlight different
aspects of the tree's structure or content, such as nodes visible from the
top, bottom, left, or right. These views help analyze and understand the
tree's characteristics from various angles.
VIEWS OF TREE
EXPLANATION
Types of View in Binary Tree are :
❖ Horizontal View
❖ Vertical View
❖ Left View
❖ Right View
❖ Top View
❖ Bottom View
VIEWS OF TREE
Horizontal View:
The horizontal view of a binary tree shows the nodes from left to right at each
level. Nodes at the same level are listed in the order of their appearance, from
left to right.
Example:
A
/ \
B C
/ \ / \
D E F G
Horizontal View:A B C D E F G
VIEWS OF TREE
import java.util.*; Queue<TreeNode> queue = new
class TreeNode { LinkedList<>();
char val; queue.offer(root);
TreeNode left; while (!queue.isEmpty()) {
TreeNode right; int levelSize =
queue.size();
public TreeNode(char val) { for (int i = 0; i < levelSize; i+
this.val = val; +) {
left = null; TreeNode node =
right = null; queue.poll();
}}
public class Main { horizontalView.add(node.val);
// Function to obtain the if (node.left !=
Horizontal View of a binary tree null) {
public static List<Character>
horizontalView(TreeNode root) { queue.offer(node.left);
List<Character> }
horizontalView = new ArrayList<>(); if (node.right !=
if (root == null) { null) {
return horizontalView;
} queue.offer(node.right);
}
VIEWS OF TREE
return horizontalView; List<Character> horizontalViewResult
} = horizontalView(root);
public static void main(String[] // Printing the Horizontal
args) { View
// Sample binary tree input System.out.print("Horizontal
TreeNode root = new View: ");
TreeNode('A'); for (char node :
root.left = new horizontalViewResult) {
TreeNode('B'); System.out.print(node + "
root.right = new ");
TreeNode('C'); }
root.left.left = new System.out.println();
TreeNode('D'); }
root.left.right = new }
TreeNode('E');
root.right.left = new
TreeNode('F');
root.right.right = new
TreeNode('G');
VIEWS OF TREE
Vertical View:
The Vertical View displays nodes at the same vertical level.
It shows the tree's structure from top to bottom.
Example:
A
/ \
B C
/ / \
D E F
Vertical View:
D B A E C F
VIEWS OF TREE
import java.util.*; Map<Integer, List<Character>>
class TreeNode { verticalMap = new TreeMap<>();
char val; Queue<TreeNode> nodeQueue =
TreeNode left; new LinkedList<>();
TreeNode right; Queue<Integer> hdQueue = new
public TreeNode(char val) { LinkedList<>();
this.val = val; nodeQueue.offer(root);
left = null; hdQueue.offer(0);
right = null; while (!nodeQueue.isEmpty()) {
} TreeNode node =
} nodeQueue.poll();
public class Main { int hd = hdQueue.poll();
// Function to obtain the verticalMap.computeIfAbsent(hd, k ->
Vertical View of a binary tree new ArrayList<>()).add(node.val);
public static List<Character> if (node.left != null) {
verticalView(TreeNode root) {
List<Character> verticalView nodeQueue.offer(node.left);
= new ArrayList<>(); hdQueue.offer(hd -
if (root == null) { 1);
return verticalView; }
}
VIEWS OF TREE
if (node.right != null) { root.left.left = new TreeNode('D');
root.right.left = new
nodeQueue.offer(node.right); TreeNode('E');
hdQueue.offer(hd + root.right.right = new
1); TreeNode('F');
}
} List<Character>
for (List<Character> values : verticalViewResult =
verticalMap.values()) { verticalView(root);
verticalView.addAll(values); // Printing the Vertical View
} System.out.print("Vertical
return verticalView; View: ");
} for (char node :
public static void main(String[] verticalViewResult) {
args) { System.out.print(node + "
// Sample binary tree input ");
TreeNode root = new }
TreeNode('A'); System.out.println();
root.left = new }
TreeNode('B'); }
root.right = new
VIEWS OF TREE
Left view:
The Left View provides a view of the nodes seen from the left side of the tree.
It includes the leftmost node at each level.
Example:
A
/ \
B C
/ \ / \
D E F G
Left View: A B D
VIEWS OF TREE
import java.util.*; if (root == null) {
class TreeNode { return leftView;
char val; }
TreeNode left; Queue<TreeNode> nodeQueue =
TreeNode right; new LinkedList<>();
nodeQueue.offer(root);
public TreeNode(char val) { while (!nodeQueue.isEmpty())
this.val = val; {
left = null; int levelSize =
right = null; nodeQueue.size();
} for (int i = 0; i < levelSize;
} i++) {
public class Main { TreeNode node =
// Function to obtain the Left nodeQueue.poll();
View of a binary tree // Add the leftmost
public static List<Character> node at each level to the leftView
leftView(TreeNode root) { list
List<Character> leftView = new if (i == 0) {
ArrayList<>(); leftView.add(node.val);
}
if (node.left !=
null)
VIEWS OF TREE
if (node.right != null) { root.left.right = new TreeNode('E');
root.right.left = new
nodeQueue.offer(node.right); TreeNode('F');
} root.right.right = new
} TreeNode('G');
} List<Character>
return leftView; leftViewResult = leftView(root);
} // Printing the Left View
public static void main(String[] System.out.print("Left View:
args) { ");
// Sample binary tree input for (char node :
TreeNode root = new leftViewResult) {
TreeNode('A'); System.out.print(node + "
root.left = new ");
TreeNode('B'); }
root.right = new System.out.println();
TreeNode('C'); }
root.left.left = new }
TreeNode('D');
VIEWS OF TREE
Right view:
The Right View offers a view of the nodes seen from the right side of the tree.
It includes the rightmost node at each level.
Example:
A
/ \
B C
/ \ / \
D E F G
Right View: A C G
VIEWS OF TREE
import java.util.*; Queue<TreeNode> queue = new
class TreeNode { LinkedList<>();
char val; queue.offer(root);
TreeNode left; while (!queue.isEmpty()) {
TreeNode right; int levelSize =
public TreeNode(char val) { queue.size();
this.val = val; for (int i = 0; i <
left = null; levelSize; i++) {
right = null; TreeNode node =
} queue.poll();
} // Add the rightmost
public class Main { node at each level to the result
// Function to obtain the Right if (i == levelSize - 1)
View of a binary tree {
public static List<Character>
rightView(TreeNode root) { rightView.add(node.val);
List<Character> rightView = new }
ArrayList<>(); if (node.left != null)
if (root == null) { {
return rightView; queue.offer(node.left);
} }
VIEWS OF TREE
if (node.right != null) { root.left.right = new TreeNode('E');
root.right.left = new
queue.offer(node.right); TreeNode('F');
} root.right.right = new
} TreeNode('G');
} List<Character> rightViewResult
return rightView; = rightView(root);
} // Printing the Right View
public static void main(String[] System.out.print("Right View:
args) { ");
// Sample binary tree input for (char node :
TreeNode root = new rightViewResult) {
TreeNode('A'); System.out.print(node + "
root.left = new TreeNode('B'); ");
root.right = new TreeNode('C'); }
root.left.left = new System.out.println();
TreeNode('D'); }
}
VIEWS OF TREE
Top view:
The Top View displays nodes visible from the top of the tree when viewed from
above.
It shows the outermost nodes at each vertical level.
Example:
A
/ \
B C
/ / \
D E F
Top View: D B A C F
VIEWS OF TREE
import java.util.*;
class TreeNode { public class Main {
char val; // Function to obtain the Top View
TreeNode left; of a binary tree
TreeNode right; public static List<Character>
public TreeNode(char val) { topView(TreeNode root) {
this.val = val; List<Character> topView = new
left = null; ArrayList<>();
right = null; if (root == null) {
} return topView;
} }
class Pair { Map<Integer, Character>
TreeNode node; verticalMap = new TreeMap<>();
int hd; Queue<Pair> queue = new
public Pair(TreeNode node, int hd) LinkedList<>();
{ queue.offer(new Pair(root, 0));
this.node = node; while (!queue.isEmpty()) {
this.hd = hd; Pair pair = queue.poll();
} TreeNode node = pair.node;
}
VIEWS OF TREE
int hd = pair.hd; public static void main(String[] args)
if (! {
verticalMap.containsKey(hd)) { // Sample binary tree input
verticalMap.put(hd, TreeNode root = new
node.val); TreeNode('A');
} root.left = new TreeNode('B');
if (node.left != null) { root.right = new TreeNode('C');
queue.offer(new root.left.left = new
Pair(node.left, hd - 1)); TreeNode('D');
} root.right.left = new
if (node.right != null) { TreeNode('E');
queue.offer(new root.right.right = new
Pair(node.right, hd + 1)); TreeNode('F');
} List<Character> topViewResult =
} topView(root);
for (char nodeVal : // Printing the Top View
verticalMap.values()) { System.out.print("Top View: ");
topView.add(nodeVal); for (char node : topViewResult)
} {
return topView; System.out.print(node + "
} ");
}
VIEWS OF TREE
Bottom view:
The Bottom View showcases nodes visible from the bottom of the tree when viewed
from below.
It shows the nodes at the lowest vertical level.
Example:
A
/ \
B C
/ / \
D E F
\
G
VIEWS OF TREE
import java.util.*; public class Main {
class TreeNode { // Function to obtain the Bottom
char val; View of a binary tree
TreeNode left; public static List<Character>
TreeNode right; bottomView(TreeNode root) {
public TreeNode(char val) { List<Character> bottomView =
this.val = val; new ArrayList<>();
left = null; if (root == null) {
right = null; return bottomView;
} }
} Map<Integer, Character>
class NodeInfo { bottomMap = new TreeMap<>();
TreeNode node; Queue<NodeInfo> nodeQueue = new
int hd; LinkedList<>();
public NodeInfo(TreeNode node, int nodeQueue.offer(new
hd) { NodeInfo(root, 0));
this.node = node; while (!nodeQueue.isEmpty()) {
this.hd = hd; NodeInfo info =
} nodeQueue.poll();
}
VIEWS OF TREE
TreeNode node = info.node; TreeNode root = new TreeNode('A');
int hd = info.hd; root.left = new TreeNode('B');
bottomMap.put(hd, root.right = new TreeNode('C');
node.val); root.left.left = new
if (node.left != null) { TreeNode('D');
nodeQueue.offer(new root.right.left = new
NodeInfo(node.left, hd - 1)); TreeNode('E');
} root.right.right = new
if (node.right != null) { TreeNode('F');
nodeQueue.offer(new root.right.right.right = new
NodeInfo(node.right, hd + 1)); TreeNode('G');
} List<Character>
} bottomViewResult = bottomView(root);
for (char value : // Printing the Bottom View
bottomMap.values()) { System.out.print("Bottom View:
bottomView.add(value); ");
} for (char node :
return bottomView; bottomViewResult) {
} System.out.print(node + "
public static void main(String[] ");
args) { }System.out.println();
}}
INTERVIEW QUESTIONS
1.What is the significance of the "Left View" of a Binary Tree?
Answer: The Left View displays the leftmost nodes at each
level, providing a perspective of nodes seen from the left side of
the tree.
INTERVIEW QUESTIONS
2.How is the "Bottom View" of a Binary Tree determined?
Answer: The Bottom View shows nodes at the lowest vertical
level when viewed from below the tree, including the nodes that
would be visible from the bottom.
INTERVIEW QUESTIONS
3.In the "Top View" of a Binary Tree, what does the outermost node
at each vertical level represent?
Answer: The outermost node in the Top View represents the
nodes visible from the top when viewed from above the tree.
INTERVIEW QUESTIONS
4.What is the primary use of the "Horizontal View" of a Binary
Tree?
Answer: The Horizontal View helps visualize the tree's
structure from left to right, emphasizing nodes at the same
horizontal distance from the root.
INTERVIEW QUESTIONS
5.How can the "Vertical View" of a Binary Tree be described?
Answer: The Vertical View displays nodes at the same vertical
level, providing a perspective of the tree's structure from top to
bottom.
/ Ethnus /ethnus /
ethnuscodemith Codemithra code_mithra
ra
https://
learn.codemithra.com
om 095 340