Facebook
From xyz, 1 Month ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 117
  1. Bellmon ford
  2. import java.util.Scanner;
  3.  
  4. public class Main {
  5.     static class CreateGraph {
  6.         class CreateEdge {
  7.             int src, dest, weight;
  8.  
  9.             CreateEdge(int src, int dest, int weight) {
  10.                 this.src = src;
  11.                 this.dest = dest;
  12.                 this.weight = weight;
  13.             }
  14.         }
  15.  
  16.         int V, E;
  17.         CreateEdge edge[];
  18.  
  19.         CreateGraph(int v, int e) {
  20.             V = v;
  21.             E = e;
  22.             edge = new CreateEdge[e];
  23.         }
  24.  
  25.         void BellmanFord(int src) {
  26.             int dist[] = new int[V];
  27.             for (int i = 0; i < V; ++i)
  28.                 dist[i] = Integer.MAX_VALUE;
  29.             dist[src] = 0;
  30.             for (int i = 1; i < V; ++i) {
  31.                 for (int j = 0; j < E; ++j) {
  32.                     int u = edge[j].src;
  33.                     int v = edge[j].dest;
  34.                     int w = edge[j].weight;
  35.                     if (dist[u] != Integer.MAX_VALUE && dist[u] + w < dist[v])
  36.                         dist[v] = dist[u] + w;
  37.                 }
  38.             }
  39.             for (int j = 0; j < E; ++j) {
  40.                 int u = edge[j].src;
  41.                 int v = edge[j].dest;
  42.                 int w = edge[j].weight;
  43.                 if (dist[u] != Integer.MAX_VALUE && dist[u] + w < dist[v]) {
  44.                     System.out.println("Graph contains negative weight cycle");
  45.                     return;
  46.                 }
  47.             }
  48.             printSolution(dist);
  49.         }
  50.  
  51.         void printSolution(int dist[]) {
  52.             System.out.println("Vertex Distance from Source");
  53.             for (int i = 0; i < V; ++i)
  54.                 System.out.println(i + "tt" + dist[i]);
  55.         }
  56.     }
  57.  
  58.     public static void main(String[] args) {
  59.         Scanner scanner = new Scanner(System.in);
  60.         System.out.print("Enter the number of vertices: ");
  61.         int V = scanner.nextInt();
  62.         System.out.print("Enter the number of edges: ");
  63.         int E = scanner.nextInt();
  64.  
  65.         CreateGraph graph = new CreateGraph(V, E);
  66.         System.out.println("Enter source, destination, and weight for each edge:");
  67.  
  68.         for (int i = 0; i < E; i++) {
  69.             int src = scanner.nextInt();
  70.             int dest = scanner.nextInt();
  71.             int weight = scanner.nextInt();
  72.             graph.edge[i] = graph.new CreateEdge(src, dest, weight);
  73.         }
  74.  
  75.         System.out.print("Enter the source vertex: ");
  76.         int sourceVertex = scanner.nextInt();
  77.         graph.BellmanFord(sourceVertex);
  78.     }
  79. }
  80.  
  81. OUTPUT
  82. Enter the number of vertices: 5
  83. Enter the number of edges: 8
  84. Enter source, destination, and weight for each edge:
  85. 0 1 -1
  86. 0 2 4
  87. 1 2 3
  88. 1 3 2
  89. 1 4 2
  90. 3 2 5
  91. 3 1 1
  92. 4 3 -3
  93. Enter the source vertex: 0
  94. Vertex Distance from Source
  95. 0  0
  96. 1  -1
  97. 2  2
  98. 3  -2
  99. 4  1
  100.  
  101.  
  102.  
  103. Belmon ford
  104. import java.util.*;
  105.  
  106. class Main {
  107.     static class Edge {
  108.         int src, dest, weight;
  109.  
  110.         Edge() {
  111.             src = dest = weight = 0;
  112.         }
  113.     }
  114.  
  115.     static class Graph {
  116.         int V, E;
  117.         Edge[] edge;
  118.  
  119.         Graph(int v, int e) {
  120.             V = v;
  121.             E = e;
  122.             edge = new Edge[e];
  123.             for (int i = 0; i < e; ++i)
  124.                 edge[i] = new Edge();
  125.         }
  126.  
  127.         void BellmanFord(Graph graph, int src) {
  128.             int V = graph.V, E = graph.E;
  129.             int dist[] = new int[V];
  130.             for (int i = 0; i < V; ++i)
  131.                 dist[i] = Integer.MAX_VALUE;
  132.             dist[src] = 0;
  133.             for (int i = 1; i < V; ++i) {
  134.                 for (int j = 0; j < E; ++j) {
  135.                     int u = graph.edge[j].src;
  136.                     int v = graph.edge[j].dest;
  137.                     int weight = graph.edge[j].weight;
  138.                     if (dist[u] != Integer.MAX_VALUE && dist[u] + weight < dist[v])
  139.                         dist[v] = dist[u] + weight;
  140.                 }
  141.             }
  142.             for (int j = 0; j < E; ++j) {
  143.                 int u = graph.edge[j].src;
  144.                 int v = graph.edge[j].dest;
  145.                 int weight = graph.edge[j].weight;
  146.                 if (dist[u] != Integer.MAX_VALUE && dist[u] + weight < dist[v]) {
  147.                     System.out.println(-1);
  148.                     return;
  149.                 }
  150.             }
  151.             for (int i = 0; i < V; ++i)
  152.                 if (dist[i] != Integer.MAX_VALUE)
  153.                     System.out.print(dist[i] + " ");
  154.                 else
  155.                     System.out.print(-1 + " ");
  156.         }
  157.     }
  158.  
  159.     public static void main(String[] args) {
  160.         Scanner sc = new Scanner(System.in);
  161.         int V = sc.nextInt();
  162.         int E = sc.nextInt();
  163.         Graph graph = new Graph(V, E);
  164.         for (int i = 0; i < E; i++) {
  165.             int u = sc.nextInt();
  166.             int v = sc.nextInt();
  167.             int w = sc.nextInt();
  168.             graph.edge[i].src = u;
  169.             graph.edge[i].dest = v;
  170.             graph.edge[i].weight = w;
  171.         }
  172.         graph.BellmanFord(graph, 0);
  173.         sc.close();
  174.     }
  175. }
  176. output
  177. 5 8
  178. 0 1 -1
  179. 0 2 4
  180. 1 2 3
  181. 1 3 2
  182. 1 4 2
  183. 3 2 5
  184. 3 1 1
  185. 4 3 -3
  186.  
  187.  
  188. BFS
  189. import java.util.*;
  190.  
  191. public class Main {
  192.     int V;
  193.     LinkedList<Integer> adj[];
  194.  
  195.     Main(int v) {
  196.         V = v;
  197.         adj = new LinkedList[v];
  198.         for (int i = 0; i < v; i++) {
  199.             adj[i] = new LinkedList<>();
  200.         }
  201.     }
  202.  
  203.     void addEdges(int v, int e) {
  204.         if (v < V && e < V) { // Check if vertices are within bounds
  205.             adj[v].add(e);
  206.             adj[e].add(v);
  207.         } else {
  208.             System.out.println("Vertices out of bounds: " + v + ", " + e);
  209.         }
  210.     }
  211.  
  212.     void BFS(int s) {
  213.         boolean visited[] = new boolean[V];
  214.         Queue<Integer> queue = new LinkedList<>();
  215.         visited[s] = true;
  216.         queue.add(s);
  217.  
  218.         while (!queue.isEmpty()) {
  219.             s = queue.poll();
  220.             System.out.print(s + " ");
  221.  
  222.             Iterator<Integer> i = adj[s].listIterator();
  223.             while (i.hasNext()) {
  224.                 int n = i.next();
  225.                 if (!visited[n]) {
  226.                     visited[n] = true;
  227.                     queue.add(n);
  228.                 }
  229.             }
  230.         }
  231.     }
  232.  
  233.     public static void main(String[] args) {
  234.         Scanner s = new Scanner(System.in);
  235.         int n = s.nextInt();
  236.         Main graph = new Main(n);
  237.         int v, e;
  238.         while (true) {
  239.             v = s.nextInt();
  240.             e = s.nextInt();
  241.             if (v == -1 && e == -1) {
  242.                 break;
  243.             }
  244.             graph.addEdges(v, e);
  245.         }
  246.         int S = s.nextInt();
  247.         System.out.print("BFS: ");
  248.         graph.BFS(S);
  249.     }
  250. }
  251.  
  252. OUTPUT
  253. 5
  254. 0 1
  255. 0 2
  256. 1 3
  257. 2 4
  258. 3 4
  259. -1 -1
  260. 0     //SOURCE
  261. BFS: 0 1 2 3 4
  262.  
  263. DFS
  264. import java.util.*;
  265.  
  266. public class Main {
  267.     int V;
  268.     LinkedList<Integer> adj[];
  269.  
  270.     Main(int v) {
  271.         V = v;
  272.         adj = new LinkedList[V];
  273.         for (int i = 0; i < V; i++) {
  274.             adj[i] = new LinkedList<>();
  275.         }
  276.     }
  277.  
  278.     void addEdges(int v, int e) {
  279.         adj[v].add(e);
  280.         adj[e].add(v);
  281.     }
  282.  
  283.     void DFSrec(int s, boolean[] visited) {
  284.         visited[s] = true;
  285.         System.out.print(s + " ");
  286.  
  287.         Iterator<Integer> i = adj[s].listIterator();
  288.         while (i.hasNext()) {
  289.             int n = i.next();
  290.             if (!visited[n])
  291.                 DFSrec(n, visited);
  292.         }
  293.     }
  294.  
  295.     void DFS(int s) {
  296.         boolean visited[] = new boolean[V];
  297.         DFSrec(s, visited);
  298.     }
  299.  
  300.     public static void main(String[] args) {
  301.         Scanner s = new Scanner(System.in);
  302.         int n = s.nextInt();
  303.         Main graph = new Main(n);
  304.         int v, e;
  305.         while (true) {
  306.             v = s.nextInt();
  307.             e = s.nextInt();
  308.             if (v == -1 && e == -1)
  309.                 break;
  310.             graph.addEdges(v, e);
  311.         }
  312.         int S = s.nextInt();
  313.         System.out.print("DFS: ");
  314.         graph.DFS(S);
  315.     }
  316. }
  317.  
  318.  
  319. OUTPUT
  320. 5
  321. 0 1
  322. 0 2
  323. 1 3
  324. 2 4
  325. 3 4
  326. -1 -1
  327. 0
  328. DFS: 0 1 3 4 2
  329.  
  330.  
  331. DIAL ALGO
  332.  
  333. import java.util.*;
  334.  
  335. public class Main {
  336.     static final int INF = Integer.MAX_VALUE;
  337.     private int V; // No. of vertices
  338.     // In a weighted graph, we need to store vertex
  339.     // and weight pair for every edge
  340.     private ArrayList<ArrayList<Tuple>> adj;
  341.  
  342.     public Main(int v) // Constructor
  343.     {
  344.         this.V = v;
  345.         this.adj = new ArrayList<ArrayList<Tuple>>();
  346.         for (int i = 0; i < v; i++)
  347.             this.adj.add(new ArrayList<Tuple>());
  348.     }
  349.  
  350.     // function to Add an edge to graph
  351.     // Adds edge between u and v of weight w
  352.     public void AddEdge(int u, int v, int w)
  353.     {
  354.         adj.get(u).add(new Tuple(v, w));
  355.         adj.get(v).add(new Tuple(u, w));
  356.     }
  357.  
  358.     // Prints shortest paths from src to all other vertices.
  359.     // W is the maximum weight of an edge
  360.     public void shortestPath(int src, int W)
  361.     {
  362.        
  363.         int[] dist = new int[V];
  364.         Arrays.fill(dist, INF);
  365.         ArrayList<Integer>[] B = new ArrayList[W * V + 1];
  366.         for (int i = 0; i < W * V + 1; i++)
  367.             B[i] = new ArrayList<Integer>();
  368.  
  369.         B[0].add(src);
  370.         dist[src] = 0;
  371.  
  372.         int idx = 0;
  373.         while (true) {
  374.             // Go sequentially through buckets till one
  375.             // non-empty bucket is found
  376.             while (B[idx].size() == 0 && idx < W * V)
  377.                 idx++;
  378.  
  379.             // If all buckets are empty, we are done.
  380.             if (idx == W * V)
  381.                 break;
  382.  
  383.             // Take top vertex from bucket and pop it
  384.             int u = B[idx].get(0);
  385.             B[idx].remove(0);
  386.  
  387.             // Process all adjacents of extracted vertex 'u'
  388.             // and update their distances if required.
  389.             for (Tuple i : adj.get(u)) {
  390.                 int v = i.v;
  391.                 int weight = i.w;
  392.  
  393.                 int du = dist[u];
  394.                 int dv = dist[v];
  395.  
  396.                 // If there is shorted path to v through u.
  397.                 if (dv > du + weight) {
  398.                     // updating the distance
  399.                     dist[v] = du + weight;
  400.                     dv = dist[v];
  401.  
  402.                     // pushing vertex v into updated
  403.                     // distance's bucket
  404.                     B[dv].add(0, v);
  405.                 }
  406.             }
  407.         }
  408.  
  409.         // Print shortest distances stored in dist[]
  410.         System.out.println("Vertex Distance from Source");
  411.         for (int i = 0; i < V; ++i)
  412.             System.out.println(i + "tt" + dist[i]);
  413.     }
  414.  
  415.     static class Tuple {
  416.         int v, w;
  417.         Tuple(int v, int w)
  418.         {
  419.             this.v = v;
  420.             this.w = w;
  421.         }
  422.     }
  423.  
  424.     public static void main(String[] args)
  425.     {
  426.         Scanner scanner = new Scanner(System.in);
  427.  
  428.         System.out.print("Enter the number of vertices: ");
  429.         int V = scanner.nextInt();
  430.         Main g = new Main(V);
  431.  
  432.         System.out.print("Enter the number of edges: ");
  433.         int E = scanner.nextInt();
  434.  
  435.         System.out.println("Enter the edges in format (u v w):");
  436.         for (int i = 0; i < E; i++) {
  437.             int u = scanner.nextInt();
  438.             int v = scanner.nextInt();
  439.             int w = scanner.nextInt();
  440.             g.AddEdge(u, v, w);
  441.         }
  442.  
  443.         System.out.print("Enter the maximum weight of an edge: ");
  444.         int maxWeight = scanner.nextInt();
  445.  
  446.         System.out.print("Enter the source vertex: ");
  447.         int src = scanner.nextInt();
  448.  
  449.         // maximum weighted edge - 4
  450.         g.shortestPath(src, maxWeight);
  451.  
  452.         scanner.close();
  453.     }
  454. }
  455.  
  456. OUTPUT
  457. Enter the number of vertices: 9
  458. Enter the number of edges: 14
  459. Enter the edges in format (u v w):
  460. 0 1 4
  461. 0 7 8
  462. 1 2 8
  463. 1 7 11
  464. 2 3 7
  465. 2 8 2
  466. 2 5 4
  467. 3 4 9
  468. 3 5 14
  469. 4 5 10
  470. 5 6 2
  471. 6 7 1
  472. 6 8 6
  473. 7 8 7
  474. Enter the maximum weight of an edge: 14
  475. Enter the source vertex: 0
  476. Vertex Distance from Source
  477. 0  0
  478. 1  4
  479. 2  12
  480. 3  19
  481. 4  21
  482. 5  11
  483. 6  9
  484. 7  8
  485. 8  14
  486.  
  487.  
  488.  
  489. Heap sort
  490.  
  491. import java.util.Scanner;
  492.  
  493. class HeapSort {
  494.     public void sort(int arr[]) {
  495.         int n = arr.length;
  496.         // Build heap (rearrange array)
  497.         for (int i = n / 2 - 1; i >= 0; i--) {
  498.             heapify(arr, n, i);
  499.         }
  500.         // One by one extract an element from heap
  501.         for (int i = n - 1; i > 0; i--) {
  502.             // Move current root to end
  503.             int temp = arr[0];
  504.             arr[0] = arr[i];
  505.             arr[i] = temp;
  506.             // call max heapify on the reduced heap
  507.             heapify(arr, i, 0);
  508.         }
  509.     }
  510.  
  511.     void heapify(int arr[], int n, int i) {
  512.         int largest = i; // Initialize largest as root
  513.         int l = 2 * i + 1; // left = 2*i + 1
  514.         int r = 2 * i + 2; // right = 2*i + 2
  515.         // If left child is larger than root
  516.         if (l < n && arr[l] > arr[largest]) {
  517.             largest = l;
  518.         }
  519.         // If right child is larger than largest so far
  520.         if (r < n && arr[r] > arr[largest]) {
  521.             largest = r;
  522.         }
  523.         // If largest is not root
  524.         if (largest != i) {
  525.             int swap = arr[i];
  526.             arr[i] = arr[largest];
  527.             arr[largest] = swap;
  528.             // Recursively heapify the affected sub-tree
  529.             heapify(arr, n, largest);
  530.         }
  531.     }
  532.  
  533.     void printArray(int arr[]) {
  534.         int n = arr.length;
  535.         for (int i = 0; i < n; ++i) {
  536.             System.out.print(arr[i] + " ");
  537.         }
  538.         System.out.println();
  539.     }
  540. }
  541.  
  542. public class Main {
  543.     public static void main(String args[]) {
  544.         Scanner sc = new Scanner(System.in);
  545.         int n = sc.nextInt();
  546.         int arr[] = new int[n];
  547.         for (int i = 0; i < n; i++) {
  548.             arr[i] = sc.nextInt();
  549.         }
  550.         HeapSort ob = new HeapSort();
  551.         ob.sort(arr);
  552.         ob.printArray(arr);
  553.     }
  554. }
  555.  
  556.  
  557. OUTPUT
  558. 4
  559. 5 2 3 1
  560. 1 2 3 5
  561.  
  562. Heap sort  SHORTEST
  563. import java.util.*;
  564. public class Main
  565. {
  566.  public static void main(String[] args) {
  567.      
  568.      Scanner sc=new Scanner(System.in);
  569.      int n=sc.nextInt();
  570.      int[] arr=new int[n];
  571.      for(int i=0; i<n; i++){
  572.          arr[i]=sc.nextInt();
  573.      }
  574.      
  575.      Arrays.sort(arr);
  576.      
  577.      for(int i=0; i<n; i++){
  578.          System.out.print(arr[i]+" ");
  579.      }
  580.  
  581.  }
  582. }
  583.  
  584. Output:  
  585. 5
  586. 2 4 5 4 2
  587. 2 2 4 4 5
  588.  
  589.  
  590.  
  591. TOPOLOGICAL SORT
  592.  
  593. import java.util.*;
  594.  
  595. public class Main {
  596.  
  597.     // Function to perform DFS and topological sorting
  598.     static void topologicalSortUtil(int v, List<List<Integer>> adj,
  599.                                     boolean[] visited,
  600.                                     Stack<Integer> stack) {
  601.         // Mark the current node as visited
  602.         visited[v] = true;
  603.         // Recur for all adjacent vertices
  604.         for (int i : adj.get(v)) {
  605.             if (!visited[i])
  606.                 topologicalSortUtil(i, adj, visited, stack);
  607.         }
  608.  
  609.         // Push current vertex to stack which stores the
  610.         // result
  611.         stack.push(v);
  612.     }
  613.  
  614.     // Function to perform Topological Sort
  615.     static void topologicalSort(List<List<Integer>> adj) {
  616.         // Stack to store the result
  617.         Stack<Integer> stack = new Stack<>();
  618.         int V = adj.size();
  619.         boolean[] visited = new boolean[V];
  620.  
  621.         // Call the recursive helper function to store
  622.         // Topological Sort starting from all vertices one
  623.         // by one
  624.         for (int i = 0; i < V; i++) {
  625.             if (!visited[i])
  626.                 topologicalSortUtil(i, adj, visited, stack);
  627.         }
  628.  
  629.         // Print contents of stack
  630.         System.out.print(
  631.                 "Topological sorting of the graph: ");
  632.         while (!stack.isEmpty()) {
  633.             System.out.print(stack.pop() + " ");
  634.         }
  635.     }
  636.  
  637.     // Driver code
  638.     public static void main(String[] args) {
  639.         Scanner scanner = new Scanner(System.in);
  640.  
  641.         // Number of nodes
  642.         System.out.print("Enter the number of nodes: ");
  643.         int V = scanner.nextInt();
  644.  
  645.         // Edges
  646.         List<List<Integer>> edges = new ArrayList<>();
  647.         System.out.println("Enter edges in format (source destination), type -1 to stop:");
  648.         while (true) {
  649.             int source = scanner.nextInt();
  650.             if (source == -1) break;
  651.             int destination = scanner.nextInt();
  652.             edges.add(Arrays.asList(source, destination));
  653.         }
  654.  
  655.         // Graph represented as an adjacency list
  656.         List<List<Integer>> adj = new ArrayList<>(V);
  657.         for (int i = 0; i < V; i++) {
  658.             adj.add(new ArrayList<>());
  659.         }
  660.  
  661.         for (List<Integer> i : edges) {
  662.             adj.get(i.get(0)).add(i.get(1));
  663.         }
  664.         topologicalSort(adj);
  665.         scanner.close();
  666.     }
  667. }
  668.  
  669.  
  670. OUTPUT
  671. Enter the number of nodes: 6
  672. Enter edges in format (source destination), type -1 to stop:
  673. 5 2
  674. 5 0
  675. 4 0
  676. 4 1
  677. 2 3
  678. 3 1
  679. -1
  680. Topological sorting of the graph: 5 4 2 3 1 0
  681.  
  682.  
  683.  
  684.                            WITHOUT -1 TERMINATE
  685. import java.util.*;
  686.  
  687. public class Main {
  688.  
  689.     // Function to perform DFS and topological sorting
  690.     static void topologicalSortUtil(int v, List<List<Integer>> adj,
  691.                                     boolean[] visited,
  692.                                     Stack<Integer> stack) {
  693.         // Mark the current node as visited
  694.         visited[v] = true;
  695.         // Recur for all adjacent vertices
  696.         for (int i : adj.get(v)) {
  697.             if (!visited[i])
  698.                 topologicalSortUtil(i, adj, visited, stack);
  699.         }
  700.  
  701.         // Push current vertex to stack which stores the
  702.         // result
  703.         stack.push(v);
  704.     }
  705.  
  706.     // Function to perform Topological Sort
  707.     static void topologicalSort(List<List<Integer>> adj) {
  708.         // Stack to store the result
  709.         Stack<Integer> stack = new Stack<>();
  710.         int V = adj.size();
  711.         boolean[] visited = new boolean[V];
  712.  
  713.         // Call the recursive helper function to store
  714.         // Topological Sort starting from all vertices one
  715.         // by one
  716.         for (int i = 0; i < V; i++) {
  717.             if (!visited[i])
  718.                 topologicalSortUtil(i, adj, visited, stack);
  719.         }
  720.  
  721.         // Print contents of stack
  722.         System.out.print(
  723.                 "Topological sorting of the graph: ");
  724.         while (!stack.isEmpty()) {
  725.             System.out.print(stack.pop() + " ");
  726.         }
  727.     }
  728.  
  729.     // Driver code
  730.     public static void main(String[] args) {
  731.         Scanner scanner = new Scanner(System.in);
  732.  
  733.         // Number of nodes
  734.         System.out.print("Enter the number of nodes: ");
  735.         int V = scanner.nextInt();
  736.  
  737.         // Edges
  738.         List<List<Integer>> edges = new ArrayList<>();
  739.         System.out.println("Enter edges in format (source destination):");
  740.         for (int i = 0; i < V; i++) {
  741.             int source = scanner.nextInt();
  742.              int destinati
  743.             edges.add(Arrays.asList(source, destination));
  744.         }
  745.  
  746.         // Graph represented as an adjacency list
  747.         List<List<Integer>> adj = new ArrayList<>(V);
  748.         for (int i = 0; i < V; i++) {
  749.             adj.add(new ArrayList<>());
  750.         }
  751.  
  752.         for (List<Integer> i : edges) {
  753.             adj.get(i.get(0)).add(i.get(1));
  754.         }
  755.         topologicalSort(adj);
  756.         scanner.close();
  757.     }
  758. }
  759.  
  760. OUTPUT
  761. Enter the number of nodes: 6
  762. Enter edges in format (source destination):
  763. 5 2
  764. 5 0
  765. 4 0
  766. 4 1
  767. 2 3
  768. 3 1
  769. Topological sorting of the graph: 5 4 2 3 1 0
  770.  
  771. BINOMIAL HEAP
  772. import java.util.Scanner;
  773.  
  774. class BinomialHeapNode {
  775.     int key, degree;
  776.     BinomialHeapNode parent;
  777.     BinomialHeapNode sibling;
  778.     BinomialHeapNode child;
  779.  
  780.     public BinomialHeapNode(int k) {
  781.         key = k;
  782.         degree = 0;
  783.         parent = null;
  784.         sibling = null;
  785.         child = null;
  786.     }
  787.  
  788.     public BinomialHeapNode reverse(BinomialHeapNode sibl) {
  789.         BinomialHeapNode ret;
  790.         if (sibling != null)
  791.             ret = sibling.reverse(this);
  792.         else
  793.             ret = this;
  794.         sibling = sibl;
  795.         return ret;
  796.     }
  797.  
  798.     public BinomialHeapNode findMinNode() {
  799.         BinomialHeapNode x = this, y = this;
  800.         int min = x.key;
  801.  
  802.         while (x != null) {
  803.             if (x.key < min) {
  804.                 y = x;
  805.                 min = x.key;
  806.             }
  807.             x = x.sibling;
  808.         }
  809.  
  810.         return y;
  811.     }
  812.  
  813.     public BinomialHeapNode findANodeWithKey(int value) {
  814.         BinomialHeapNode temp = this, node = null;
  815.  
  816.         while (temp != null) {
  817.             if (temp.key == value) {
  818.                 node = temp;
  819.                 break;
  820.             }
  821.  
  822.             if (temp.child == null)
  823.                 temp = temp.sibling;
  824.             else {
  825.                 node = temp.child.findANodeWithKey(value);
  826.                 if (node == null)
  827.                     temp = temp.sibling;
  828.                 else
  829.                     break;
  830.             }
  831.         }
  832.         return node;
  833.     }
  834.  
  835.     public int getSize() {
  836.         return (1 + ((child == null) ? 0 : child.getSize())
  837.                 + ((sibling == null) ? 0 : sibling.getSize()));
  838.     }
  839. }
  840.  
  841. class BinomialHeap {
  842.     private BinomialHeapNode Nodes;
  843.     private int size;
  844.  
  845.     public BinomialHeap() {
  846.         Nodes = null;
  847.         size = 0;
  848.     }
  849.  
  850.     public boolean isEmpty() {
  851.         return Nodes == null;
  852.     }
  853.  
  854.     public int getSize() {
  855.         return size;
  856.     }
  857.  
  858.     public void makeEmpty() {
  859.         Nodes = null;
  860.         size = 0;
  861.     }
  862.  
  863.     public void insert(int value) {
  864.         if (value > 0) {
  865.             BinomialHeapNode temp = new BinomialHeapNode(value);
  866.             if (Nodes == null) {
  867.                 Nodes = temp;
  868.                 size = 1;
  869.             } else {
  870.                 unionNodes(temp);
  871.                 size++;
  872.             }
  873.         }
  874.     }
  875.  
  876.     private void merge(BinomialHeapNode binHeap) {
  877.         BinomialHeapNode temp1 = Nodes, temp2 = binHeap;
  878.  
  879.         while ((temp1 != null) && (temp2 != null)) {
  880.  
  881.             if (temp1.degree == temp2.degree) {
  882.                 BinomialHeapNode tmp = temp2;
  883.                 temp2 = temp2.sibling;
  884.                 tmp.sibling = temp1.sibling;
  885.                 temp1.sibling = tmp;
  886.                 temp1 = tmp.sibling;
  887.             } else {
  888.  
  889.                 if (temp1.degree < temp2.degree) {
  890.                     if ((temp1.sibling == null) || (temp1.sibling.degree > temp2.degree)) {
  891.                         BinomialHeapNode tmp = temp2;
  892.                         temp2 = temp2.sibling;
  893.                         tmp.sibling = temp1.sibling;
  894.                         temp1.sibling = tmp;
  895.                         temp1 = tmp.sibling;
  896.                     } else {
  897.                         temp1 = temp1.sibling;
  898.                     }
  899.                 } else {
  900.                     BinomialHeapNode tmp = temp1;
  901.                     temp1 = temp2;
  902.                     temp2 = temp2.sibling;
  903.                     temp1.sibling = tmp;
  904.  
  905.                     if (tmp == Nodes) {
  906.                         Nodes = temp1;
  907.                     }
  908.                 }
  909.             }
  910.         }
  911.  
  912.         if (temp1 == null) {
  913.             temp1 = Nodes;
  914.  
  915.             while (temp1.sibling != null) {
  916.                 temp1 = temp1.sibling;
  917.             }
  918.             temp1.sibling = temp2;
  919.         }
  920.     }
  921.  
  922.     private void unionNodes(BinomialHeapNode binHeap) {
  923.         merge(binHeap);
  924.  
  925.         BinomialHeapNode prevTemp = null, temp = Nodes, nextTemp = Nodes.sibling;
  926.  
  927.         while (nextTemp != null) {
  928.  
  929.             if ((temp.degree != nextTemp.degree)
  930.                     || ((nextTemp.sibling != null) && (nextTemp.sibling.degree == temp.degree))) {
  931.                 prevTemp = temp;
  932.                 temp = nextTemp;
  933.             } else {
  934.  
  935.                 if (temp.key <= nextTemp.key) {
  936.                     temp.sibling = nextTemp.sibling;
  937.                     nextTemp.parent = temp;
  938.                     nextTemp.sibling = temp.child;
  939.                     temp.child = nextTemp;
  940.                     temp.degree++;
  941.                 } else {
  942.  
  943.                     if (prevTemp == null) {
  944.                         Nodes = nextTemp;
  945.                     } else {
  946.                         prevTemp.sibling = nextTemp;
  947.                     }
  948.  
  949.                     temp.parent = nextTemp;
  950.                     temp.sibling = nextTemp.child;
  951.                     nextTemp.child = temp;
  952.                     nextTemp.degree++;
  953.                     temp = nextTemp;
  954.                 }
  955.             }
  956.             nextTemp = temp.sibling;
  957.         }
  958.     }
  959.  
  960.     public int findMinimum() {
  961.         return Nodes.findMinNode().key;
  962.     }
  963.  
  964.     public void delete(int value) {
  965.         if ((Nodes != null) && (Nodes.findANodeWithKey(value) != null)) {
  966.             decreaseKeyValue(value, Integer.MIN_VALUE);
  967.             extractMin();
  968.         }
  969.     }
  970.  
  971.     public void decreaseKeyValue(int old_value, int new_value) {
  972.         BinomialHeapNode temp = Nodes.findANodeWithKey(old_value);
  973.         if (temp == null)
  974.             return;
  975.         temp.key = new_value;
  976.         BinomialHeapNode tempParent = temp.parent;
  977.  
  978.         while ((tempParent != null) && (temp.key < tempParent.key)) {
  979.             int z = temp.key;
  980.             temp.key = tempParent.key;
  981.             tempParent.key = z;
  982.  
  983.             temp = tempParent;
  984.             tempParent = tempParent.parent;
  985.         }
  986.     }
  987.  
  988.     public int extractMin() {
  989.         if (Nodes == null)
  990.             return -1;
  991.  
  992.         BinomialHeapNode temp = Nodes, prevTemp = null;
  993.         BinomialHeapNode minNode = Nodes.findMinNode();
  994.  
  995.         while (temp.key != minNode.key) {
  996.             prevTemp = temp;
  997.             temp = temp.sibling;
  998.         }
  999.  
  1000.         if (prevTemp == null) {
  1001.             Nodes = temp.sibling;
  1002.         } else {
  1003.             prevTemp.sibling = temp.sibling;
  1004.         }
  1005.  
  1006.         temp = temp.child;
  1007.         BinomialHeapNode fakeNode = temp;
  1008.  
  1009.         while (temp != null) {
  1010.             temp.parent = null;
  1011.             temp = temp.sibling;
  1012.         }
  1013.  
  1014.         if ((Nodes == null) && (fakeNode == null)) {
  1015.             size = 0;
  1016.         } else {
  1017.             if ((Nodes == null) && (fakeNode != null)) {
  1018.                 Nodes = fakeNode.reverse(null);
  1019.                 size = Nodes.getSize();
  1020.             } else {
  1021.                 if ((Nodes != null) && (fakeNode == null)) {
  1022.                     size = Nodes.getSize();
  1023.                 } else {
  1024.                     unionNodes(fakeNode.reverse(null));
  1025.                     size = Nodes.getSize();
  1026.                 }
  1027.             }
  1028.         }
  1029.         return minNode.key;
  1030.     }
  1031.  
  1032.     public void displayHeap() {
  1033.         System.out.print("nHeap : ");
  1034.         displayHeapHelper(Nodes);
  1035.         System.out.println("n");
  1036.     }
  1037.  
  1038.     private void displayHeapHelper(BinomialHeapNode r) {
  1039.         if (r != null) {
  1040.             displayHeapHelper(r.child);
  1041.             System.out.print(r.key + " ");
  1042.             displayHeapHelper(r.sibling);
  1043.         }
  1044.     }
  1045. }
  1046.  
  1047. public class Main {
  1048.     public static void main(String[] args) {
  1049.         BinomialHeap binHeap = new BinomialHeap();
  1050.         Scanner scanner = new Scanner(System.in);
  1051.  
  1052.         while (true) {
  1053.             System.out.print("Enter your choice: ");
  1054.             int choice = scanner.nextInt();
  1055.  
  1056.             switch (choice) {
  1057.                 case 1:
  1058.                     System.out.print("Enter value to insert: ");
  1059.                     int valueToInsert = scanner.nextInt();
  1060.                     binHeap.insert(valueToInsert);
  1061.                     break;
  1062.                 case 2:
  1063.                     System.out.print("Enter value to delete: ");
  1064.                     int valueToDelete = scanner.nextInt();
  1065.                     binHeap.delete(valueToDelete);
  1066.                     break;
  1067.                 case 3:
  1068.                     System.out.println("Size of the binomial heap is " + binHeap.getSize());
  1069.                     binHeap.displayHeap();
  1070.                     break;
  1071.                 case 4:
  1072.                     System.out.println("Exiting...");
  1073.                     scanner.close();
  1074.                     System.exit(0);
  1075.                 default:
  1076.                     System.out.println("Invalid choice! Please enter a valid option.");
  1077.             }
  1078.         }
  1079.     }
  1080. }
  1081.  
  1082.  
  1083. OUTPUT
  1084. Enter your choice: 1
  1085. Enter value to insert: 45
  1086. Enter your choice: 3
  1087. Size of the binomial heap is 1
  1088.  
  1089. Heap : 45
  1090.  
  1091. Enter your choice: 4
  1092. Exiting...
  1093.  
  1094.  
  1095.  
  1096. K-ARRY
  1097.  
  1098. import java.util.ArrayList;
  1099. import java.util.Scanner;
  1100.  
  1101. public class Main {
  1102.     private static int n;
  1103.     private static ArrayList<Integer> arl = new ArrayList<Integer>();
  1104.    
  1105.     public static int getMax() {
  1106.         if (isEmpty()) {
  1107.             return Integer.MIN_VALUE;
  1108.         }
  1109.         return arl.get(0);
  1110.     }
  1111.  
  1112.     public static boolean isEmpty() {
  1113.         return (arl.size() == 0);
  1114.     }
  1115.  
  1116.     public static void insert(int value) {
  1117.        
  1118.         arl.add(value);
  1119.         int childrenIndex = arl.size() - 1;
  1120.         int parentIndex = (childrenIndex - 1) / n;
  1121.         while (parentIndex >= 0 && arl.get(childrenIndex) > arl.get(parentIndex)) {
  1122.             int temp = arl.get(childrenIndex);
  1123.             arl.set(childrenIndex, arl.get(parentIndex));
  1124.             arl.set(parentIndex, temp);
  1125.  
  1126.             childrenIndex = parentIndex;
  1127.             parentIndex = (childrenIndex - 1) / n;
  1128.         }  
  1129.     }
  1130.  
  1131.     public static void removeMax() {
  1132.         arl.set(0, arl.get(arl.size() - 1));
  1133.         arl.remove(arl.size() - 1);
  1134.         int parentIndex = 0;
  1135.         while (true) {
  1136.             int largestValueIndex = parentIndex;
  1137.             for (int i = n * parentIndex + 1; i <= (n * parentIndex + n) && i < arl.size(); i++) {
  1138.                 if (arl.get(largestValueIndex) < arl.get(i)) {
  1139.                     largestValueIndex = i;
  1140.                 }
  1141.             }
  1142.  
  1143.             if (largestValueIndex == parentIndex) {
  1144.                 break;
  1145.             }
  1146.             else {
  1147.                 int temp = arl.get(parentIndex);
  1148.                 arl.set(parentIndex, arl.get(largestValueIndex));
  1149.                 arl.set(largestValueIndex, temp);
  1150.  
  1151.                 parentIndex = largestValueIndex;
  1152.             }
  1153.         }
  1154.     }
  1155.     public static void main(String[] args) {
  1156.         Scanner scanner = new Scanner(System.in);
  1157.         System.out.print("Enter the value of n: ");
  1158.         n = scanner.nextInt();
  1159.         System.out.print("Enter the number of elements to insert: ");
  1160.         int numElements = scanner.nextInt();
  1161.        
  1162.         System.out.println("Enter " + numElements + " elements:");
  1163.  
  1164.         for (int i = 0; i < numElements; i++) {
  1165.             int value = scanner.nextInt();
  1166.             insert(value);
  1167.         }
  1168.         System.out.println("Get Top element: " + getMax());
  1169.         System.out.println("Heap elements: " + arl);
  1170.         removeMax();
  1171.         System.out.println("The maximum Element in the heap after removal: " + getMax());
  1172.         scanner.close();
  1173.     }
  1174. }
  1175.  
  1176. OUTPUT
  1177. Enter the value of n: 3
  1178. Enter the number of elements to insert: 5
  1179. Enter 5 elements:
  1180. 10 20 5 15 30
  1181.  
  1182.  
  1183. Get Top element: 30
  1184. Heap elements: [30, 20, 5, 15, 10]
  1185. The maximum Element in the heap after removal: 20
  1186.  
  1187.  
  1188. WINNER TREE
  1189.  
  1190. import java.util.Scanner;
  1191.  
  1192. public class Main {
  1193.     private int[] tree;
  1194.     private int[] players;
  1195.     private int numPlayers;
  1196.  
  1197.     public Main(int[] players) {
  1198.         this.players = players;
  1199.         this.numPlayers = players.length;
  1200.         int treeSize = 2 * numPlayers - 1;
  1201.         this.tree = new int[treeSize];
  1202.         buildWinnerTree();
  1203.     }
  1204.  
  1205.     private void buildWinnerTree() {
  1206.         for (int i = numPlayers - 1; i < tree.length; i++) {
  1207.             tree[i] = i - (numPlayers - 1);
  1208.         }
  1209.         for (int i = numPlayers - 2; i >= 0; i--) {
  1210.             tree[i] = players[tree[2 * i + 1]] < players[tree[2 * i + 2]] ? tree[2 * i + 2] : tree[2 * i + 1];
  1211.         }
  1212.     }
  1213.  
  1214.     public int getWinner() {
  1215.         return players[tree[0]];
  1216.     }
  1217.  
  1218.     public static void main(String[] args) {
  1219.         Scanner scanner = new Scanner(System.in);
  1220.  
  1221.         System.out.print("Enter the number of players: ");
  1222.         int numPlayers = scanner.nextInt();
  1223.         int[] players = new int[numPlayers];
  1224.  
  1225.         System.out.println("Enter the scores of each player:");
  1226.         for (int i = 0; i < numPlayers; i++) {
  1227.             players[i] = scanner.nextInt();
  1228.         }
  1229.  
  1230.         Main winnerTree = new Main(players);
  1231.         System.out.println("The winner is: " + winnerTree.getWinner());
  1232.  
  1233.         scanner.close();
  1234.     }
  1235. }
  1236.  
  1237.  
  1238. Enter the number of players: 8
  1239. Enter the scores of each player:
  1240. 3 7 1 9 4 2 8 5
  1241. The winner is: 9
  1242.  
  1243.  
  1244. WINNER TREE WITH INDEX
  1245. import java.util.Arrays;
  1246. import java.util.Scanner;
  1247.  
  1248. public class Main {
  1249.     public static class WinnerTree {
  1250.         private int[] tree;
  1251.         private int[] players;
  1252.  
  1253.         public WinnerTree(int[] players) {
  1254.             this.players = players;
  1255.             int n = players.length;
  1256.             int treeSize = 1;
  1257.             while (treeSize < n) {
  1258.                 treeSize *= 2;
  1259.             }
  1260.             tree = new int[2 * treeSize - 1];
  1261.             Arrays.fill(tree, -1);
  1262.             for (int i = 0; i < n; i++) {
  1263.                 tree[treeSize - 1 + i] = i;
  1264.             }
  1265.             build(0, 0, treeSize - 1);
  1266.         }
  1267.  
  1268.         private void build(int node, int left, int right) {
  1269.             if (left == right) {
  1270.                 if (left < players.length) {
  1271.                     tree[node] = left;
  1272.                 }
  1273.                 return;
  1274.             }
  1275.             int mid = (left + right) / 2;
  1276.             build(2 * node + 1, left, mid);
  1277.             build(2 * node + 2, mid + 1, right);
  1278.             if (tree[2 * node + 1] != -1 && (tree[2 * node + 2] == -1 || players[tree[2 * node + 1]] >= players[tree[2 * node + 2]])) {
  1279.                 tree[node] = tree[2 * node + 1];
  1280.             } else {
  1281.                 tree[node] = tree[2 * node + 2];
  1282.             }
  1283.         }
  1284.  
  1285.         public int getWinner() {
  1286.             return tree[0];
  1287.         }
  1288.     }
  1289.  
  1290.     public static void main(String[] args) {
  1291.         Scanner scanner = new Scanner(System.in);
  1292.         System.out.print("Enter the number of players: ");
  1293.         int numPlayers = scanner.nextInt();
  1294.         int[] players = new int[numPlayers];
  1295.         System.out.println("Enter the scores of each player:");
  1296.         for (int i = 0; i < numPlayers; i++) {
  1297.             System.out.print("Player " + (i + 1) + ": ");
  1298.             players[i] = scanner.nextInt();
  1299.         }
  1300.         scanner.close();
  1301.  
  1302.         WinnerTree winnerTree = new WinnerTree(players);
  1303.         int winnerIndex = winnerTree.getWinner();
  1304.         System.out.println("The player with the highest score is at index: " + winnerIndex);
  1305.         System.out.println("The score of the winning player is: " + players[winnerIndex]);
  1306.     }
  1307. }
  1308.  
  1309. OUTPUT
  1310. Enter the number of players: 5
  1311. Enter the scores of each player:
  1312. Player 1: 10
  1313. Player 2: 5
  1314. Player 3: 8
  1315. Player 4: 12
  1316. Player 5: 6
  1317. The player with the highest score is at index: 3
  1318. The score of the winning player is: 12
  1319.  
  1320.  
  1321.  
  1322.  
  1323.  
  1324.  
  1325.  
  1326. WINNING TREE WITH LOWEST SCORE
  1327.  
  1328. import java.util.Arrays;
  1329. import java.util.*;
  1330. public class Main {
  1331. private int[] tree;
  1332. private int[] players;
  1333. public Main(int[] players) {
  1334. this.players = players;
  1335. int n = players.length;
  1336. int treeSize = calculateTreeSize(n);
  1337. tree = new int[2 * treeSize - 1];
  1338. Arrays.fill(tree, -1);
  1339. for (int i = 0; i < n; i++) {
  1340. tree[treeSize - 1 + i] = i;
  1341. }
  1342. buildWinnerTree(0, 0, treeSize - 1);
  1343. }
  1344. private int calculateTreeSize(int n) {
  1345. int treeSize = 1;
  1346. while (treeSize < n) {
  1347. treeSize *= 2;
  1348. }
  1349. return treeSize;
  1350. }
  1351. private void buildWinnerTree(int node, int left, int right) {
  1352. if (left == right) return;
  1353. int mid = (left + right) / 2;
  1354. buildWinnerTree(2 * node + 1, left, mid);
  1355. buildWinnerTree(2 * node + 2, mid + 1, right);
  1356. tree[node] = players[tree[2 * node + 1]] <=players[tree[2 * node + 2]] ? tree[2 * node + 1] : tree[2 *node + 2];
  1357. }
  1358. public int getWinnerIndex() {
  1359. return tree[0];
  1360. }
  1361. public static void main(String[] args) {
  1362. Scanner sc=new Scanner(System.in);
  1363. int n=sc.nextInt();
  1364. int[] players = new int[n];
  1365.     for(int i=0; i<n; i++){
  1366.          players[i]=sc.nextInt();
  1367.      }
  1368.  
  1369. Main winnerTree = new Main(players);
  1370. int winnerIndex = winnerTree.getWinnerIndex();
  1371. int winningScore = players[winnerIndex];
  1372. System.out.println("The player with the lowest score is at index: " + winnerIndex);
  1373. System.out.println("The score of the winning player is: " + winningScore);
  1374. }
  1375. }
  1376.  
  1377.  
  1378. Output
  1379. 4
  1380. 1 7 0 5
  1381. The player with the lowest score is at index: 2
  1382. The score of the winning player is: 0
  1383.  
  1384. Right view
  1385. import java.util.Scanner;
  1386.  
  1387. class Node {
  1388.     Node left;
  1389.     Node right;
  1390.     int data;
  1391. }
  1392.  
  1393. class BinaryTree {
  1394.     int maxLevel;
  1395.  
  1396.     public void rightViewOfTree(Node node, int level) {
  1397.         if (node == null) {
  1398.             return;
  1399.         }
  1400.  
  1401.         if (level >= maxLevel) {
  1402.             System.out.print(node.data + " ");
  1403.             maxLevel++;
  1404.         }
  1405.  
  1406.         rightViewOfTree(node.right, level + 1);
  1407.         rightViewOfTree(node.left, level + 1);
  1408.     }
  1409.  
  1410.     public Node createNewNode(String val) {
  1411.         if (val.equals("null")) {
  1412.             return null;
  1413.         }
  1414.         Node newNode = new Node();
  1415.         newNode.data = Integer.parseInt(val);
  1416.         newNode.left = null;
  1417.         newNode.right = null;
  1418.         return newNode;
  1419.     }
  1420. }
  1421.  
  1422. public class RightView {
  1423.  
  1424.     public static void main(String[] args) {
  1425.         Scanner scanner = new Scanner(System.in);
  1426.         BinaryTree binaryTree = new BinaryTree();
  1427.  
  1428.         System.out.print("Enter the nodes in the form of [2,3,null,5]: ");
  1429.         String input = scanner.nextLine().trim();
  1430.  
  1431.         String[] values = input.substring(1, input.length() - 1).split(",");
  1432.         Node root = binaryTree.createNewNode(values[0]);
  1433.  
  1434.         for (int i = 1; i < values.length; i++) {
  1435.             insertNode(root, values[i]);
  1436.         }
  1437.  
  1438.         System.out.println("Right view of the binary tree:");
  1439.         binaryTree.rightViewOfTree(root, 0);
  1440.  
  1441.         scanner.close();
  1442.     }
  1443.  
  1444.     public static void insertNode(Node root, String val) {
  1445.         if (val.equals("null") || root == null) {
  1446.             return;
  1447.         }
  1448.  
  1449.         if (root.left == null) {
  1450.             root.left = new Node();
  1451.             root.left.data = Integer.parseInt(val);
  1452.         } else if (root.right == null) {
  1453.             root.right = new Node();
  1454.             root.right.data = Integer.parseInt(val);
  1455.         } else {
  1456.             insertNode(root.right, val);
  1457.         }
  1458.     }
  1459. }
  1460.  
  1461. Left view
  1462. import java.util.Scanner;
  1463.  
  1464. class Node {
  1465.     Node left;
  1466.     Node right;
  1467.     int data;
  1468. }
  1469.  
  1470. class BinaryTree {
  1471.     int maxLevel;
  1472.  
  1473.     public void leftViewOfTree(Node node, int level) {
  1474.         if (node == null) {
  1475.             return;
  1476.         }
  1477.         if (level >= maxLevel) {
  1478.             System.out.print(node.data + " ");
  1479.             maxLevel++;
  1480.         }
  1481.         leftViewOfTree(node.left, level + 1);
  1482.         leftViewOfTree(node.right, level + 1);
  1483.     }
  1484.  
  1485.     public Node createNewNode(String val) {
  1486.         if (val.equals("null")) {
  1487.             return null;
  1488.         }
  1489.         Node newNode = new Node();
  1490.         newNode.data = Integer.parseInt(val);
  1491.         newNode.left = null;
  1492.         newNode.right = null;
  1493.         return newNode;
  1494.     }
  1495. }
  1496.  
  1497. public class Main {
  1498.  
  1499.     public static void main(String[] args) {
  1500.         Scanner scanner = new Scanner(System.in);
  1501.         BinaryTree binaryTree = new BinaryTree();
  1502.  
  1503.         System.out.print("Enter the nodes in the form of [2,3,4,nul,5]: ");
  1504.         String input = scanner.nextLine().trim();
  1505.  
  1506.         String[] values = input.substring(1, input.length() - 1).split(",");
  1507.         Node root = binaryTree.createNewNode(values[0]);
  1508.  
  1509.         for (int i = 1; i < values.length; i++) {
  1510.             insertNode(root, values[i]);
  1511.         }
  1512.  
  1513.         System.out.println("Left view of the binary tree:");
  1514.         binaryTree.leftViewOfTree(root, 0);
  1515.  
  1516.         scanner.close();
  1517.     }
  1518.  
  1519.     public static void insertNode(Node root, String val) {
  1520.         if (val.equals("null") || root == null) {
  1521.             return;
  1522.         }
  1523.  
  1524.         if (root.left == null) {
  1525.             root.left = new Node();
  1526.             root.left.data = Integer.parseInt(val);
  1527.         } else if (root.right == null) {
  1528.             root.right = new Node();
  1529.             root.right.data = Integer.parseInt(val);
  1530.         } else {
  1531.             insertNode(root.left, val);
  1532.         }
  1533.     }
  1534. }
  1535.  
  1536.  
  1537. Bottom view
  1538. import java.util.*;
  1539. import java.util.Map.Entry;
  1540.  
  1541. class Node {
  1542.     int data, hd;
  1543.     Node left, right;
  1544.  
  1545.     public Node(int data) {
  1546.         this.data = data;
  1547.         left = right = null;
  1548.         this.hd = Integer.MAX_VALUE;
  1549.     }
  1550. }
  1551.  
  1552. public class Main {
  1553.     static Node root;
  1554.  
  1555.     static Node build(String s) {
  1556.         s = s.substring(1, s.length() - 1); // Removing '[' and ']'
  1557.         String[] arr = s.split(",");
  1558.         if (arr[0].equals(""))
  1559.             return null;
  1560.         Node root = new Node(Integer.parseInt(arr[0]));
  1561.         Queue<Node> q = new LinkedList<Node>();
  1562.         q.add(root);
  1563.         int i = 1;
  1564.         while (!q.isEmpty() && i < arr.length) {
  1565.             Node curr = q.poll();
  1566.             if (!arr[i].equals("null") && !arr[i].equals("")) {
  1567.                 int h = Integer.parseInt(arr[i]);
  1568.                 curr.left = new Node(h);
  1569.                 q.add(curr.left);
  1570.             }
  1571.             i++;
  1572.             if (i >= arr.length)
  1573.                 break;
  1574.             if (!arr[i].equals("null") && !arr[i].equals("")) {
  1575.                 int h = Integer.parseInt(arr[i]);
  1576.                 curr.right = new Node(h);
  1577.                 q.add(curr.right);
  1578.             }
  1579.             i++;
  1580.         }
  1581.         return root;
  1582.     }
  1583.  
  1584.     static void bottomview(Node root) {
  1585.         if (root == null)
  1586.             return;
  1587.         int hd = 0;
  1588.         Map<Integer, Integer> map = new TreeMap<>();
  1589.         Queue<Node> queue = new LinkedList<Node>();
  1590.         root.hd = hd;
  1591.         queue.add(root);
  1592.         while (!queue.isEmpty()) {
  1593.             Node temp = queue.remove();
  1594.             hd = temp.hd;
  1595.             map.put(hd, temp.data);
  1596.             if (temp.left != null) {
  1597.                 temp.left.hd = hd - 1;
  1598.                 queue.add(temp.left);
  1599.             }
  1600.             if (temp.right != null) {
  1601.                 temp.right.hd = hd + 1;
  1602.                 queue.add(temp.right);
  1603.             }
  1604.         }
  1605.         for (int value : map.values()) {
  1606.             System.out.print(value + " ");
  1607.         }
  1608.     }
  1609.  
  1610.     public static void main(String[] args) {
  1611.         Scanner sc = new Scanner(System.in);
  1612.         Main ob = new Main();
  1613.         String input = sc.nextLine();
  1614.         root = build(input);
  1615.         ob.bottomview(root);
  1616.     }
  1617. }
  1618.  
  1619.  
  1620.  
  1621. Boundary traversal
  1622.  
  1623. import java.util.*;
  1624.  
  1625. public class Main {
  1626.     static class TreeNode {
  1627.         int val;
  1628.         TreeNode left, right;
  1629.  
  1630.         TreeNode(int val) {
  1631.             this.val = val;
  1632.             left = null;
  1633.             right = null;
  1634.         }
  1635.     }
  1636.  
  1637.     public static TreeNode buildTree(String input) {
  1638.         input = input.substring(1, input.length() - 1); // Remove '[' and ']'
  1639.         String[] values = input.split(",");
  1640.         if (values.length == 0 || values[0].equals("null")) {
  1641.             return null;
  1642.         }
  1643.  
  1644.         TreeNode root = new TreeNode(Integer.parseInt(values[0]));
  1645.         Queue<TreeNode> queue = new LinkedList<>();
  1646.         queue.offer(root);
  1647.  
  1648.         int index = 1;
  1649.         while (!queue.isEmpty() && index < values.length) {
  1650.             TreeNode curr = queue.poll();
  1651.  
  1652.             if (!values[index].equals("null")) {
  1653.                 curr.left = new TreeNode(Integer.parseInt(values[index]));
  1654.                 queue.offer(curr.left);
  1655.             }
  1656.             index++;
  1657.  
  1658.             if (index < values.length && !values[index].equals("null")) {
  1659.                 curr.right = new TreeNode(Integer.parseInt(values[index]));
  1660.                 queue.offer(curr.right);
  1661.             }
  1662.             index++;
  1663.         }
  1664.  
  1665.         return root;
  1666.     }
  1667.  
  1668.     public static List<List<Integer>> findVertical(TreeNode root) {
  1669.         TreeMap<Integer, List<Integer>> map = new TreeMap<>();
  1670.         Queue<Tuple> q = new LinkedList<>();
  1671.         q.offer(new Tuple(root, 0));
  1672.         while (!q.isEmpty()) {
  1673.             Tuple tuple = q.poll();
  1674.             TreeNode node = tuple.node;
  1675.             int col = tuple.col;
  1676.  
  1677.             if (!map.containsKey(col)) {
  1678.                 map.put(col, new ArrayList<>());
  1679.             }
  1680.             map.get(col).add(node.val);
  1681.  
  1682.             if (node.left != null) {
  1683.                 q.offer(new Tuple(node.left, col - 1));
  1684.             }
  1685.             if (node.right != null) {
  1686.                 q.offer(new Tuple(node.right, col + 1));
  1687.             }
  1688.         }
  1689.         return new ArrayList<>(map.values());
  1690.     }
  1691.  
  1692.     static class Tuple {
  1693.         TreeNode node;
  1694.         int col;
  1695.  
  1696.         public Tuple(TreeNode _node, int _col) {
  1697.             node = _node;
  1698.             col = _col;
  1699.         }
  1700.     }
  1701.  
  1702.     public static void main(String[] args) {
  1703.         Scanner scanner = new Scanner(System.in);
  1704.         System.out.print("Enter the tree values in the format [val1,val2,...]: ");
  1705.         String input = scanner.nextLine().trim();
  1706.         TreeNode root = buildTree(input);
  1707.         List<List<Integer>> result = findVertical(root);
  1708.  
  1709.         System.out.println("Output: " + result);
  1710.  
  1711.         scanner.close();
  1712.     }
  1713. }
  1714. Enter the tree values in the format [val1,val2,...]: [1,3,null,null,2]
  1715. Output: [[3], [1, 2]]
  1716.  
  1717.  
  1718.  
  1719.  
  1720. Vertical order
  1721.  
  1722. //VERTICAL ORDER
  1723. import java.util.*;
  1724.  
  1725. public class Main{
  1726.    
  1727.     static TreeMap<Integer, LinkedList<Integer>> map= new TreeMap<>();
  1728.    
  1729.     static class TreeNode {
  1730.         int data;
  1731.         TreeNode left=null,right=null;
  1732.         TreeNode(int d){
  1733.             data=d;
  1734.         }
  1735.     }
  1736.     static TreeNode buildTrees(String st[])
  1737.     {
  1738.         if(st[0]=="N" || st.length==0)
  1739.         return null;
  1740.         TreeNode root= new TreeNode(Integer.parseInt(st[0]));
  1741.         Queue<TreeNode> Q=new LinkedList<TreeNode>();
  1742.         Q.add(root);
  1743.         int i=1;
  1744.         while(!Q.isEmpty() && i<st.length)
  1745.         {
  1746.             TreeNode current_element=Q.poll();
  1747.             String current_value=st[i];
  1748.             if(!current_value.equals("N"))
  1749.             {
  1750.                 int element=Integer.parseInt(current_value);
  1751.                 current_element.left=new TreeNode(element);
  1752.                 Q.add(current_element.left);
  1753.             }
  1754.             i++;
  1755.             if(i>=st.length)
  1756.               break;
  1757.               current_value=st[i];
  1758.             if(!current_value.equals("N"))
  1759.             {
  1760.                 int element=Integer.parseInt(current_value);
  1761.                 current_element.right=new TreeNode(element);
  1762.                 Q.add(current_element.right);
  1763.             }
  1764.             i++;  
  1765.         }return root;}
  1766.    
  1767.     static LinkedList<Integer> key=new LinkedList<>();
  1768.  
  1769.     static void MapOrder(TreeNode crt,int x){
  1770.         if(crt==null)
  1771.          return;
  1772.          
  1773.          MapOrder(crt.left,x-1);
  1774.          LinkedList<Integer> t= map.get(x);
  1775.          
  1776.          if(t==null){
  1777.              t= new LinkedList<>();
  1778.              t.add(crt.data);
  1779.       key.add(x);
  1780.          }
  1781.          else
  1782.           t.add(crt.data);
  1783.          
  1784.          map.put(x,t);
  1785.          
  1786.          MapOrder(crt.right,x+1);
  1787.     }
  1788.    
  1789.  public static void main(String[] args) {
  1790.     Scanner sc=new Scanner(System.in);
  1791.         String st[]=sc.nextLine().split(" ");
  1792.         TreeNode root= buildTrees(st);
  1793.   MapOrder(root,0);
  1794.  
  1795.          key.sort(Comparator.naturalOrder());
  1796.  
  1797.   while(key.peek()!=null)
  1798.    System.out.println(map.get(key.remove()));  
  1799.  
  1800.  }
  1801. }
  1802.  
  1803.  
  1804.  
  1805.  
  1806. // ****************Right view:**********************
  1807.  
  1808.  
  1809. import java.util.Scanner;
  1810.  
  1811. class Node {
  1812.     Node left;
  1813.     Node right;
  1814.     int data;
  1815. }
  1816.  
  1817. class BinaryTree {
  1818.     int maxLevel;
  1819.  
  1820.     public void rightViewOfTree(Node node, int level) {
  1821.         if (node == null) {
  1822.             return;
  1823.         }
  1824.  
  1825.         if (level >= maxLevel) {
  1826.             System.out.print(node.data + " ");
  1827.             maxLevel++;
  1828.         }
  1829.  
  1830.         rightViewOfTree(node.right, level + 1);
  1831.         rightViewOfTree(node.left, level + 1);
  1832.     }
  1833.  
  1834.     public Node createNewNode(int val) {
  1835.         Node newNode = new Node();
  1836.         newNode.data = val;
  1837.         newNode.left = null;
  1838.         newNode.right = null;
  1839.         return newNode;
  1840.     }
  1841. }
  1842.  
  1843. public class RightView {
  1844.  
  1845.     public static void main(String[] args) {
  1846.         Scanner scanner = new Scanner(System.in);
  1847.         BinaryTree binaryTree = new BinaryTree();
  1848.  
  1849.         System.out.print("Enter the number of nodes: ");
  1850.         int numNodes = scanner.nextInt();
  1851.  
  1852.         Node root = null;
  1853.  
  1854.         for (int i = 0; i < numNodes; i++) {
  1855.             System.out.print("Enter value for node " + (i + 1) + ": ");
  1856.             int val = scanner.nextInt();
  1857.             if (i == 0) {
  1858.                 root = binaryTree.createNewNode(val);
  1859.             } else {
  1860.                 insertNode(root, val);
  1861.             }
  1862.         }
  1863.  
  1864.         System.out.println("Right view of the binary tree:");
  1865.         binaryTree.rightViewOfTree(root, 0);
  1866.  
  1867.         scanner.close();
  1868.     }
  1869.  
  1870.     public static void insertNode(Node root, int val) {
  1871.         if (val < root.data) {
  1872.             if (root.left == null) {
  1873.                 root.left = new Node();
  1874.                 root.left.data = val;
  1875.             } else {
  1876.                 insertNode(root.left, val);
  1877.             }
  1878.         } else {
  1879.             if (root.right == null) {
  1880.                 root.right = new Node();
  1881.                 root.right.data = val;
  1882.             } else {
  1883.                 insertNode(root.right, val);
  1884.             }
  1885.         }
  1886.     }
  1887. }
  1888.  
  1889. // ****************Bottom view:********************
  1890.  
  1891.  
  1892. //BOTTOM VIEW
  1893.  
  1894. import java.util.*;
  1895. import java.util.Map.Entry;
  1896.  
  1897. class Node {
  1898.     int data,hd;
  1899.     Node left, right;
  1900.     public Node(int data){
  1901.         this.data = data;
  1902.         left = right = null;
  1903.         this.hd = Integer. MAX_VALUE;
  1904.     }
  1905. }
  1906.  
  1907. public class Main {
  1908.     static Node root;
  1909.     private List<Integer> path1 = new ArrayList<>();
  1910.     private List<Integer> path2 = new ArrayList<>();
  1911.     static Node build(String s[]){
  1912.         if(s[0]=="N"||s.length==0)
  1913.             return null;
  1914.         Node root=new Node(Integer.parseInt(s[0]));
  1915.    Queue<Node> q=new LinkedList<Node>();
  1916.         q.add(root);
  1917.       int i=1;
  1918.         while(!q.isEmpty() && i<s.length){
  1919.             Node curr=q.poll();
  1920.             String cval=s[i];
  1921.             if(!cval.equals("N")){
  1922.                 int h=Integer.parseInt(cval);
  1923.                 curr.left=new Node(h);
  1924.                 q.add(curr.left);
  1925.             }
  1926.             i++;
  1927.             if(i >= s.length)
  1928.                 break;
  1929.             cval = s[i];
  1930.                if(!cval.equals("N")){
  1931.                    int h=Integer.parseInt(cval);
  1932.                    curr.right=new Node(h);
  1933.                    q.add(curr.right);
  1934.                }
  1935.                i++;
  1936.            }
  1937.            return root;
  1938.  }
  1939.  static void bottomview(Node root){
  1940.         if (root == null)
  1941.             return;
  1942.         int hd = 0;
  1943.         Map<Integer, Integer> map = new TreeMap<>();
  1944.         Queue<Node> queue = new LinkedList<Node>();
  1945.         root.hd = hd;
  1946.         queue.add(root);
  1947.                 while (!queue.isEmpty()){
  1948.             Node temp = queue.remove();
  1949.             hd = temp.hd;
  1950.             map.put(hd, temp.data);
  1951.             if (temp.left != null){
  1952.                 temp.left.hd = hd-1;
  1953.                 queue.add(temp.left);
  1954.             }
  1955.             if (temp.right != null)
  1956.             {
  1957.                 temp.right.hd = hd+1;
  1958.                 queue.add(temp.right);
  1959.             }
  1960.         }
  1961.         Set<Entry<Integer, Integer>> set = map.entrySet();
  1962.         Iterator<Entry<Integer, Integer>> iterator = set.iterator();
  1963.         while (iterator.hasNext()){
  1964.             Map.Entry<Integer, Integer> me = iterator.next();
  1965.             System.out.print(me.getValue()+" ");
  1966.         }
  1967.     }
  1968.     public static void main(String[] args){
  1969.         Scanner sc=new Scanner(System.in);
  1970.         int i;
  1971.         Main ob = new Main();
  1972.         String s[]=sc.nextLine().split(" ");
  1973.         root = build(s);
  1974.         ob.bottomview(root);
  1975.  
  1976.     }
  1977. }
  1978.  
  1979.  
  1980.  
  1981. // ***************Recover BST**************
  1982.  
  1983. import java.util.*;
  1984. import java.io.*;
  1985. import java.util.*;
  1986. class TreeNode
  1987. {
  1988.     int data;
  1989.     TreeNode right;
  1990.     TreeNode left;
  1991.     TreeNode(int data)
  1992.     {
  1993.         this.data=data;
  1994.         this.left=null;
  1995.         this.right=null;
  1996.     }
  1997.  }
  1998. public class Main
  1999. {
  2000.     static TreeNode build(String st[])
  2001.     {
  2002.         if(st[0]=="N" || st.length==0)
  2003.         return null;
  2004.         TreeNode root= new TreeNode(Integer.parseInt(st[0]));
  2005.         Queue<TreeNode> Q=new LinkedList<TreeNode>();
  2006.         Q.add(root);
  2007.         int i=1;
  2008.         while(!Q.isEmpty() && i<st.length)
  2009.         {
  2010.             TreeNode current_element=Q.poll();
  2011.             String current_value=st[i];
  2012.             if(!current_value.equals("N"))
  2013.             {
  2014.                 int element=Integer.parseInt(current_value);
  2015.                 current_element.left=new TreeNode(element);
  2016.                 Q.add(current_element.left);
  2017.             }
  2018.             i++;
  2019.             if(i>=st.length)
  2020.               break;
  2021.               current_value=st[i];
  2022.             if(!current_value.equals("N"))
  2023.             {
  2024.                 int element=Integer.parseInt(current_value);
  2025.                 current_element.right=new TreeNode(element);
  2026.                 Q.add(current_element.right);
  2027.             }
  2028.             i++;  
  2029.         }return root;}
  2030.  
  2031.      static TreeNode firstNode,secondNode,prevNode;
  2032.     public static void recoverTree(TreeNode root) {
  2033.         if(root==null)
  2034.             return;
  2035.         inorderTraversal(root);
  2036.         if(firstNode != null && secondNode != null){
  2037.             int temp = secondNode.data;
  2038.             secondNode.data = firstNode.data;
  2039.             firstNode.data = temp;
  2040.         }
  2041.     }
  2042.     private static void inorderTraversal(TreeNode root){
  2043.         if(root==null)
  2044.             return;
  2045.          inorderTraversal(root.left);
  2046.         if(prevNode != null && root.data < prevNode.data && firstNode == null) {
  2047.                firstNode = prevNode;
  2048.         }
  2049.         if (prevNode != null && root.data < prevNode.data && firstNode != null) {
  2050.                 sec
  2051.         }
  2052.         prevNode = root;
  2053.         inorderTraversal(root.right);
  2054.     }
  2055.     public static void inorder(TreeNode temp)
  2056.     {
  2057.         if(temp==null)
  2058.             return;
  2059.         inorder(temp.left);
  2060.         System.out.print(temp.data+" ");
  2061.         inorder(temp.right);
  2062.     }
  2063.  public static void main(String[] args)
  2064.  {
  2065.      Scanner sc= new Scanner(System.in);
  2066.      String st[]=sc.nextLine().split(" ");
  2067.      if(st[0].equals("N"))
  2068.      {
  2069.   System.out.println("0");
  2070.   return;
  2071.      }
  2072.      TreeNode root=build(st);
  2073.      inorder(root);
  2074.      recoverTree(root);
  2075.      System.out.println("nAfter Recovery");
  2076.             inorder(root);
  2077. }
  2078. }
  2079.  
  2080.  
  2081. // *************Left View Of Tree:*********************
  2082. import java.util.Scanner;
  2083.  
  2084. class Node {
  2085.     Node left;
  2086.     Node right;
  2087.     int data;
  2088. }
  2089.  
  2090. class BinaryTree {
  2091.     int maxLevel;
  2092.  
  2093.     public void leftViewOfTree(Node node, int level) {
  2094.         if (node == null) {
  2095.             return;
  2096.         }
  2097.         if (level >= maxLevel) {
  2098.             System.out.print(node.data + " ");
  2099.             maxLevel++;
  2100.         }
  2101.         leftViewOfTree(node.left, level + 1);
  2102.         leftViewOfTree(node.right, level + 1);
  2103.     }
  2104.  
  2105.     public Node createNewNode(int val) {
  2106.         Node newNode = new Node();
  2107.         newNode.data = val;
  2108.         newNode.left = null;
  2109.         newNode.right = null;
  2110.         return newNode;
  2111.     }
  2112. }
  2113.  
  2114. public class Main {
  2115.  
  2116.     public static void main(String[] args) {
  2117.         Scanner scanner = new Scanner(System.in);
  2118.         BinaryTree binaryTree = new BinaryTree();
  2119.  
  2120.         System.out.print("Enter the number of nodes: ");
  2121.         int numNodes = scanner.nextInt();
  2122.  
  2123.         Node root = null;
  2124.  
  2125.         for (int i = 0; i < numNodes; i++) {
  2126.             System.out.print("Enter value for node " + (i + 1) + ": ");
  2127.             int val = scanner.nextInt();
  2128.             if (i == 0) {
  2129.                 root = binaryTree.createNewNode(val);
  2130.             } else {
  2131.                 insertNode(root, val);
  2132.             }
  2133.         }
  2134.  
  2135.         System.out.println("Left view of the binary tree:");
  2136.         binaryTree.leftViewOfTree(root, 0);
  2137.  
  2138.         scanner.close();
  2139.     }
  2140.  
  2141.     public static void insertNode(Node root, int val) {
  2142.         if (val < root.data) {
  2143.             if (root.left == null) {
  2144.                 root.left = new Node();
  2145.                 root.left.data = val;
  2146.             } else {
  2147.                 insertNode(root.left, val);
  2148.             }
  2149.         } else {
  2150.             if (root.right == null) {
  2151.                 root.right = new Node();
  2152.                 root.right.data = val;
  2153.             } else {
  2154.                 insertNode(root.right, val);
  2155.             }
  2156.         }
  2157.     }
  2158. }
  2159.  
  2160.