Skip to content

Commit 50af9e2

Browse files
author
Ayush Karn
committed
Added Avl tree, BFS, DFS, Egg Drop problem, Kadane Algo, LCS
1 parent 7ef4ae8 commit 50af9e2

File tree

6 files changed

+492
-0
lines changed

6 files changed

+492
-0
lines changed
Lines changed: 219 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,219 @@
1+
/*
2+
Copyright 2020 Sakshi Khachane
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
14+
15+
public class AVL_Tree
16+
{
17+
//Declaring Node class
18+
private class Node
19+
{
20+
int data;
21+
Node left;
22+
Node right;
23+
int height;
24+
public Node(int data)
25+
{
26+
this.data = data;
27+
this.height = 1;
28+
}
29+
}
30+
31+
private Node root;
32+
33+
public void insert(int num)
34+
{
35+
this.root = insert(this.root, num);
36+
}
37+
38+
39+
//Display node Function
40+
public void inOrder(Node node)
41+
{
42+
if (node != null)
43+
{
44+
inOrder(node.left);
45+
System.out.print(node.data + " ");
46+
inOrder(node.right);
47+
}
48+
}
49+
50+
//preorder traversal
51+
public void preOrder(Node node)
52+
{
53+
if (node != null)
54+
{
55+
System.out.print(node.data + " ");
56+
preOrder(node.left);
57+
preOrder(node.right);
58+
}
59+
}
60+
61+
//post order traversal
62+
public void postOrder(Node node)
63+
{
64+
if (node != null)
65+
{
66+
postOrder(node.left);
67+
postOrder(node.right);
68+
System.out.print(node.data + " ");
69+
}
70+
}
71+
72+
73+
74+
private Node insert(Node node1, int num)
75+
{
76+
77+
if (node1 == null)
78+
{
79+
Node node2 = new Node(num);
80+
return node2;
81+
}
82+
83+
if (num > node1.data)
84+
{
85+
node1.right = insert(node1.right, num);
86+
} else if (num < node1.data)
87+
{
88+
node1.left = insert(node1.left, num);
89+
}
90+
91+
node1.height = Math.max(height(node1.left), height(node1.right)) + 1;
92+
93+
int balancefac = balancefac(node1);
94+
95+
// L-L Case
96+
if (balancefac > 1 && num < node1.left.data)
97+
{
98+
return rightRotate(node1);
99+
}
100+
101+
// R-R Case
102+
if (balancefac < -1 && num > node1.right.data)
103+
{
104+
return leftRotate(node1);
105+
}
106+
107+
// L-R Case
108+
if (balancefac > 1 && num > node1.left.data)
109+
{
110+
node1.left = leftRotate(node1.left);
111+
return rightRotate(node1);
112+
}
113+
114+
// R-L Case
115+
if (balancefac < -1 && num < node1.right.data)
116+
{
117+
node1.right = rightRotate(node1.right);
118+
return leftRotate(node1);
119+
}
120+
return node1;
121+
122+
}
123+
124+
125+
//Finding height of the node
126+
private int height(Node num)
127+
{
128+
if (num == null)
129+
{
130+
return 0;
131+
}
132+
133+
return num.height;
134+
}
135+
136+
//Balance Factor
137+
private int balancefac(Node num)
138+
{
139+
if (num == null)
140+
{
141+
return 0;
142+
}
143+
144+
return height(num.left) - height(num.right);
145+
}
146+
147+
//performing right rotation
148+
private Node rightRotate(Node node3)
149+
{
150+
151+
Node node4 = node3.left;
152+
Node node = node4.right;
153+
154+
node4.right = node3;
155+
node3.left = node;
156+
157+
node3.height = Math.max(height(node3.left), height(node3.right)) + 1;
158+
node4.height = Math.max(height(node4.left), height(node4.right)) + 1;
159+
160+
return node4;
161+
}
162+
163+
//performing left rotation
164+
private Node leftRotate(Node node3)
165+
{
166+
167+
Node node4 = node3.right;
168+
Node node = node4.left;
169+
170+
node4.left = node3;
171+
node3.right = node;
172+
173+
node3.height = Math.max(height(node3.left), height(node3.right)) + 1;
174+
node4.height = Math.max(height(node4.left), height(node4.right)) + 1;
175+
176+
return node4;
177+
}
178+
179+
180+
public static void main(String[] args)
181+
{
182+
183+
AVL_Tree tree = new AVL_Tree();
184+
tree.insert(10);
185+
tree.insert(20);
186+
tree.insert(30);
187+
tree.insert(40);
188+
tree.insert(50);
189+
tree.insert(25);
190+
191+
System.out.println("Preorder display: ");
192+
tree.preOrder(tree.root);
193+
System.out.println("\n\nInorder display: ");
194+
tree.inOrder(tree.root);
195+
System.out.println("\n\nPostorder display: ");
196+
tree.postOrder(tree.root);
197+
198+
199+
}
200+
}
201+
202+
/* Input:
203+
* 10 20 30 40 50 25
204+
*/
205+
206+
207+
208+
/*
209+
* Output:
210+
*
211+
* Preorder display:
212+
30 20 10 25 40 50
213+
214+
Inorder display:
215+
10 20 25 30 40 50
216+
217+
Postorder display:
218+
10 25 20 50 40 30
219+
*/
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
3+
Copyright 2017 Aman Mehara
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
17+
*/
18+
19+
import java.util.HashSet;
20+
import java.util.PriorityQueue;
21+
import java.util.Queue;
22+
import java.util.Set;
23+
24+
public class BreadthFirstTraversal<V> {
25+
26+
public void breadthFirstTraversal(DirectedGraph<V> graph, V vertex) {
27+
28+
final Set<V> visited = new HashSet<>();
29+
final Queue<V> queue = new PriorityQueue<>();
30+
31+
queue.add(vertex);
32+
visited.add(vertex);
33+
34+
while (!queue.isEmpty()) {
35+
36+
V s = queue.remove();
37+
38+
graph.adjacencyList().get(s).forEach(v -> {
39+
if (!visited.contains(v)) {
40+
visited.add(v);
41+
queue.add(v);
42+
}
43+
});
44+
45+
}
46+
47+
}
48+
49+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
3+
Copyright 2017 Aman Mehara
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
17+
*/
18+
19+
import java.util.*;
20+
21+
public class DepthFirstTraversal<V> {
22+
23+
public void depthFirstTraversal(DirectedGraph<V> graph, V vertex) {
24+
25+
final Set<V> visited = new HashSet<>();
26+
recurse(graph, vertex, visited);
27+
28+
}
29+
30+
private void recurse(DirectedGraph<V> graph, V vertex, Set<V> visited) {
31+
32+
visited.add(vertex);
33+
34+
graph.adjacencyList().get(vertex).forEach(v -> {
35+
if (!visited.contains(v)) {
36+
recurse(graph, v, visited);
37+
}
38+
});
39+
40+
}
41+
42+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import java.util.*;
2+
import java.io.*;
3+
4+
public class Egg_Drop {
5+
static int eggDrop(int n,int k)
6+
{
7+
// boundary condition
8+
// if 1 egg and k floors, min attempt = k
9+
// if n eggs and 1 floor, min attempt =1
10+
int T[][]=new int[n][k+1];
11+
for (int i = 0; i <=k; i++) {
12+
T[0][i]=i;
13+
}
14+
for (int i = 0; i < n; i++) {
15+
T[i][0]=0;
16+
T[i][1]=1;
17+
}
18+
for (int i = 1; i < n; i++) {
19+
for (int j = 2; j <=k; j++) {
20+
T[i][j]=999;
21+
for(int x=1;x<=j;x++)
22+
{
23+
int res=1+Math.max(T[i-1][x-1],T[i][j-x]);
24+
if(res<T[i][j])
25+
{
26+
T[i][j]=res;
27+
28+
}
29+
}
30+
}
31+
}
32+
for (int i = 0; i < n; i++) {
33+
for (int j = 0; j <=k; j++) {
34+
System.out.print(T[i][j]+" ");
35+
}
36+
System.out.println("");
37+
}
38+
return (T[n-1][k]);
39+
}
40+
public static void main(String[] args) {
41+
int N=2; //eggs
42+
int K=4;
43+
System.out.println("Minimum Attempts: "+eggDrop(N,K));
44+
45+
}
46+
47+
}

0 commit comments

Comments
 (0)