Skip to content

Commit

Permalink
Add BFS/DFS tree, regex and digitsum
Browse files Browse the repository at this point in the history
  • Loading branch information
targarace committed Jun 7, 2018
1 parent 307549c commit 02b87e9
Show file tree
Hide file tree
Showing 18 changed files with 663 additions and 2 deletions.
Binary file added Java/.DS_Store
Binary file not shown.
Binary file added Java/src/.DS_Store
Binary file not shown.
Binary file added Java/src/com/.DS_Store
Binary file not shown.
Binary file added Java/src/com/godepth/.DS_Store
Binary file not shown.
111 changes: 111 additions & 0 deletions Java/src/com/godepth/Anagrams/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package com.godepth.Anagrams;

import java.util.HashMap;
import java.util.Map;

public class Main {
public static void main(String[] args){
Anagrams anagrams = new Anagrams();
anagrams.delete();
}
}

class Anagrams {

public void delete()
{
String leftRemoveString = "";
String rightRemoveString = "";
String anagramString = "";
String leftString = "cdesvsdctykyuwcfdvdfv";
String rightString = "abchdfbrbrtbvervwdveasxfdbthwc";

Map<String, String> left = new HashMap<>();
left.put("a", "0");
left.put("b", "0");
left.put("c", "0");
left.put("d", "0");
left.put("e", "0");
left.put("f", "0");
left.put("g", "0");
left.put("h", "0");
left.put("i", "0");
left.put("j", "0");
left.put("k", "0");
left.put("l", "0");
left.put("m", "0");
left.put("n", "0");
left.put("o", "0");
left.put("p", "0");
left.put("q", "0");
left.put("r", "0");
left.put("s", "0");
left.put("t", "0");
left.put("u", "0");
left.put("v", "0");
left.put("w", "0");
left.put("x", "0");
left.put("y", "0");
left.put("z", "0");

Map<String, String> right = new HashMap<>();
right.put("a", "0");
right.put("b", "0");
right.put("c", "0");
right.put("d", "0");
right.put("e", "0");
right.put("f", "0");
right.put("g", "0");
right.put("h", "0");
right.put("i", "0");
right.put("j", "0");
right.put("k", "0");
right.put("l", "0");
right.put("m", "0");
right.put("n", "0");
right.put("o", "0");
right.put("p", "0");
right.put("q", "0");
right.put("r", "0");
right.put("s", "0");
right.put("t", "0");
right.put("u", "0");
right.put("v", "0");
right.put("w", "0");
right.put("x", "0");
right.put("y", "0");
right.put("z", "0");

for (char item : leftString.toCharArray()) {
left.put(Character.toString(item), Integer.toString(Integer.parseInt(left.get(Character.toString(item))) + 1));
System.out.println(Character.toString(item) + ":" + left.get(Character.toString(item)));
}

System.out.println("=====================================================");

for (char item : rightString.toCharArray()) {
right.put(Character.toString(item), Integer.toString(Integer.parseInt(right.get(Character.toString(item))) + 1));
System.out.println(Character.toString(item) + ":" + right.get(Character.toString(item)));
}

String list[] = {"a","b","c", "d", "e" , "f", "g", "h" , "i", "j", "k" , "l", "m", "n" , "o", "p", "q" , "r", "s", "t" , "u", "v", "w" , "x", "y", "z"};

for (String element : list) {
if (Integer.parseInt(left.get(element)) >= 1 && Integer.parseInt(right.get(element)) >= 1) {
anagramString += element;
} else if (Integer.parseInt(left.get(element)) >= 1 && Integer.parseInt(right.get(element)) == 0) {
leftRemoveString += element;
} else if (Integer.parseInt(left.get(element)) == 0 && Integer.parseInt(right.get(element)) >= 1) {
rightRemoveString += element;
}
}

System.out.println("Anagrams: " + anagramString);
System.out.println("Anagrams remove chars (left): " + leftRemoveString);
System.out.println("Anagrams remove char number (left): " + leftRemoveString.length());
System.out.println("Anagrams remove chars (right): " + rightRemoveString);
System.out.println("Anagrams remove char number (right): " + rightRemoveString.length());
System.out.println("Anagrams remove char number (total): " + ((leftRemoveString.length()) + (rightRemoveString.length())));

}
}
35 changes: 35 additions & 0 deletions Java/src/com/godepth/BFS/BFSNode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.godepth.BFS;

public class BFSNode<T> {
private T data = null;
private BFSNode<T> left, right;

public BFSNode(T item) {
data = item;
left = right = null;
}

public BFSNode<T> getLeft() {
return left;
}

public void setLeft(BFSNode<T> node) {
left = node;
}

public BFSNode<T> getRight() {
return right;
}

public void setRight(BFSNode<T> node) {
right = node;
}

public T getData() {
return data;
}

public void setData(T item) {
data = item;
}
}
41 changes: 41 additions & 0 deletions Java/src/com/godepth/BFS/BFSTree.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.godepth.BFS;

public class BFSTree<T> {
public BFSNode<T> root;

public BFSTree() {
root = null;
}

public void printLevelOrder() {
int height, i;
height = height(root);

for (i = 1; i <= height; i++) {
printGivenLevel(root, i);
}
}

private int height(BFSNode<T> root) {
if (root == null) {
return 0;
} else {
int leftHeight = height(root.getLeft());
int rightHeight = height(root.getRight());
return ((leftHeight > rightHeight ? leftHeight : rightHeight) + 1);
}
}

private void printGivenLevel(BFSNode<T> root, int level) {
if (root == null) {
return;
}

if (level == 1) {
System.out.print(root.getData() + " ");
} else if (level > 1) {
printGivenLevel(root.getLeft(), level - 1);
printGivenLevel(root.getRight(), level - 1);
}
}
}
29 changes: 29 additions & 0 deletions Java/src/com/godepth/BFS/QueueBFSTree.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.godepth.BFS;

import java.util.Queue;
import java.util.LinkedList;

public class QueueBFSTree<T> {

public BFSNode<T> root;

public void printLevelOrder()
{
Queue<BFSNode<T>> queue = new LinkedList<>();
queue.add(root);

while (!queue.isEmpty()) {

BFSNode<T> temp = queue.poll();
System.out.print(temp.getData());

if (temp.getLeft() != null) {
queue.add(temp.getLeft());
}

if (temp.getRight() != null) {
queue.add(temp.getRight());
}
}
}
}
35 changes: 35 additions & 0 deletions Java/src/com/godepth/DFS/DFSNode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.godepth.DFS;

public class DFSNode<T> {
private T data = null;
private DFSNode<T> left, right;

public DFSNode(T item) {
data = item;
left = right = null;
}

public DFSNode<T> getLeft() {
return left;
}

public void setLeft(DFSNode<T> node) {
left = node;
}

public DFSNode<T> getRight() {
return right;
}

public void setRight(DFSNode<T> node) {
right = node;
}

public T getData() {
return data;
}

public void setData(T item) {
data = item;
}
}
42 changes: 42 additions & 0 deletions Java/src/com/godepth/DFS/DFSTraversal.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.godepth.DFS;

public class DFSTraversal<T> {

public DFSNode<T> root;

public void printInorder(DFSNode<T> node)
{
if (node == null)
return;

printInorder(node.getLeft());

System.out.print(node.getData() + " ");

printInorder(node.getRight());
}

public void printPreorder(DFSNode<T> node)
{
if (node == null)
return;

System.out.print(node.getData() + " ");

printPreorder(node.getLeft());

printPreorder(node.getRight());
}

public void printPostorder(DFSNode<T> node)
{
if (node == null)
return;

printPostorder(node.getLeft());

printPostorder(node.getRight());

System.out.print(node.getData() + " ");
}
}
28 changes: 28 additions & 0 deletions Java/src/com/godepth/DFS/StackDSTTree.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.godepth.DFS;

import java.util.Stack;

public class StackDSTTree<T> {

public DFSNode<T> root;

public void printDepthOrder()
{
Stack<DFSNode<T>> stack = new Stack<>();
stack.add(root);

while (!stack.isEmpty()){

DFSNode<T> temp = stack.pop();
System.out.print(temp.getData());

if (temp.getLeft() != null) {
stack.push(temp.getLeft());
}

if (temp.getRight() != null) {
stack.push(temp.getRight());
}
}
}
}
33 changes: 33 additions & 0 deletions Java/src/com/godepth/DiagonalDifference/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.godepth.DiagonalDifference;

class AbsoluteDifference {

private int[][] square = {
{3},
{11, 2, 4},
{4, 5, 6},
{10, 8, -12}
};

public int count()
{
int length = square[0][0];
int i;
int left = 0;
int right = 0;

for (i = 0; i < length ; i++) {
left = left + square[i+1][i];
right = right + square[i+1][length-i-1];
}

return Math.abs(left - right);
}
}

public class Main {
public static void main(String[] args) {
AbsoluteDifference diff = new AbsoluteDifference();
System.out.print("Disgonal diff absolute value is " + diff.count());
}
}
Loading

0 comments on commit 02b87e9

Please sign in to comment.