0% found this document useful (0 votes)
1 views

Java_Helper_Functions_Bagrut

The document provides Java helper functions for working with Queue, Node, and BinNode classes, specifically for Integer data types. It includes methods for summing elements, checking sizes, filtering even numbers in queues, and various operations on linked lists and binary trees. Each method is accompanied by explanations and is designed to enhance the functionality of these data structures.

Uploaded by

noaam.farber
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Java_Helper_Functions_Bagrut

The document provides Java helper functions for working with Queue, Node, and BinNode classes, specifically for Integer data types. It includes methods for summing elements, checking sizes, filtering even numbers in queues, and various operations on linked lists and binary trees. Each method is accompanied by explanations and is designed to enhance the functionality of these data structures.

Uploaded by

noaam.farber
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

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 (!q.isEmpty()) {
int val = q.remove();
sum += val;
temp.insert(val);
}
while (!temp.isEmpty()) {
q.insert(temp.remove());
}
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 (!q.isEmpty()) {
count++;
int val = q.remove();
temp.insert(val);
}
while (!temp.isEmpty()) {
q.insert(temp.remove());
}
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 (!q.isEmpty()) {
int val = q.remove();
if (val % 2 != 0) {
allEven = false;
}
temp.insert(val);
}
while (!temp.isEmpty()) {
q.insert(temp.remove());
}
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 (!q.isEmpty()) {
int val = q.remove();
if (val % 2 == 0) {
result.insert(val);
}
temp.insert(val);
}
while (!temp.isEmpty()) {
q.insert(temp.remove());
}
return result;
}

// Returns true if queue is sorted in ascending order


public static boolean isSorted(Queue<Integer> q) {
if (q.isEmpty()) return true;
Queue<Integer> temp = new Queue<>();
int prev = q.remove();
temp.insert(prev);
boolean sorted = true;
while (!q.isEmpty()) {
int curr = q.remove();
if (prev > curr) sorted = false;
prev = curr;
temp.insert(curr);
}
while (!temp.isEmpty()) {
q.insert(temp.remove());
}
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 = head.getValue();
while (head != null) {
if (head.getValue() > max) {
max = head.getValue();
}
head = head.getNext();
}
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<>(head.getValue() * 2);
Node<Integer> temp = newHead;
head = head.getNext();
while (head != null) {
temp.setNext(new Node<>(head.getValue() * 2));
temp = temp.getNext();
head = head.getNext();
}
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 (root.getValue() == val) return true;
return contains(root.getLeft(), val) ||
contains(root.getRight(), val);
}
// Returns the number of even values in the tree
public static int countEven(BinNode<Integer> root) {
if (root == null) return 0;
int count = (root.getValue() % 2 == 0) ? 1 : 0;
return count + countEven(root.getLeft()) +
countEven(root.getRight());
}

// 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(root.getLeft());
int right = heightDiff(root.getRight());
if (left == -1 || right == -1 || Math.abs(left - right) > 1)
return -1;
return 1 + Math.max(left, right);
}
}

You might also like