Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

When I compile my code using javac I got an error: binarytree.java:138: error: reached end of file while parsing } How to fix this? *binarytree.java

When I compile my code using javac I got an error:

binarytree.java:138: error: reached end of file while parsing }

How to fix this?

*binarytree.java package binarytree; import java.util.*; import java.util.function.Function; public class BinaryTree { private BTNode root; public BinaryTree() { root = null; } public BinaryTree(T data) { root = new BTNode(data); } public BinaryTree(T data, BinaryTree left, BinaryTree right) { root = new BTNode(data); if (left != null) root.setLeft(left.getRoot()); if (right != null) root.setRight(right.getRoot()); } public BTNode getRoot() { return root; } public void setRoot(BTNode root) { this.root = root; } public boolean isEmpty() { return root == null; } public int size() { return size(root); } private int size(BTNode node) { if (node == null) return 0; else return 1 + size(node.getLeft()) + size(node.getRight()); } public int height() { return height(root); } private int height(BTNode node) { if (node == null) return -1; else return 1 + Math.max(height(node.getLeft()), height(node.getRight())); } public int numberOfLeaves() { return numberOfLeaves(root); } private int numberOfLeaves(BTNode node) { if (node == null) return 0; else if (node.getLeft() == null && node.getRight() == null) return 1; else return numberOfLeaves(node.getLeft()) + numberOfLeaves(node.getRight()); } public int countDepthK(int k) { return countDepthK(root, k, 0); } private int countDepthK(BTNode node, int k, int depth) { if (node == null) return 0; else if (depth == k) return 1; else return countDepthK(node.getLeft(), k, depth + 1) + countDepthK(node.getRight(), k, depth + 1); } public void map(Function mapper) { map(root, mapper); } private void map(BTNode node, Function mapper) { if (node != null) { node.setData(mapper.apply(node.getData())); map(node.getLeft(), mapper); map(node.getRight(), mapper); } } public List> pathFromRoot(BTNode node) { if (node == null) throw new IllegalArgumentException("Node is null"); List> path = new ArrayList>(); pathFromRoot(root, node, path); if (path.isEmpty()) throw new IllegalArgumentException("Node is not in tree"); return path; } private boolean pathFromRoot(BTNode node, BTNode target, List> path) { if (node == null) return false; if (node.equals(target)) { path.add(node); return true; } boolean found = pathFromRoot(node.getLeft(), target, path) || pathFromRoot(node.getRight(), target, path); if (found) path.add(node); return found; } public int distance(BTNode node1, BTNode node2) { if (node1 == null || node2 == null) throw new IllegalArgumentException("Node is not in the tree"); List> pathToNode1 = pathFromRoot(node1); List> pathToNode2 = pathFromRoot(node2); if (pathToNode1.isEmpty() || pathToNode2.isEmpty()) throw new IllegalArgumentException("Node is not in the tree"); int i; for (i = 0; i < pathToNode1.size() && i < pathToNode2.size(); i++) { if (pathToNode1.get(i) != pathToNode2.get(i)) break; } return (pathToNode1.size() - i) + (pathToNode2.size() - i); } *BTNode.java package binarytree; public class BTNode { private T data; private BTNode left; private BTNode right; public BTNode(T data, BTNode left, BTNode right) { this.data = data; this.left = left; this.right = right; } public T getData() { return data; } public void setData(T data) { this.data = data; } public BTNode getLeft() { return left; } public void setLeft(BTNode left) { this.left = left; } public BTNode getRight() { return right; } public void setRight(BTNode right) { this.right = right; } public boolean isLeaf() { return left == null && right == null; } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Financial management theory and practice

Authors: Eugene F. Brigham and Michael C. Ehrhardt

12th Edition

978-0030243998, 30243998, 324422695, 978-0324422696

Students also viewed these Programming questions