Question
PROVIDED CODE: import java.util.*; import java.io.*; // This class represents a tree of integers. public class IntTree { private IntTreeNode overallRoot; public boolean hasZeroPath() {
PROVIDED CODE:
import java.util.*; import java.io.*;
// This class represents a tree of integers. public class IntTree { private IntTreeNode overallRoot;
public boolean hasZeroPath() { // TODO: Your code here }
////////////////////////////////////////////// // You can ignore code below // //////////////////////////////////////////////
// Constructs a tree with default numbers. public IntTree() { overallRoot = null; }
public IntTree(String s) { overallRoot = fromString(new StringBuilder(s.toLowerCase().trim())); }
public IntTreeNode getRoot() { return overallRoot; }
public void setRoot(IntTreeNode root) { overallRoot = root; }
// post: Prints the numbers in this tree in a pre-order fashion. public void print() { print(overallRoot); }
private void print(IntTreeNode root) { if (root != null) { System.out.print(root.data + " "); print(root.left); print(root.right); } }
public boolean equals(Object o) { if (o instanceof IntTree) { IntTree other = (IntTree) o; return toString().equals(other.toString()); } else { return false; } }
// post: Returns a text representation of the tree. public String toString() { return toString(overallRoot); }
private String toString(IntTreeNode root) { if (root == null) { return "null"; } else if (root.left == null && root.right == null) { return "[" + root.data + "]"; } else { return "[" + root.data + " " + toString(root.left) + " " + toString(root.right) + "]"; } }
private IntTreeNode fromString(StringBuilder s) { String next = nextToken(s); if (next.length() == 0 || next.equals("null") || next.equals("/")) { return null; } else { next = next.substring(1, next.length() - 1).trim(); // remove [] from ends StringBuilder nextBuilder = new StringBuilder(next); String rootStr = nextToken(nextBuilder); int data = Integer.parseInt(rootStr); String leftStr = nextToken(nextBuilder); String rightStr = nextToken(nextBuilder); return new IntTreeNode(data, fromString(new StringBuilder(leftStr)), fromString(new StringBuilder(rightStr))); } }
// Returns string representation of next complete node or data value from given buffer. private String nextToken(StringBuilder s) { while (s.indexOf(" ") == 0) { s.deleteCharAt(0); } if (s.length() == 0) { return ""; }
int i = 0; if (s.charAt(0) == '[' || s.charAt(0) == '(') { int depth = 0; do { if (s.charAt(i) == '[' || s.charAt(i) == '(') { depth++; } else if (s.charAt(i) == ']' || s.charAt(i) == ')') { depth--; } i++; } while (i 0); if (depth > 0) { throw new IllegalArgumentException("missing closing bracket in " + s); } } else { while (i
String result = s.substring(0, i).trim(); s.delete(0, i); while (s.indexOf(" ") == 0) { s.deleteCharAt(0); } return result; } }
The program should be written in Java, using only the provided methods. You may not import additional packages.
4. Binary Trees Write a method hasZeroPath that returns true if there is a path from the root of the tree to a leaf consisting only of elements with zero value. An empty tree is considered to have a O-length path and should therefore return true. Examples t.overallRoot | 0 / 7 0 t.overallRoot | 0 t.overallRoot | 0 / / 7 0 / / 3 / 3 7 7 3 0 1 21 hasZeroPath() returns true hasZeroPath() returns false hasZeroPath() returns true You are writing a method that will become part of the IntTree class. You may define private helper methods to solve this problem, but otherwise you may not call any other methods of the class. You may not construct any extra data structures to solve thisStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started