0% found this document useful (0 votes)
10 views6 pages

CSD201 PE Cheats

CSD

Uploaded by

ngohonghai712
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views6 pages

CSD201 PE Cheats

CSD

Uploaded by

ngohonghai712
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

// Java code to implement the approach Find the level of the node

// Driver Code Euler path in undirected graph

/* Driver program to test above functions */ find height of tree

// Driver program to test above functions print reverse level order traversal
// Function to find the level of the node

public static int findLevel(int N, int[][] edges, int X){

int maxVertex = 0;

for (int[] it : edges) {

maxVertex = Math.max(maxVertex, Math.max(it[0], it[1]));

// Creating adjacency list

ArrayList<Integer>[] adj

= new ArrayList[maxVertex + 1];

for (int i = 0; i <= maxVertex; i++)

adj[i] = new ArrayList<>();

for (int i = 0; i < edges.length; i++) {

adj[edges[i][0]].add(edges[i][1]);

adj[edges[i][1]].add(edges[i][0]);

// X is not present

if (X > maxVertex || adj[X].size() == 0)

return -1;

// Queue for BFS traversal

LinkedList<Integer> q = new LinkedList<>();

q.offer(0);

int level = 0;

boolean[] visited = new boolean[maxVertex + 1];

// BFS traversal

while (!q.isEmpty()) {

int sz = q.size();

while (sz-- > 0) {

int currentNode = q.poll();

if (currentNode == X)

return level;

for (int it : adj[currentNode]) {

if (!visited[it]) {

q.offer(it);

visited[it] = true;

level++;

return -1;
// Efficient Java program to

// find out Eulerian path

import java.util.*;

class GFG

// Function to find out the path

// It takes the adjacency matrix

// representation of the graph as input

static void findpath(int[][] graph, int n){

Vector<Integer> numofadj = new Vector<>();

// Find out number of edges each vertex has

for (int i = 0; i < n; i++)

numofadj.add(accumulate(graph[i], 0));

// Find out how many vertex has odd number edges

int startPoint = 0, numofodd = 0;

for (int i = n - 1; i >= 0; i--){

if (numofadj.elementAt(i) % 2 == 1) {

numofodd++;

startPoint = i;

// If number of vertex with odd number of edges

// is greater than two return "No Solution".

if (numofodd > 2){

System.out.println("No Solution");

return;

// If there is a path find the path

// Initialize empty stack and path

// take the starting current as discussed

Stack<Integer> stack = new Stack<>();

Vector<Integer> path = new Vector<>();

int cur = startPoint;

// Loop will run until there is element in the stack

// or current edge has some neighbour.

while (!stack.isEmpty() || accumulate(graph[cur], 0) != 0){

// If current node has not any neighbour

// add it to path and pop stack

// set new current to the popped element

if (accumulate(graph[cur], 0) == 0){

path.add(cur);

cur = stack.pop();
// Java program to find height of tree

// A binary tree node

class Node {

int data;

Node left, right;

Node(int item)

data = item;

left = right = null;

class BinaryTree {

Node root;

/* Compute the "maxDepth" of a tree -- the number of

nodes along the longest path from the root node

down to the farthest leaf node.*/

int maxDepth(Node node){

if (node == null)

return 0;

else {

/* compute the depth of each subtree */

int lDepth = maxDepth(node.left);

int rDepth = maxDepth(node.right);

/* use the larger one */

if (lDepth > rDepth)

return (lDepth + 1);

else

return (rDepth + 1);

}
// A recursive java program to print reverse level order traversal

// A binary tree node

class Node {

int data;

Node left, right;

Node(int item) {

data = item;

left = right;

class BinaryTree {

Node root;

/* Function to print REVERSE level order traversal a tree*/

void reverseLevelOrder(Node node) {

int h = height(node);

int i;

for (i = h; i >= 1; i--)

//THE ONLY LINE DIFFERENT FROM NORMAL LEVEL ORDER

printGivenLevel(node, i);

/* Print nodes at a given level */

void printGivenLevel(Node node, int level) {

if (node == null)

return;

if (level == 1)

System.out.print(node.data + " ");

else if (level > 1) {

printGivenLevel(node.left, level - 1);

printGivenLevel(node.right, level - 1);

/* Compute the "height" of a tree -- the number of

nodes along the longest path from the root node

down to the farthest leaf node.*/

int height(Node node) {

if (node == null)

return 0;
// TEST CODE HERE

You might also like