Professional Documents
Culture Documents
import java.util.*;
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int val) {
this.val = val;
}
}
View of tree
import java.util.*;
import java.util.Map.Entry;
class Node {
int data, hd;
Node left, right;
// Right View
void rightview(Node root) {
if (root == null)
return;
Queue<Node> q = new LinkedList<>();
q.add(root);
while (!q.isEmpty()) {
int n = q.size();
for (int i = 0; i < n; i++) {
Node curr = q.peek();
q.remove();
if (i == n - 1) {
System.out.print(curr.data + " ");
}
if (curr.left != null)
q.add(curr.left);
if (curr.right != null)
q.add(curr.right);
}
}
}
// Left View
void leftview(Node root) {
if (root == null)
return;
Queue<Node> queue = new LinkedList<>();
queue.add(root);
while (!queue.isEmpty()) {
int n = queue.size();
for (int i = 1; i <= n; i++) {
Node temp = queue.poll();
if (i == 1)
System.out.print(temp.data + " ");
if (temp.left != null)
queue.add(temp.left);
if (temp.right != null)
queue.add(temp.right);
}
}
}
// Top View
void topview(Node root) {
if (root == null)
return;
Queue<QueueObj> q = new LinkedList<>();
Map<Integer, Integer> map = new HashMap<>();
int min = 0;
int max = 0;
q.add(new QueueObj(root, 0));
while (!q.isEmpty()) {
QueueObj curr = q.poll();
if (!map.containsKey(curr.hd))
map.put(curr.hd, curr.node.data);
if (curr.node.left != null) {
min = Math.min(min, curr.hd - 1);
q.add(new QueueObj(curr.node.left, curr.hd - 1));
}
if (curr.node.right != null) {
max = Math.max(max, curr.hd + 1);
q.add(new QueueObj(curr.node.right, curr.hd + 1));
}
}
for (; min <= max; min++)
System.out.print(map.get(min) + " ");
}
// Bottom View
void bottomview(Node root) {
if (root == null)
return;
int hd = 0;
Map<Integer, Integer> map = new TreeMap<>();
Queue<Node> queue = new LinkedList<Node>();
root.hd = hd;
queue.add(root);
while (!queue.isEmpty()) {
Node temp = queue.remove();
hd = temp.hd;
map.put(hd, temp.data);
if (temp.left != null) {
temp.left.hd = hd - 1;
queue.add(temp.left);
}
if (temp.right != null) {
temp.right.hd = hd + 1;
queue.add(temp.right);
}
}
for (Map.Entry<Integer, Integer> me : map.entrySet()) {
System.out.print(me.getValue() + " ");
}
}
System.out.println("Right View:");
main.rightview(main.root);
System.out.println();
System.out.println("Left View:");
main.leftview(main.root);
System.out.println();
System.out.println("Top View:");
main.topview(main.root);
System.out.println();
System.out.println("Bottom View:");
main.bottomview(main.root);
}
}
Vertical Order Traversal
import java.util.*;
import java.util.AbstractMap.SimpleEntry;
class TreeNode {
int val;
TreeNode left;
TreeNode right;
while (!nodeQueue.isEmpty()) {
SimpleEntry<TreeNode, Integer> entry = nodeQueue.poll();
TreeNode node = entry.getKey();
int col = entry.getValue();
verticalMap.computeIfAbsent(col, k -> new ArrayList<>()).add(node.val);
if (node.left != null) {
nodeQueue.offer(new SimpleEntry<>(node.left, col - 1));
}
if (node.right != null) {
nodeQueue.offer(new SimpleEntry<>(node.right, col + 1));
}
}
Boundary Traversal
import java.util.*;
class Node {
int data;
Node left, right;
Node(int item)
{
data = item;
left = right = null;
}
}
class Main {
static Node root;
static void printLeaves(Node node)
{
if (node == null)
return;
printLeaves(node.left);
if (node.left == null && node.right == null)
System.out.print(node.data + " ");
printLeaves(node.right);
}
static void printBoundaryLeft(Node node)
{
if (node == null)
return;
if (node.left != null) {
System.out.print(node.data + " ");
printBoundaryLeft(node.left);
}
else if (node.right != null) {
System.out.print(node.data + " ");
printBoundaryLeft(node.right);
}
}
static void printBoundaryRight(Node node)
{
if (node == null)
return;
if (node.right != null) {
printBoundaryRight(node.right);
System.out.print(node.data + " ");
}
else if (node.left != null) {
printBoundaryRight(node.left);
System.out.print(node.data + " ");
}
}
static void printBoundary(Node node)
{
if (node == null)
return;
System.out.print(node.data + " ");
printBoundaryLeft(node.left);
printLeaves(node.left);
printLeaves(node.right);
printBoundaryRight(node.right);
}
static Node insert(Node root, int data) {
if (root == null)
root = new Node(data);
else if (root.data < data)
root.right = insert(root.right, data);
else
root.left = insert(root.left, data);
return root;
}
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int input = sc.nextInt();
while(input >= 0) {
root = insert(root, input);
input = sc.nextInt();
}
printBoundary(root);
} }
BFS
import java.util.*;
BFS(int v) {
V = v;
adj = new LinkedList[v];
for (int i = 0; i < v; i++) {
adj[i] = new LinkedList();
}
}
void bFS(int s) {
boolean visited[] = new boolean[V];
Queue<Integer> queue = new LinkedList<Integer>();
visited[s] = true;
queue.add(s);
while (queue.size() != 0) {
s = queue.poll();
System.out.print(s + " ");
Iterator<Integer> i = adj[s].listIterator();
while (i.hasNext()) {
int n = i.next();
if (visited[n] == false) {
visited[n] = true;
queue.add(n);
}
}
}
}
DFS
import java.util.*;
DFS(int v) {
V = v;
adj = new LinkedList[v];
for (int i = 0; i < v; i++) {
adj[i] = new LinkedList();
}
}
Iterator<Integer> i = adj[s].listIterator();
while (i.hasNext()) {
int n = i.next();
if (visited[n] == false)
dFSrec(n, visited);
}
}
void dFS(int s) {
boolean visited[] = new boolean[V];
dFSrec(s, visited);
}
Dial’s Algorithm
import java.util.*;
class Graph {
private int V;
private List<List<Node>> adj;
public Graph(int V) {
this.V = V;
adj = new ArrayList<>(V);
for (int i = 0; i < V; i++) {
adj.add(new ArrayList<>());
}
}
while (!pq.isEmpty()) {
int currentVertex = pq.poll().vertex;
for (Node neighbor : adj.get(currentVertex)) {
int newDist = distance[currentVertex] + neighbor.weight;
if (newDist < distance[neighbor.vertex]) {
distance[neighbor.vertex] = newDist;
pq.add(new Node(neighbor.vertex, newDist));
}
}
}
Bellman-Ford Algorithm
import java.util.*;
class Main {
class Edge {
int src, dest, weight;
Edge() {
src = dest = weight = 0;
}
};
int V, E;
Edge edge[];
Main(int v, int e) {
V = v;
E = e;
edge = new Edge[e];
for (int i = 0; i < e; ++i)
edge[i] = new Edge();
}
import java.util.*;
TopologicalSort(int v) {
V = v;
adj = new LinkedList[v];
for (int i = 0; i < v; i++) {
adj[i] = new LinkedList();
}
}
graph.TopoSort();
}
}
Heap Sort
import java.util.*;
BINOMIAL HEAP
import java.util.*;
class BinomialHeapNode {
int key, degree;
BinomialHeapNode parent;
BinomialHeapNode sibling;
BinomialHeapNode child;
public BinomialHeapNode(int k) {
key = k;
degree = 0;
parent = null;
sibling = null;
child = null;
}
public BinomialHeap() {
Nodes = null;
size = 0;
}
Winner Tree
import java.util.*;
class Node {
int idx;
Node left, right;
}
K-ARY HEAP
import java.util.Scanner;
buildHeap(arr, n, k);
System.out.println("Built Heap: ");
for (int i = 0; i < n; i++)
System.out.print(arr[i] + " ");
System.out.println();
public static void restoreDown(int[] arr, int len, int index, int k) {
int[] child = new int[k + 1];
while (true) {
for (int i = 1; i <= k; i++)
child[i] = (k * index + i) < len ? (k * index + i) : -1;
int maxChild = -1, maxChildIndex = -1;
for (int i = 1; i <= k; i++) {
if (child[i] != -1 && arr[child[i]] > maxChild) {
maxChildIndex = child[i];
maxChild = arr[child[i]];
}
}
if (maxChild == -1)
break;
if (arr[index] < arr[maxChildIndex])
swap(arr, index, maxChildIndex);
index = maxChildIndex;
}
}
HORIZONTAL VIEW
import java.util.*;
class TreeNode {
char val;
TreeNode left;
TreeNode right;
WINNER + LOSER
import java.util.Arrays;
import java.util.Scanner;