Skip to content

Commit 5ec507a

Browse files
committed
#Modification 29
1 parent 4a96079 commit 5ec507a

File tree

56 files changed

+316
-1139
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+316
-1139
lines changed

backtracking/Word_Break_PUB.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package backtracking;
2+
import java.util.*;
3+
4+
public class Word_Break_PUB {
5+
6+
private static void backtrack(String sentence, String A, String sentence2, ArrayList<String> result) {
7+
8+
if(A.length() == 0) {
9+
result.add(sentence);
10+
return;
11+
}
12+
13+
for(int i = 0; i < A.length(); i++) {
14+
15+
String substring = A.substring(0, i + 1);
16+
17+
if(sentence2.contains(substring)) {
18+
String newSentence = sentence + " " + substring;
19+
backtrack(newSentence.trim(), A.substring( i + 1, A.length() ), sentence2, result);
20+
}
21+
}
22+
}
23+
24+
public static ArrayList<String> wordBreak(String A, String sentence) {
25+
ArrayList<String> result = new ArrayList<>();
26+
backtrack("", A, sentence, result);
27+
return result;
28+
}
29+
30+
public static void main(String[] args) {
31+
32+
String sentence = "I Like mango icecream and SamSung Mobile";
33+
String A = null;
34+
String[] result;
35+
String[] dictionary = {"mobile", "samsung", "sam", "sung", "man", "mango", "icecream", "and", "go", "i", "like",
36+
"icecream"};
37+
38+
System.out.println(wordBreak(A,sentence));
39+
}
40+
41+
}

binaryTree/BT_Problem_06_a.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package binaryTree;
22
import java.util.*;
33
/*
4-
* Problem Title :- In-order Traversal of a tree both using Recursion
4+
* Problem Title :- In-order Traversal of a tree without using Recursion
55
*/
66
// Class to print the in-order traversal
77
public class BT_Problem_06_a {

binaryTree/BT_Problem_06_b.java

Lines changed: 30 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,98 +1,48 @@
11
package binaryTree;
22

33
/*
4-
* Problem Title :- In-order Traversal of a tree both using Recursion
4+
* Problem Title :- In-order Traversal of a tree using Recursion
55
*/
66
public class BT_Problem_06_b {
77

8-
// Root of Binary Tree
9-
Node root;
8+
Node root;
9+
10+
// Constructor
11+
BT_Problem_06_b(){
12+
root = null;
13+
}
1014

11-
// Constructor
12-
BT_Problem_06_b(){
13-
root = null;
14-
}
15-
16-
/*
17-
* Given a binary tree,
18-
* print its nodes according to the
19-
* "bottom-up" post-order traversal.
20-
*/
21-
void printPostorder(Node node) {
22-
if(node == null)
23-
return;
24-
// first recur on left subtree
25-
printPostorder(node.left);
26-
27-
// then recur on right subtree
28-
printPostorder(node.right);
29-
30-
// now deal with the node
31-
System.out.print(node.data + " ");
32-
}
33-
34-
/*
35-
* Given a binary tree,
36-
* print its nodes in in-order
37-
*/
38-
void printInorder(Node node) {
15+
//Given a binary tree, print its nodes in in-order
16+
void printInorder(Node node) {
3917

40-
if(node == null) return;
18+
if(node == null) return;
4119

42-
/* first recur on left child */
43-
printPreorder(node.left);
20+
/* first recur on left child */
21+
printInorder(node.left);
4422

45-
/* then print data of node */
46-
System.out.print(node.data + " ");
23+
/* then print data of node */
24+
System.out.print(node.data + " ");
4725

48-
/* now recur on right child */
49-
printPreorder(node.right);
50-
}
26+
/* now recur on right child */
27+
printInorder(node.right);
28+
}
5129

52-
/*
53-
* Given a binary tree,
54-
* print its nodes in preorder
55-
*/
56-
void printPreorder(Node node) {
57-
58-
if(node == null) return;
59-
60-
/* first print data of node */
61-
System.out.print(node.data + " ");
62-
63-
/* then recur on left subtree */
64-
printPreorder(node.left);
65-
66-
/* now recur on right subtree */
67-
printPreorder(node.right);
68-
}
30+
// Wrappers over above recursive function
31+
void printInorder() { printInorder(root); }
6932

70-
// Wrappers over above recursive functions
71-
void printPostorder() { printPostorder(root);}
72-
void printInorder() { printInorder(root); }
73-
void printPreorder() { printPreorder(root); }
7433

75-
// Driver method
76-
public static void main(String[] args) {
34+
// Driver method
35+
public static void main(String[] args) {
7736

78-
BT_Problem_06_b tree = new BT_Problem_06_b();
79-
80-
tree.root = new Node(1);
81-
tree.root.left = new Node(2);
82-
tree.root.right = new Node(3);
83-
tree.root.left.left = new Node(4);
84-
tree.root.left.right = new Node(5);
85-
86-
System.out.println("Preorder traversal of binary tree is ");
87-
tree.printPreorder();
37+
BT_Problem_06_b tree = new BT_Problem_06_b();
8838

89-
System.out.println("\nInorder traversal of binary tree is ");
90-
tree.printInorder();
39+
tree.root = new Node(1);
40+
tree.root.left = new Node(2);
41+
tree.root.right = new Node(3);
42+
tree.root.left.left = new Node(4);
43+
tree.root.left.right = new Node(5);
9144

92-
System.out.println("\nPostorder traversal of binary tree is ");
93-
tree.printPostorder();
94-
95-
}
96-
97-
45+
System.out.println("\nInorder traversal of binary tree is ");
46+
tree.printInorder();
47+
}
9848
}

binaryTree/BT_Problem_07.java

Lines changed: 0 additions & 15 deletions
This file was deleted.

binaryTree/BT_Problem_07_a.java

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package binaryTree;
2+
import java.util.*;
3+
4+
/*
5+
* Problem Title :- Preorder Traversal of a tree without using Recursion or Iteratively
6+
*/
7+
public class BT_Problem_07_a {
8+
9+
// Root of Binary Tree
10+
Node root;
11+
12+
// Given a binary tree, print its nodes in pre-order
13+
void preorder() {
14+
15+
if(root == null) return;
16+
17+
Stack<Node> s = new Stack<>();
18+
s.push(root);
19+
20+
// traverse the tree
21+
while(s.empty() == false) {
22+
23+
Node mynode = s.peek();
24+
System.out.print(mynode.data + " ");
25+
26+
s.pop();
27+
28+
//Push right child of popped node to stack
29+
if(mynode.right != null)
30+
s.push(mynode.right);
31+
32+
//Push left child of popped node to stack
33+
if(mynode.left != null)
34+
s.push(mynode.left);
35+
36+
}
37+
}
38+
39+
// Driver method
40+
public static void main(String[] args) {
41+
42+
// creating a binary tree and entering the nodes
43+
BT_Problem_07_a tree = new BT_Problem_07_a();
44+
45+
tree.root = new Node(1);
46+
tree.root.left = new Node(2);
47+
tree.root.right = new Node(3);
48+
tree.root.left.left = new Node(4);
49+
tree.root.left.right = new Node(5);
50+
51+
tree.preorder();
52+
53+
}
54+
55+
}

binaryTree/BT_Problem_07_b.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package binaryTree;
2+
3+
/*
4+
* Problem Title :- Pre-order Traversal of a tree using Recursion
5+
*/
6+
public class BT_Problem_07_b {
7+
// Root of Binary Tree
8+
Node root;
9+
10+
// Constructor
11+
BT_Problem_07_b(){
12+
root = null;
13+
}
14+
15+
/*
16+
* Given a binary tree,
17+
* print its nodes in preorder
18+
*/
19+
void printPreorder(Node node) {
20+
21+
if(node == null) return;
22+
23+
/* first print data of node */
24+
System.out.print(node.data + " ");
25+
26+
/* then recur on left subtree */
27+
printPreorder(node.left);
28+
29+
/* now recur on right subtree */
30+
printPreorder(node.right);
31+
}
32+
33+
// Wrappers over above recursive function
34+
void printPreorder() {
35+
printPreorder(root);
36+
}
37+
38+
//Driver Code
39+
public static void main(String[] args) {
40+
BT_Problem_07_b tree = new BT_Problem_07_b();
41+
42+
tree.root = new Node(1);
43+
tree.root.left = new Node(2);
44+
tree.root.right = new Node(3);
45+
tree.root.left.left = new Node(4);
46+
tree.root.left.right = new Node(5);
47+
48+
System.out.println("Preorder traversal of binary tree is ");
49+
tree.printPreorder();
50+
}
51+
52+
}

binaryTree/BT_Problem_08.java

Lines changed: 0 additions & 10 deletions
This file was deleted.

0 commit comments

Comments
 (0)