diff --git a/src/CamScanner 02-17-2025 17.15.pdf b/src/CamScanner 02-17-2025 17.15.pdf new file mode 100644 index 0000000..6d5d135 Binary files /dev/null and b/src/CamScanner 02-17-2025 17.15.pdf differ diff --git a/src/H20.java b/src/H20.java new file mode 100644 index 0000000..b7db758 --- /dev/null +++ b/src/H20.java @@ -0,0 +1,208 @@ +import java.util.ArrayList; +import java.util.Scanner; + +public class H20 extends HelpCommands { + + public static void wideScan(BinNode root) { + Queue> qTree = new Queue<>(); + qTree.insert(root); + while (!qTree.isEmpty()) { + while (qTree.head() == null) { + qTree.remove(); + if (qTree.isEmpty()) { + return; + } + } + System.out.println(qTree.head().getValue()); + qTree.insert(qTree.head().getLeft()); + qTree.insert(qTree.head().getRight()); + qTree.remove(); + } + } + + public static boolean hasOneSon(BinNode root) { + return root.hasRight() && !root.hasLeft() || root.hasLeft() && !root.hasRight(); + + } + + public static void ex40(BinNode root) { + if (root == null) { + return; + } + + Queue> queue = new Queue<>(); + queue.insert(root); + int level = 0; + + while (!queue.isEmpty()) { + int size = lengthQueue(queue); + ArrayList currentLevel = new ArrayList<>(); + + for (int i = 0; i < size; i++) { + BinNode node = queue.remove(); + + if (level % 2 == 0) { + currentLevel.add(node.getValue()); + } + + if (node.hasRight()) { + queue.insert(node.getRight()); + } + if (node.hasLeft()) { + queue.insert(node.getLeft()); + } + } + + if (!currentLevel.isEmpty()) { + for (T val : currentLevel) { + System.out.print(val + " "); + } + System.out.println(); + } + + level++; + } + } + + public static int getHeightOfValue(BinNode root, T value) { + if (root == null) { + return -1; + } + if (root.getValue() == value) { + return hightTree(root); + } + return Math.max(getHeightOfValue(root.getLeft(), value), getHeightOfValue(root.getRight(), value)); + } + + public static int ex42(BinNode root, int val1, int val2) { + return Math.abs(getHeightOfValue(root, val1) - getHeightOfValue(root, val2)); + } + + public static void ex43(BinNode root) { + //השאלה לא מובנת, אנחנו בכל מקרה עושים סריקה לעומק ללא שימוש ברקורסיה, לא מוסבר מה זה ביקור לעומק ומה זה סריקה תחילית + + + } + + + public static void printLeftToRight(BinNode root) { + if (root == null) { + return; + } + printLeftToRight(root.getLeft()); + System.out.println(root.getValue()); + printLeftToRight(root.getRight()); + } + + + public static Scanner input = new Scanner(System.in); + + public static void AddToTree(int n, BinNode root) { + if (root == null) + return; + if (n > root.getValue() && root.hasRight()) + AddToTree(n, root.getRight()); + else if (n > root.getValue() && !root.hasRight()) { + root.setRight(new BinNode<>(n)); + } else if (n < root.getValue() && root.hasLeft()) { + AddToTree(n, root.getLeft()); + } else if (n < root.getValue() && !root.hasLeft()) { + root.setLeft(new BinNode<>(n)); + } + + } + + public static BinNode BuildSearchTree() { // q45 p219 + System.out.println("insert a number"); + int x = input.nextInt(); + + BinNode root = new BinNode<>(x); + while (x != -999) { + + System.out.println("insert a number"); + x = input.nextInt(); + if (x != -999) { + AddToTree(x, root); + } + } + return root; + } + + public static boolean IsItThere(Node node, int n) { //o(n) + Node fst = node; + while (fst != null) { + if (fst.getValue() == n) + return true; + fst = fst.getNext(); + } + return false; + } + + public static int HowManyNodes(Node node) { //o(n) + Node fst = node; + int c = 0; + while (fst != null) { + c++; + fst = fst.getNext(); + } + return c; + } + + public static int HowManyInTree(BinNode root) { + if (root == null) + return 0; + return 1 + HowManyInTree(root.getLeft()) + HowManyInTree(root.getRight()); + + } + + public static Node FromTreeToList(BinNode root) { // q46 + Node ret = new Node<>(Integer.MIN_VALUE); + Node temp = ret; + int x = HowManyInTree(root); + while (x != HowManyNodes(temp)) { + while (root.getValue() > temp.getValue() && root.hasLeft()) { + root = root.getLeft(); + } + temp.setNext(new Node<>(root.getValue())); + temp = temp.getNext(); + } + return ret.getNext(); + } + + public static boolean IsItSorted(BinNode root) { // q47 + if (root == null) + return true; + if (root.hasLeft() && root.hasRight()) { + if (root.getValue() < root.getLeft().getValue() || root.getValue() > root.getRight().getValue()) + return false; + } else if (root.hasRight() && !root.hasLeft()) { + if (root.getValue() > root.getRight().getValue()) + return false; + } else if (root.hasLeft() && !root.hasRight()) { + if (root.getValue() < root.getLeft().getValue()) + return false; + } + return IsItSorted(root.getLeft()) && IsItSorted(root.getRight()); + } + + public static void printNodes(Node node) { // f + System.out.print("["); + while (node != null) { + System.out.print(node); + node = node.getNext(); + if (node != null) { + System.out.print(", "); + } + } + System.out.print("]"); + System.out.println(); + } + + + public static void main(String[] args) { + BinNode root = buildTree(5); + printMeTree(root); + ex40(root); + } + +} diff --git a/src/HelpCommands.java b/src/HelpCommands.java index 9350cd2..8b253aa 100644 --- a/src/HelpCommands.java +++ b/src/HelpCommands.java @@ -287,9 +287,9 @@ public static Queue buildQueue(int n, int max) { * @param queue * @return the length of the queue */ - public static int lengthQueue(Queue queue) { + public static int lengthQueue(Queue queue) { int len = 0; - Queue temp = new Queue<>(); + Queue temp = new Queue<>(); while (!queue.isEmpty()) { temp.insert(queue.remove()); len++; @@ -845,16 +845,14 @@ public static String[][] printMat(BinNode root) { } - public static boolean isLeaf(BinNode root) { // returns true if the node is a leaf - if (root == null) - return false; - if (root.getLeft() == null && root.getRight() == null) - return true; - else + public static boolean isLeaf(BinNode root) { // returns true if the node is a leaf + if (root == null) { return false; + } + return root.getLeft() == null && root.getRight() == null; } - public static int hightTree(BinNode root) { + public static int hightTree(BinNode root) { if (root == null) return 0; if (isLeaf(root))