Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a static method of the BTNode class to meet the following specification Bt

ID: 3599494 • Letter: W

Question

Write a static method of the BTNode class to meet the following specification

BtNode has instance variables data, left, right

a) public static void increase(BTNode<Integer>root)

precondition: root is the root reference of a binary tree

postcondition: every node of the tree has had its data increased by one

b) public static void int count42(BtNode<Integer>root)

precondition: root is the root reference of a binary tree

postcondition: the return value indicates how many times 42 appears in the tree. If the tree is empty return zero.

Explanation / Answer

public class BTNode { private T data; private BTNode left; private BTNode right; public static void increase(BTNoderoot) { if(root != null) { increase(root.left); root.data += 1; increase(root.right); } } public static int count42(BTNoderoot) { if(root == null) { return 0; } else { return count42(root.left) + (root.data == 42 ? 1 : 0) + count42(root.right); } } }
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote