Skip to content

Commit

Permalink
#Modification 29
Browse files Browse the repository at this point in the history
  • Loading branch information
amangit1314 committed May 4, 2021
1 parent 4a96079 commit 5ec507a
Show file tree
Hide file tree
Showing 56 changed files with 316 additions and 1,139 deletions.
41 changes: 41 additions & 0 deletions backtracking/Word_Break_PUB.java
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));
}

}
2 changes: 1 addition & 1 deletion binaryTree/BT_Problem_06_a.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package binaryTree;
import java.util.*;
/*
* Problem Title :- In-order Traversal of a tree both using Recursion
* Problem Title :- In-order Traversal of a tree without using Recursion
*/
// Class to print the in-order traversal
public class BT_Problem_06_a {
Expand Down
110 changes: 30 additions & 80 deletions binaryTree/BT_Problem_06_b.java
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();
}
}
15 changes: 0 additions & 15 deletions binaryTree/BT_Problem_07.java

This file was deleted.

55 changes: 55 additions & 0 deletions binaryTree/BT_Problem_07_a.java
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();

}

}
52 changes: 52 additions & 0 deletions binaryTree/BT_Problem_07_b.java
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();
}

}
10 changes: 0 additions & 10 deletions binaryTree/BT_Problem_08.java

This file was deleted.

Loading

0 comments on commit 5ec507a

Please sign in to comment.