Skip to content

Commit d8ae1b3

Browse files
committed
10/23
1 parent 28b1755 commit d8ae1b3

File tree

3 files changed

+134
-1
lines changed

3 files changed

+134
-1
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package DSA.Graphs;
2+
3+
import java.util.*;
4+
5+
public class AlienDictionaryTry {
6+
7+
8+
static class Graph{
9+
int v;
10+
LinkedList<Character>[] adjList;
11+
boolean[] visited;
12+
13+
public Graph(int v) {
14+
this.v = v;
15+
adjList = new LinkedList[v];
16+
visited = new boolean[v];
17+
for (int i=0;i<v;i++){
18+
adjList[i] = new LinkedList<>();
19+
}
20+
}
21+
22+
void addEdge(int src, char dest){
23+
adjList[src].add(dest);
24+
25+
}
26+
}
27+
28+
public static void main(String[] args) {
29+
String[] words = {"caa", "aaa", "aab"};
30+
int noOfAlphabets = 3;
31+
printOrder(words, noOfAlphabets);
32+
}
33+
static Stack<Character> st = new Stack<>();
34+
35+
private static void printOrder(String[] words, int count) {
36+
int n = words.length;
37+
Graph g = new Graph(count);
38+
for(int i=0;i<n-1;i++){
39+
String a = words[i];
40+
String b = words[i+1];
41+
42+
int j = (a.length()>b.length())? b.length():a.length();
43+
while(j>0){
44+
if(a.charAt(j-1)!=b.charAt(j-1)){
45+
g.addEdge(a.charAt(j-1)-97, b.charAt(j-1));
46+
break;
47+
}
48+
j--;
49+
}
50+
}
51+
for(int i=0;i<count;i++){
52+
if(!g.visited[i])
53+
topoSort(g,i);
54+
}
55+
56+
while (!st.empty()){
57+
System.out.print(st.pop()+" ");
58+
}
59+
}
60+
61+
private static void topoSort(Graph g,int src) {
62+
63+
g.visited[src] = true;
64+
Iterator<Character> it = g.adjList[src].listIterator();
65+
char c;
66+
while(it.hasNext()){
67+
c = it.next();
68+
if(!g.visited[c-97]){
69+
topoSort(g,c-97);
70+
}
71+
}
72+
st.push((char)(src+'a'));
73+
}
74+
}

src/DSA/Graphs/MST/Prim.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ public void primMST(int[][] graph) {
4040
//Start of graph
4141
keySet[0]=0;
4242
minSpanSet[0]= -1;
43-
int counter =0;
4443
for(int i=0;i<v-1;i++) {
4544
int min_key = findMinKey(keySet,spanned);
4645
spanned[min_key] = true;
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package DSA.Trees;
2+
3+
import java.util.LinkedList;
4+
import java.util.Queue;
5+
6+
public class ConnectNodesSameLevel {
7+
8+
static class Node{
9+
int data;
10+
Node left, right, nextRight;
11+
12+
public Node(int key) {
13+
this.data = key;
14+
left = right = nextRight = null;
15+
}
16+
}
17+
18+
public static void main(String[] args) {
19+
Node root = new Node(10);
20+
root.left = new Node(8);
21+
root.right = new Node(2);
22+
root.left.left = new Node(3);
23+
root.right.right = new Node(90);
24+
25+
// Populates nextRight pointer in all nodes
26+
connect(root);
27+
28+
// Let us check the values of nextRight pointers
29+
System.out.println("Following are populated nextRight pointers in \n" +
30+
"the tree (-1 is printed if there is no nextRight)");
31+
System.out.println("nextRight of "+ root.data +" is "+
32+
((root.nextRight != null) ? root.nextRight.data : -1));
33+
System.out.println("nextRight of "+ root.left.data+" is "+
34+
((root.left.nextRight != null) ? root.left.nextRight.data : -1));
35+
System.out.println("nextRight of "+ root.right.data+" is "+
36+
((root.right.nextRight != null) ? root.right.nextRight.data : -1));
37+
System.out.println("nextRight of "+ root.left.left.data+" is "+
38+
((root.left.left.nextRight != null) ? root.left.left.nextRight.data : -1));
39+
System.out.println("nextRight of "+ root.right.right.data+" is "+
40+
((root.right.right.nextRight != null) ? root.right.right.nextRight.data : -1));
41+
}
42+
43+
private static void connect(Node root) {
44+
Queue<Node> q = new LinkedList<>();
45+
q.add(root);
46+
q.add(null);
47+
while(!q.isEmpty()){
48+
Node temp = q.poll();
49+
if(temp!=null) {
50+
temp.nextRight = q.peek();
51+
if (temp.left != null)
52+
q.add(temp.left);
53+
if (temp.right != null)
54+
q.add(temp.right);
55+
}
56+
else if(!q.isEmpty())
57+
q.add(null);
58+
}
59+
}
60+
}

0 commit comments

Comments
 (0)