// Complete the THREE exercises below. For each \"EXERCISE\" comment, add code i
ID: 666177 • Letter: #
Question
// Complete the THREE exercises below. For each "EXERCISE" comment, add code immediately below the comment.
//
// 2. 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 1: The method "size" computes the number of nodes in the tree.
// The size of "null" must be 0.
public static int size (Node t) {
// TODO: Complete this method.
return -1;
}
//////////////////////////////////////////////////////////////////////////////////////
// Recall the definitions of height and depth from p226 of textbook:
//
// The depth of a node in a tree is the number of links on the path from it to the
root.
//
// The height of a tree is the maximum depth among its nodes.
//
// In the BST NodeSamples.t01,
// - node 41 has depth 0, height 2
// - node 21 has depth 1, height 1
// - node 61 has depth 1, height 0
// - node 11 has depth 2, height 0
// - node 31 has depth 2, height 0
// height of the whole tree is the height of the root
// EXERCISE 2: The method "height" computes the height of the tree.
// The height of "null" must be 0.
public static int height (Node t) {
// TODO: Complete this method.
return -1;
}
//////////////////////////////////////////////////////////////////////////////////////
// EXERCISE 3: The method "sizeOdd" computes the number of nodes with odd keys.
// The sizeOdd of "null" must be 0.
public static int sizeOdd (Node t) {
// TODO: Complete this method.
return -1;
}
Explanation / Answer
comments too added
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));
}
return null;
}
//////////////////////////////////////////////////////////////////////////////////////
// EXERCISE 1: The method "size" computes the number of nodes in the tree.
// The size of "null" must be 0.
public static int size (Node t) {
// TODO: Complete this method.
if (t == null) return 0;
// count both left and right child and including
return 1 + size(t.left) + size(t.right);
}
//////////////////////////////////////////////////////////////////////////////////////
// Recall the definitions of height and depth from p226 of textbook:
//
// The depth of a node in a tree is the number of links on the path from it to the root.
//
// The height of a tree is the maximum depth among its nodes.
//
// In the BST NodeSamples.t01,
// - node 41 has depth 0, height 2
// - node 21 has depth 1, height 1
// - node 61 has depth 1, height 0
// - node 11 has depth 2, height 0
// - node 31 has depth 2, height 0
// height of the whole tree is the height of the root
// EXERCISE 2: The method "height" computes the height of the tree.
// The height of "null" must be 0.
public static int height (Node t) {
// TODO: Complete this method.
if(t == null)
return -1;
//getting both subtrees height
int lefth = height(t.left);
int righth = height(t.right);
if(lefth > righth)
return lefth + 1;
else
return righth +1;
}
//////////////////////////////////////////////////////////////////////////////////////
// EXERCISE 3: The method "sizeOdd" computes the number of nodes with odd keys.
// The sizeOdd of "null" must be 0.
public static int sizeOdd (Node t) {
// TODO: Complete this method.
//comapring odd values
if(t==null){
return 0;
}
if ((Integer.parseInt(t.key)%2 == 1)){
return 1+ sizeOdd(t.left) + sizeOdd(t.right);
}
return 0;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.