Professional Documents
Culture Documents
Values in Life
Values in Life
Heap Sort
import java.util.*;
2.Winner tree
import java.util.*;
3.BFS
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
class Graph {
int vertices;
LinkedList<Integer>[] adjList;
@SuppressWarnings("unchecked")
Graph(int vertices) {
this.vertices = vertices;
adjList = new LinkedList[vertices];
for (int i = 0; i < vertices; ++i)
adjList[i] = new LinkedList<>();
}
visited[startNode] = true;
queue.add(startNode);
while (!queue.isEmpty()) {
int currentNode = queue.poll();
System.out.print(currentNode + " ");
for (int neighbor : adjList[currentNode]) {
if (!visited[neighbor]) {
visited[neighbor] = true;
queue.add(neighbor);
}
}
}
}
}
System.out.print("BFS : " );
graph.bfs(0);
scanner.close();
}
}
}
import java.util.*;
import java.util.Map.Entry;
class Node {
int data, hd;
Node left, right;
4.DFS
import java.util.*;
@SuppressWarnings("unchecked")
Graph(int v) {
V = v;
adj = new LinkedList[v];
for (int i = 0; i < v; ++i)
adj[i] = new LinkedList();
}
Iterator<Integer> i = adj[v].listIterator();
while (i.hasNext()) {
int n = i.next();
if (!visited[n])
DFSUtil(n, visited);
}
}
void DFS(int v) {
boolean visited[] = new boolean[V];
DFSUtil(v, visited);
}
}
g.DFS(0);
}
}
5.All views
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);
}
}
}
class Node {
int data;
Node left, right;
static void preOrderTraversal(Node root, long hd, long vd, TreeMap<Long, Vector<Integer> >
m){
if (root == null)
return;
long val = hd << 30 | vd;
if (m.get(val) != null)
m.get(val).add(root.data);
else {
Vector<Integer> v = new Vector<Integer>();
v.add(root.data);
m.put(val, v);
}
preOrderTraversal(root.left, hd - 1, vd + 1, m);
preOrderTraversal(root.right, hd + 1, vd + 1, m);
}
7.Bellman ford
import java.util.*;
class Solution
{
class Edge
{
int src, dest, weight;
Edge()
{
src = dest = weight = 0;
}
};
int V, E;
Edge edge[];
Solution(int v, int e)
{
V = v;
E = e;
edge = new Edge[e];
for (int i = 0; i < e; ++i)
edge[i] = new Edge();
}
void BellmanFord(Solution graph, int src)
{
int V = graph.V, E = graph.E;
int dist[] = new int[V];
for (int i = 0; i < V; ++i)
dist[i] = Integer.MAX_VALUE;
dist[src] = 0;
for (int i = 1; i < V; ++i)
{
for (int j = 0; j < E; ++j)
{
int u = graph.edge[j].src;
int v = graph.edge[j].dest;
int weight = graph.edge[j].weight;
if (dist[u] != Integer.MAX_VALUE && dist[u] + weight < dist[v])
dist[v] = dist[u] + weight;
}
}
for (int j = 0; j < E; ++j)
{
int u = graph.edge[j].src;
int v = graph.edge[j].dest;
int weight = graph.edge[j].weight;
if (dist[u] != Integer.MAX_VALUE && dist[u] + weight < dist[v])
{
System.out.println(-1);
return;
}
}
for (int i = 0; i < V; ++i)
if (dist[i] != Integer.MAX_VALUE)
System.out.print(dist[i] + " ");
else
System.out.print(-1 + " ");
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int V = sc.nextInt();
int E = sc.nextInt();
Solution graph = new Solution(V, E);
for (int i = 0; i < E; i++)
{
int u = sc.nextInt();
int v = sc.nextInt();
int w = sc.nextInt();
graph.edge[i].src = u;
graph.edge[i].dest = v;
graph.edge[i].weight = w;
}
graph.BellmanFord(graph, 0);
}
}
8.Boundary traversal
import java.util.*;
TreeNode(int val) {
this.val = val;
left = right = null;
}
}
9.Binary heap
import java.util.*;
class TreeNode {
int val;
TreeNode left, right;
TreeNode(int val) {
this.val = val;
left = null;
right = null;
}
}
while (!queue.isEmpty()) {
int size = queue.size();
TreeNode rightmost = null;
return result;
}
if (scanner.hasNextInt()) {
root = new TreeNode(scanner.nextInt());
queue.offer(root);
}
while (!queue.isEmpty()) {
TreeNode current = queue.poll();
if (scanner.hasNext()) {
String leftValue = scanner.next();
if (!leftValue.equals("null")) {
current.left = new TreeNode(Integer.parseInt(leftValue));
queue.offer(current.left);
}
}
if (scanner.hasNext()) {
String rightValue = scanner.next();
if (!rightValue.equals("null")) {
current.right = new TreeNode(Integer.parseInt(rightValue));
queue.offer(current.right);
}
}
}
return root;
}
}
// Driver code
public static void main(String[] args)
{
// Number of nodes
int V = 4;
Scanner sc = new Scanner(System.in);
// Edges
topologicalSort(adj, V);
}
}
11. Recover BST
import java.io.*;
import java.util.*;
class Node {
int data;
Node left;
Node right;
public Node(int d) {
data = d;
left = null;
right = null;
}
}
int ind = 0;
int data = Integer.valueOf(nodes.get(ind++));
if(!nodes.get(ind).equals("N")) {
data = Integer.valueOf(nodes.get(ind));
curr.left = new Node(data);
q.offer(curr.left);
}
ind++;
if(ind == nodes.size())
break;
if(!nodes.get(ind).equals("N")) {
data = Integer.valueOf(nodes.get(ind));
curr.right = new Node(data);
q.offer(curr.right);
}
ind++;
}
}
return root;
}
if(curr.left != null)
q.offer(curr.left);
if(curr.right != null)
q.offer(curr.right);
}
}
}
prev = root;
recover(root.right);
}
// System.out.println(Integer.valueOf("23"));
Node root = buildTree(nodes);
recover(root);
int temp = from.data;
from.data = to.data;
to.data = temp;
preOrder(root);
}
}
12. Dials
import java.util.*;
public class Graph {
static final int INF = Integer.MAX_VALUE;
private int V;
private ArrayList<ArrayList<Tuple> > adj;
public Graph(int v){
this.V = v;
this.adj = new ArrayList<ArrayList<Tuple> >();
for (int i = 0; i < v; i++)
this.adj.add(new ArrayList<Tuple>());
}
public void AddEdge(int u, int v, int w){
adj.get(u).add(new Tuple(v, w));
adj.get(v).add(new Tuple(u, w));
}
public void shortestPath(int src, int W){
int[] dist = new int[V];
Arrays.fill(dist, INF);
ArrayList<Integer>[] B = new ArrayList[W * V + 1];
for (int i = 0; i < W * V + 1; i++)
B[i] = new ArrayList<Integer>();
B[0].add(src);
dist[src] = 0;
int idx = 0;
while (true) {
while (B[idx].size() == 0 && idx < W * V)
idx++;
if (idx == W * V)
break;
int u = B[idx].get(0);
B[idx].remove(0);
for (Tuple i : adj.get(u)) {
int v = i.v;
int weight = i.w;
int du = dist[u];
int dv = dist[v];
if (dv > du + weight) {
dist[v] = du + weight;
dv = dist[v];
B[dv].add(0, v);
}
}
}
13. K-ary
import java.util.*;
public class KaryHeap {
buildHeap(arr, n, k);
int element = 3;
insert(arr, n, k, element);
n++;
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;
if (maxChild == -1)
break;
index = maxChildIndex;
}
}