// Complete the THREE exercises below. For each \"EXERCISE\" comment, add code i
ID: 666190 • Letter: #
Question
// Complete the THREE exercises below. For each "EXERCISE" comment, add code immediately below the comment.
//You MUST NOT edit the IDEA/SBT configuration/tests. Altering it in your submission will result in 0 points for this assignment.
//
// This class represents operations on simple Binary Search Trees (BST) with integer keys and no values, i.e., a set of integers.
//
// DO NOT change the name or type of any function (the first line of the function)
// All of the methods in this class are static (and mostly recursive).
// These methods could naturally appear inside Node.java also.
//
// This code is not representative of the object-oriented style of programming, but is concise and allows null to be used to represent leaf nodes.
public class NodeOps {
public static String toString (Node t) {
if (t == null) {
return "null";
} else {
return "new Node (" + t.key + ", " + toString (t.left) + ", " + toString
(t.right) + ")";
}
}
public static String toStringIndent (Node t, String indent) {
if (t == null) {
return indent + "null";
} else {
String indent2 = indent + " ";
return indent + "new Node ( " + indent2 + t.key + ", " + toStringIndent
(t.left, indent2) + ", " + toStringIndent (t.right, indent2) + " " + indent + ")";
}
}
public static Node copy (Node n) {
if (n == null) {
return null;
} else {
return new Node (n.key, copy (n.left), copy (n.right));
}
}
//////////////////////////////////////////////////////////////////////////////////////
// EXERCISE 7: A tree is size-perfect if for every node, size of left == size of
right.
public static boolean isPerfectlyBalancedS (Node t) {
// TODO: Complete this method.
return false;
}
//////////////////////////////////////////////////////////////////////////////////////
// EXERCISE 8: A tree is height-perfect if for every node, height of left == height
of right.
public static boolean isPerfectlyBalancedH (Node t) {
// TODO: Complete this method.
return false;
}
//////////////////////////////////////////////////////////////////////////////////////
// EXERCISE 9: A tree is semi-perfect if every node is semi-perfect.
// A node with 0 children is semi-perfect.
// A node with 1 child is NOT semi-perfect.
// A node with 2 children is semi-perfect if (size-of-larger-child <= size-of-
smaller-child * 3)
public static boolean isSemiBalanced (Node t) {
// TODO: Complete this method.
return false;
}
Explanation / Answer
Ans:
public int sizeAtDepth(int k) { return sizeAtDepth(k, root, 0); }
private int sizeAtDepth(int k, Node x, int depth){
if (x == null) return 0;
return sizeAtDepth(k, x.right, depth + 1) + sizeAtDepth(k, x.left, depth + 1) + (k == depth ? 1 : 0);
}
// the number of nodes in the tree, above depth k (not including k)
public int sizeAboveDepth(int k) { return sizeAboveDepth(k, root, 0); }
private int sizeAboveDepth(int k, Node x, int depth){
if (x == null) return 0;
return sizeAboveDepth(k, x.left, depth + 1) + sizeAboveDepth(k, x.right, depth + 1) + (k > depth ? 1 : 0);
}
// the number of nodes in the tree, below depth k (not including k)
public int sizeBelowDepth(int k) { return sizeBelowDepth(k, root, 0); }
private int sizeBelowDepth(int k, Node x, int depth){
if (x == null) return 0;
return sizeBelowDepth(k, x.left, depth + 1) + sizeBelowDepth(k, x.right, depth + 1) + (k < depth ? 1 : 0);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.