Posts

Showing posts with the label dsa

Graph Implementation in java Bfs and Dfs

Image
Graph Data Structure implementation in java import java.util.*; class Graph { ArrayList<ArrayList<Integer>> list = new ArrayList<ArrayList<Integer>>(); int V; public Graph(int v){ this.V = v; for(int i=0;i<V;i++) list.add(new ArrayList<Integer>()); } void add(int s , int d){ list.get(s).add(d); list.get(d).add(s); } void display(){ for(int i=0;i<V;i++){ System.out.print(i+" : "); for(int x : list.get(i)) System.out.print(" => "+x); System.out.println(""); } } void dfs(int start){ boolean visited[] = new boolean[V]; _dfs(start,visited); } void _dfs(int node , boolean[] visited){ visited[node] = true; System.out.print(node+" => "); for(int x : list.get(node)){ if(!visited[x]){ _dfs(x,visited); } } } void bfs(int start){ boolean visited[] = new boolean[V]; Queue<Integer> q = new LinkedList<Integer>(); q.add(start); vis...

Graph in Java

Solving Graph in Java import java.util.*; class Graph { LinkedList list[] ; public Graph(int V){ list = new LinkedList[V]; for(int i=0;i V;i++) list[i] = new LinkedList (); } void addEdge(int source , int destination){ list[source].add(destination); list[destination].add(source); } public int bfs(int source , int destination){ boolean visited[] = new boolean[list.length]; int parent[] = new int[list.length]; Queue q = new LinkedList (); q.add(source); parent[source] = -1; visited[source] = true; while(!q.isEmpty()){ int curr = q.poll(); if(curr == destination) break; for(int neighbor : list[curr]){ if(!visited[neighbor]){ visited[neighbor] = true; q.add(neighbor); parent[neighbor] = curr; } } } int cur = destination; int distance = 0; while(parent[cur] != -1) { ...

Another Binary Tree

import java.util.*; class solution { static Scanner input = new Scanner(System.in); static Node createTree(){ Node root = null; System.out.println("Enter Data : "); int data = input.nextInt(); if(data == -1) return root; root = new Node(data); System.out.println("Enter to left of "+data); root.left = createTree(); System.out.println("Enter to right of "+data); root.right = createTree(); return root; } static void inOrder(Node root){ if(root == null) return; inOrder(root.left); System.out.println(root.data+"=>"); inOrder(root.right); } static void postOrder(Node root){ if(root == null) return; postOrder(root.left); postOrder(root.right); System.out.println(root.data+"=>"); } static void preOrder(Node root){ if(root == null) return; System.out.println(root.data+"=...

Popular Posts

java:17: error: local variables referenced from a lambda expression must be final or effectively final count ++ ;

Family Tree Project in Java

Creating basic tic tac toe android app using java