diff --git a/src/HelpCommands.java b/src/HelpCommands.java index 9350cd2..74217b6 100644 --- a/src/HelpCommands.java +++ b/src/HelpCommands.java @@ -731,11 +731,11 @@ public static int[][] arrowMat(BinNode root) { BinNode hz = copyCnstr(root); BinNode 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++) { @@ -837,11 +837,11 @@ public static String[][] printMat(BinNode root) { BinNode hz = copyCnstr(root); BinNode 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]); } @@ -854,13 +854,11 @@ public static boolean isLeaf(BinNode root) { // returns true if the nod return false; } - public static int hightTree(BinNode root) { - if (root == null) + public static int getTreeHeight(BinNode 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 t, int lev, int num) { @@ -884,9 +882,9 @@ public static BinNode indexHz(BinNode 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()); @@ -958,5 +956,14 @@ public static void inOrder(BinNode root) { } } + public static boolean hasValue(BinNode root, T value) { + if (root == null) { + return false; + } + if (root.getValue() == value) { + return true; + } + return hasValue(root.getLeft(), value) || hasValue(root.getRight(), value); + } } \ No newline at end of file diff --git a/src/L19.java b/src/L19.java new file mode 100644 index 0000000..9a189ac --- /dev/null +++ b/src/L19.java @@ -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 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 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 leaf = new BinNode<>(5); + addNodes(leaf); + printMeTree(leaf); + } + +} diff --git a/src/NumShow.java b/src/NumShow.java new file mode 100644 index 0000000..5cb936d --- /dev/null +++ b/src/NumShow.java @@ -0,0 +1,2 @@ +public record NumShow(int num, int numOfAppears) { +}