Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 19 additions & 12 deletions src/HelpCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -731,11 +731,11 @@ public static int[][] arrowMat(BinNode<Integer> root) {
BinNode<Integer> hz = copyCnstr(root);
BinNode<Integer> vr = copyCnstr(root);

hz.setValue((int) (Math.pow(2, hightTree(hz) - 1)));
hz.setValue((int) (Math.pow(2, HelpCommands.getTreeHeight(hz) - 1)));
vr.setValue(0);

int[][] arr = arrowMat(root, indexVr(vr), indexHz(hz),
new int[hightTree(root)][(int) (Math.pow(2, hightTree(root))) + 1]);
new int[HelpCommands.getTreeHeight(root)][(int) (Math.pow(2, HelpCommands.getTreeHeight(root))) + 1]);

for (int i = 1; i < arr.length; i++) {

Expand Down Expand Up @@ -837,11 +837,11 @@ public static String[][] printMat(BinNode<Integer> root) {
BinNode<Integer> hz = copyCnstr(root);
BinNode<Integer> vr = copyCnstr(root);

hz.setValue((int) (Math.pow(2, hightTree(hz) - 1)));
hz.setValue((int) (Math.pow(2, HelpCommands.getTreeHeight(hz) - 1)));
vr.setValue(0);

return printMat(root, indexVr(vr), indexHz(hz),
new String[hightTree(root)][(int) (Math.pow(2, hightTree(root))) + 1]);
new String[HelpCommands.getTreeHeight(root)][(int) (Math.pow(2, HelpCommands.getTreeHeight(root))) + 1]);

}

Expand All @@ -854,13 +854,11 @@ public static boolean isLeaf(BinNode<Integer> root) { // returns true if the nod
return false;
}

public static int hightTree(BinNode<Integer> root) {
if (root == null)
public static <T> int getTreeHeight(BinNode<T> root) {
if (root == null) {
return 0;
if (isLeaf(root))
return 1;
else
return (1 + Math.max(hightTree(root.getLeft()), hightTree(root.getRight())));
}
return 1 + Math.max(getTreeHeight(root.getRight()), getTreeHeight(root.getLeft()));
}

public static int nodeLevel(BinNode<Integer> t, int lev, int num) {
Expand All @@ -884,9 +882,9 @@ public static BinNode<Integer> indexHz(BinNode<Integer> index) {
if (index != null) {

if (index.getLeft() != null)
index.getLeft().setValue(index.getValue() - ((int) (Math.pow(2, hightTree(index) - 2))));
index.getLeft().setValue(index.getValue() - ((int) (Math.pow(2, HelpCommands.getTreeHeight(index) - 2))));
if (index.getRight() != null)
index.getRight().setValue(index.getValue() + ((int) (Math.pow(2, hightTree(index) - 2))));
index.getRight().setValue(index.getValue() + ((int) (Math.pow(2, HelpCommands.getTreeHeight(index) - 2))));

indexHz(index.getLeft());
indexHz(index.getRight());
Expand Down Expand Up @@ -958,5 +956,14 @@ public static void inOrder(BinNode<Integer> root) {
}
}

public static <T> boolean hasValue(BinNode<T> root, T value) {
if (root == null) {
return false;
}
if (root.getValue() == value) {
return true;
}
return hasValue(root.getLeft(), value) || hasValue(root.getRight(), value);
}

}
54 changes: 54 additions & 0 deletions src/L19.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import java.util.Objects;

public class L19 extends HelpCommands {

public static String eraseFirst(String str) {
return str.substring(1);
}

public static boolean wordFromRoot(BinNode<Character> tree, String str) {
if (Objects.equals(str, "")) {
return true;
}
if (tree == null) {
return false;
}
if (tree.getValue() == str.charAt(0)) {
return wordFromRoot(tree.getLeft(), eraseFirst(str)) || wordFromRoot(tree.getRight(), eraseFirst(str));
}
return wordFromRoot(tree.getLeft(), str) || wordFromRoot(tree.getRight(), str);
}

public static boolean isPrime(int n) {
if (n <= 1) {
return false;
}
for (int i = 2; i < n; i++) {
if (n % i == 0) {
return false;
}
}
return true;
}

public static boolean addNodes(BinNode<Integer> leaf) {
if (isPrime(leaf.getValue())) {
return false;
} else {
for (int i = 2; i <= leaf.getValue() / 2; i++) {
if (leaf.getValue() % i == 0) {
leaf.setRight(new BinNode<>(leaf.getValue() / i));
leaf.setLeft(new BinNode<>(i));
}
}
}
return true;
}

public static void main(String[] args) {
BinNode<Integer> leaf = new BinNode<>(5);
addNodes(leaf);
printMeTree(leaf);
}

}
2 changes: 2 additions & 0 deletions src/NumShow.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
public record NumShow(int num, int numOfAppears) {
}