-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4a96079
commit 5ec507a
Showing
56 changed files
with
316 additions
and
1,139 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package backtracking; | ||
import java.util.*; | ||
|
||
public class Word_Break_PUB { | ||
|
||
private static void backtrack(String sentence, String A, String sentence2, ArrayList<String> result) { | ||
|
||
if(A.length() == 0) { | ||
result.add(sentence); | ||
return; | ||
} | ||
|
||
for(int i = 0; i < A.length(); i++) { | ||
|
||
String substring = A.substring(0, i + 1); | ||
|
||
if(sentence2.contains(substring)) { | ||
String newSentence = sentence + " " + substring; | ||
backtrack(newSentence.trim(), A.substring( i + 1, A.length() ), sentence2, result); | ||
} | ||
} | ||
} | ||
|
||
public static ArrayList<String> wordBreak(String A, String sentence) { | ||
ArrayList<String> result = new ArrayList<>(); | ||
backtrack("", A, sentence, result); | ||
return result; | ||
} | ||
|
||
public static void main(String[] args) { | ||
|
||
String sentence = "I Like mango icecream and SamSung Mobile"; | ||
String A = null; | ||
String[] result; | ||
String[] dictionary = {"mobile", "samsung", "sam", "sung", "man", "mango", "icecream", "and", "go", "i", "like", | ||
"icecream"}; | ||
|
||
System.out.println(wordBreak(A,sentence)); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,98 +1,48 @@ | ||
package binaryTree; | ||
|
||
/* | ||
* Problem Title :- In-order Traversal of a tree both using Recursion | ||
* Problem Title :- In-order Traversal of a tree using Recursion | ||
*/ | ||
public class BT_Problem_06_b { | ||
|
||
// Root of Binary Tree | ||
Node root; | ||
Node root; | ||
|
||
// Constructor | ||
BT_Problem_06_b(){ | ||
root = null; | ||
} | ||
|
||
// Constructor | ||
BT_Problem_06_b(){ | ||
root = null; | ||
} | ||
|
||
/* | ||
* Given a binary tree, | ||
* print its nodes according to the | ||
* "bottom-up" post-order traversal. | ||
*/ | ||
void printPostorder(Node node) { | ||
if(node == null) | ||
return; | ||
// first recur on left subtree | ||
printPostorder(node.left); | ||
|
||
// then recur on right subtree | ||
printPostorder(node.right); | ||
|
||
// now deal with the node | ||
System.out.print(node.data + " "); | ||
} | ||
|
||
/* | ||
* Given a binary tree, | ||
* print its nodes in in-order | ||
*/ | ||
void printInorder(Node node) { | ||
//Given a binary tree, print its nodes in in-order | ||
void printInorder(Node node) { | ||
|
||
if(node == null) return; | ||
if(node == null) return; | ||
|
||
/* first recur on left child */ | ||
printPreorder(node.left); | ||
/* first recur on left child */ | ||
printInorder(node.left); | ||
|
||
/* then print data of node */ | ||
System.out.print(node.data + " "); | ||
/* then print data of node */ | ||
System.out.print(node.data + " "); | ||
|
||
/* now recur on right child */ | ||
printPreorder(node.right); | ||
} | ||
/* now recur on right child */ | ||
printInorder(node.right); | ||
} | ||
|
||
/* | ||
* Given a binary tree, | ||
* print its nodes in preorder | ||
*/ | ||
void printPreorder(Node node) { | ||
|
||
if(node == null) return; | ||
|
||
/* first print data of node */ | ||
System.out.print(node.data + " "); | ||
|
||
/* then recur on left subtree */ | ||
printPreorder(node.left); | ||
|
||
/* now recur on right subtree */ | ||
printPreorder(node.right); | ||
} | ||
// Wrappers over above recursive function | ||
void printInorder() { printInorder(root); } | ||
|
||
// Wrappers over above recursive functions | ||
void printPostorder() { printPostorder(root);} | ||
void printInorder() { printInorder(root); } | ||
void printPreorder() { printPreorder(root); } | ||
|
||
// Driver method | ||
public static void main(String[] args) { | ||
// Driver method | ||
public static void main(String[] args) { | ||
|
||
BT_Problem_06_b tree = new BT_Problem_06_b(); | ||
|
||
tree.root = new Node(1); | ||
tree.root.left = new Node(2); | ||
tree.root.right = new Node(3); | ||
tree.root.left.left = new Node(4); | ||
tree.root.left.right = new Node(5); | ||
|
||
System.out.println("Preorder traversal of binary tree is "); | ||
tree.printPreorder(); | ||
BT_Problem_06_b tree = new BT_Problem_06_b(); | ||
|
||
System.out.println("\nInorder traversal of binary tree is "); | ||
tree.printInorder(); | ||
tree.root = new Node(1); | ||
tree.root.left = new Node(2); | ||
tree.root.right = new Node(3); | ||
tree.root.left.left = new Node(4); | ||
tree.root.left.right = new Node(5); | ||
|
||
System.out.println("\nPostorder traversal of binary tree is "); | ||
tree.printPostorder(); | ||
|
||
} | ||
|
||
|
||
System.out.println("\nInorder traversal of binary tree is "); | ||
tree.printInorder(); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package binaryTree; | ||
import java.util.*; | ||
|
||
/* | ||
* Problem Title :- Preorder Traversal of a tree without using Recursion or Iteratively | ||
*/ | ||
public class BT_Problem_07_a { | ||
|
||
// Root of Binary Tree | ||
Node root; | ||
|
||
// Given a binary tree, print its nodes in pre-order | ||
void preorder() { | ||
|
||
if(root == null) return; | ||
|
||
Stack<Node> s = new Stack<>(); | ||
s.push(root); | ||
|
||
// traverse the tree | ||
while(s.empty() == false) { | ||
|
||
Node mynode = s.peek(); | ||
System.out.print(mynode.data + " "); | ||
|
||
s.pop(); | ||
|
||
//Push right child of popped node to stack | ||
if(mynode.right != null) | ||
s.push(mynode.right); | ||
|
||
//Push left child of popped node to stack | ||
if(mynode.left != null) | ||
s.push(mynode.left); | ||
|
||
} | ||
} | ||
|
||
// Driver method | ||
public static void main(String[] args) { | ||
|
||
// creating a binary tree and entering the nodes | ||
BT_Problem_07_a tree = new BT_Problem_07_a(); | ||
|
||
tree.root = new Node(1); | ||
tree.root.left = new Node(2); | ||
tree.root.right = new Node(3); | ||
tree.root.left.left = new Node(4); | ||
tree.root.left.right = new Node(5); | ||
|
||
tree.preorder(); | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package binaryTree; | ||
|
||
/* | ||
* Problem Title :- Pre-order Traversal of a tree using Recursion | ||
*/ | ||
public class BT_Problem_07_b { | ||
// Root of Binary Tree | ||
Node root; | ||
|
||
// Constructor | ||
BT_Problem_07_b(){ | ||
root = null; | ||
} | ||
|
||
/* | ||
* Given a binary tree, | ||
* print its nodes in preorder | ||
*/ | ||
void printPreorder(Node node) { | ||
|
||
if(node == null) return; | ||
|
||
/* first print data of node */ | ||
System.out.print(node.data + " "); | ||
|
||
/* then recur on left subtree */ | ||
printPreorder(node.left); | ||
|
||
/* now recur on right subtree */ | ||
printPreorder(node.right); | ||
} | ||
|
||
// Wrappers over above recursive function | ||
void printPreorder() { | ||
printPreorder(root); | ||
} | ||
|
||
//Driver Code | ||
public static void main(String[] args) { | ||
BT_Problem_07_b tree = new BT_Problem_07_b(); | ||
|
||
tree.root = new Node(1); | ||
tree.root.left = new Node(2); | ||
tree.root.right = new Node(3); | ||
tree.root.left.left = new Node(4); | ||
tree.root.left.right = new Node(5); | ||
|
||
System.out.println("Preorder traversal of binary tree is "); | ||
tree.printPreorder(); | ||
} | ||
|
||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.