Skip to content

Instantly share code, notes, and snippets.

@dragneelfps
Created January 19, 2018 18:13
Show Gist options
  • Select an option

  • Save dragneelfps/516a918887c25b8f204eb419a75ba108 to your computer and use it in GitHub Desktop.

Select an option

Save dragneelfps/516a918887c25b8f204eb419a75ba108 to your computer and use it in GitHub Desktop.
Top, Bottom, Left and Right Views for a Graph
package graphs;
import java.util.*;
class Graph{
Node root = null;
Graph(){
}
static class Node{
int data;
Node left, right;
Node(int data){
this.data = data;
left = right = null;
}
}
}
class GraphViews extends Graph{
GraphViews() {
super();
spanMap = new TreeMap<>();
}
int depth = -1;
int span = 0;
SortedMap<Integer,Integer> spanMap;
void leftView(Node root,int depth) {
if(root == null) return;
if(depth > this.depth) {
System.out.println(root.data);
this.depth = depth;
}
leftView(root.left, depth+1);
leftView(root.right, depth + 1);
}
void rightView(Node root, int depth) {
if(root == null) return;
if(depth > this.depth) {
System.out.println(root.data);
this.depth = depth;
}
rightView(root.right, depth + 1);
rightView(root.left, depth + 1);
}
void topView(Node root, int span) {
if(root == null) return;
if(!spanMap.containsKey(span)) {
spanMap.put(span,root.data);
}
topView(root.left,span-1);
topView(root.right,span+1);
}
void bottomView(Node root, int span,int depth) {
if(root == null) return;
if(depth > this.depth) {
spanMap.put(span, root.data);
}
bottomView(root.left,span-1,depth+1);
bottomView(root.right, span+1,depth+1);
}
void reset() {
span = 0;
depth = -1;
spanMap.clear();
}
}
public class SideViews {
public static void main(String[] args) {
GraphViews g = new GraphViews();
g.root = new Graph.Node(1);
g.root.left = new Graph.Node(2);
g.root.right = new Graph.Node(3);
g.root.left.left = new Graph.Node(4);
g.root.right.right = new Graph.Node(5);
g.root.right.right.right = new Graph.Node(6);
g.root.left.right = new Graph.Node(7);
g.root.left.right.left = new Graph.Node(8);
g.root.right.left = new Graph.Node(9);
g.root.right.right.left = new Graph.Node(10);
g.root.right.right.left.left = new Graph.Node(11);
// 1
// / \
// 2 3
// / \ / \
// 4 7 9 5
// / / \
// 8 10 6
// /
// 11
System.out.println("Left View");
g.leftView(g.root, 0);
g.reset();
System.out.println("Right View");
g.rightView(g.root, 0);
g.reset();
System.out.println("Top View");
g.topView(g.root, 0);
for(int key: g.spanMap.keySet()) {
// System.out.print("(" + key + "," + g.spanMap.get(key) + ") ");
System.out.print(g.spanMap.get(key) + " ");
}
System.out.println();
g.reset();
System.out.println("Bottom View");
g.bottomView(g.root, 0,0);
for(int key: g.spanMap.keySet()) {
// System.out.print("(" + key + "," + g.spanMap.get(key) + ") ");
System.out.print(g.spanMap.get(key) + " ");
}
//OUTPUT:
//Left View
//1
//2
//4
//8
//11
//Right View
//1
//3
//5
//6
//11
//Top View
//4 2 1 3 5 6
//Bottom View
//4 8 11 10 5 6
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment