Java Helper Functions for 5-Unit Bagrut
כולל פעולות שימושיות למחלקותQueue, Node ו-BinNode. כל הפעולות כתובות עם
Integer ומלוות בהסברים בקוד.
public class HelperFunctions {
// ================================
// Queue<Integer> methods
// ================================
// Returns the sum of all elements in the queue (doesn't destroy
the queue)
public static int sum(Queue<Integer> q) {
int sum = 0;
Queue<Integer> temp = new Queue<>();
while (![Link]()) {
int val = [Link]();
sum += val;
[Link](val);
}
while (![Link]()) {
[Link]([Link]());
}
return sum;
}
// Returns the number of elements in the queue
public static int size(Queue<Integer> q) {
int count = 0;
Queue<Integer> temp = new Queue<>();
while (![Link]()) {
count++;
int val = [Link]();
[Link](val);
}
while (![Link]()) {
[Link]([Link]());
}
return count;
}
// Returns true if all values in the queue are even
public static boolean allEven(Queue<Integer> q) {
boolean allEven = true;
Queue<Integer> temp = new Queue<>();
while (![Link]()) {
int val = [Link]();
if (val % 2 != 0) {
allEven = false;
}
[Link](val);
}
while (![Link]()) {
[Link]([Link]());
}
return allEven;
}
// Returns a queue with only even numbers
public static Queue<Integer> filterEven(Queue<Integer> q) {
Queue<Integer> result = new Queue<>();
Queue<Integer> temp = new Queue<>();
while (![Link]()) {
int val = [Link]();
if (val % 2 == 0) {
[Link](val);
}
[Link](val);
}
while (![Link]()) {
[Link]([Link]());
}
return result;
}
// Returns true if queue is sorted in ascending order
public static boolean isSorted(Queue<Integer> q) {
if ([Link]()) return true;
Queue<Integer> temp = new Queue<>();
int prev = [Link]();
[Link](prev);
boolean sorted = true;
while (![Link]()) {
int curr = [Link]();
if (prev > curr) sorted = false;
prev = curr;
[Link](curr);
}
while (![Link]()) {
[Link]([Link]());
}
return sorted;
}
// ================================
// Node<Integer> (Linked List) methods
// ================================
// Returns the maximum value in the list
public static int maxValue(Node<Integer> head) {
if (head == null) throw new RuntimeException("Empty list");
int max = [Link]();
while (head != null) {
if ([Link]() > max) {
max = [Link]();
}
head = [Link]();
}
return max;
}
// Returns a new list with doubled values
public static Node<Integer> doubleValues(Node<Integer> head) {
if (head == null) return null;
Node<Integer> newHead = new Node<>([Link]() * 2);
Node<Integer> temp = newHead;
head = [Link]();
while (head != null) {
[Link](new Node<>([Link]() * 2));
temp = [Link]();
head = [Link]();
}
return newHead;
}
// ================================
// BinNode<Integer> (Binary Tree) methods
// ================================
// Returns true if tree contains a certain value
public static boolean contains(BinNode<Integer> root, int val) {
if (root == null) return false;
if ([Link]() == val) return true;
return contains([Link](), val) ||
contains([Link](), val);
}
// Returns the number of even values in the tree
public static int countEven(BinNode<Integer> root) {
if (root == null) return 0;
int count = ([Link]() % 2 == 0) ? 1 : 0;
return count + countEven([Link]()) +
countEven([Link]());
}
// Checks if tree is balanced in terms of height difference
public static boolean isBalanced(BinNode<Integer> root) {
return heightDiff(root) != -1;
}
private static int heightDiff(BinNode<Integer> root) {
if (root == null) return 0;
int left = heightDiff([Link]());
int right = heightDiff([Link]());
if (left == -1 || right == -1 || [Link](left - right) > 1)
return -1;
return 1 + [Link](left, right);
}
}