Question: **JAVA** How would I count the total number of Nodes in a Binary Tree using one reduce call? TreeFunctions.java : public class TreeFunctions { /*
**JAVA** How would I count the total number of Nodes in a Binary Tree using one reduce call?
TreeFunctions.java :
public class TreeFunctions { /* Count the total number of nodes */ static int countNodes (BinTree t) { //ANSWER HERE } public static void main (String[] args) { BinTree t123, t456, t7; t123 = new Node<>(1, new Leaf<>(2), new Leaf<>(3)); t456 = new Node<>(4, new Leaf<>(5), new Leaf<>(6)); t7 = new Node<>(7, t123, t456); System.out.printf("t7 has %d nodes%n", countNodes(t7)); System.out.printf("t7 has %d leaves%n", countLeaves(t7)); System.out.printf("t7 has %d internal nodes%n", countInternalNodes(t7)); System.out.printf("t7 has height %d%n", height(t7)); System.out.printf("is t7 balanced? %b%n", isBalanced(t7)); System.out.printf("preorder traversal of t7: %s%n", preorder(t7)); System.out.printf("inorder traversal of t7: %s%n", inorder(t7)); System.out.printf("postorder traversal of t7: %s%n", postorder(t7)); BinTree,Integer> exp1, exp2, exp3; exp1 = new Node<>((a,b) -> a+b, new Leaf<>(2), new Leaf<>(3)); exp2 = new Node<>((a,b) -> a-b, new Leaf<>(5), new Leaf<>(6)); exp3 = new Node<>(Math::max, exp1, exp2); System.out.printf("evaluating exp1 => %d%n", eval(exp1)); System.out.printf("evaluating exp2 => %d%n", eval(exp2)); System.out.printf("evaluating exp3 => %d%n", eval(exp3)); int sum = t7.reduce(n -> n, (n,r1,r2) -> n+r1+r2); }
Here is the Tree.java file that is used for this:
import java.util.function.Function; @FunctionalInterface interface TriFunction { R apply(A a, B b, C c); } class WrongTreeE extends Exception { } abstract class BinTree { abstract boolean isLeaf(); abstract L getLeafData() throws WrongTreeE; abstract N getNodeData() throws WrongTreeE; abstract BinTree getLeft() throws WrongTreeE; abstract BinTree getRight() throws WrongTreeE; abstract BinTree map(Function fnode, Function fleaf); abstract R reduce(Function base, TriFunction f); } //----------------------------------------------------------------------- class Leaf extends BinTree { private L data; Leaf(L data) { this.data = data; } boolean isLeaf() { return true; } L getLeafData() { return data; } N getNodeData() throws WrongTreeE { throw new WrongTreeE(); } BinTree getLeft() throws WrongTreeE { throw new WrongTreeE(); } BinTree getRight() throws WrongTreeE { throw new WrongTreeE(); } BinTree map(Function fnode, Function fleaf) { return new Leaf<>(fleaf.apply(data)); } R reduce(Function base, TriFunction f) { return base.apply(data); } } //----------------------------------------------------------------------- class Node extends BinTree { private N data; private BinTree left, right; Node(N data, BinTree left, BinTree right) { this.data = data; this.left = left; this.right = right; } boolean isLeaf() { return false; } L getLeafData() throws WrongTreeE { throw new WrongTreeE(); } N getNodeData() { return data; } BinTree getLeft() { return left; } BinTree getRight() { return right; } BinTree map(Function fnode, Function fleaf) { return new Node<>(fnode.apply(data), left.map(fnode,fleaf), right.map(fnode,fleaf)); } R reduce(Function base, TriFunction f) { return f.apply(data, left.reduce(base,f), right.reduce(base,f)); } } //----------------------------------------------------------------------- //----------------------------------------------------------------------- Any explanation on how to use reduce method to find the answer would be helpful.
As you can see in the last line of the main method of TreeFunctions.java, that is how you would find the total number of nodes in the main method. But I'm confused how you would do it in its own function (the countNodes function).
Thanks!
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
