Question
PLEASE HELP ME WITH THIS HW!! //some useful codes!! // Simple binary tree class that includes methods to construct a // tree of ints, to
PLEASE HELP ME WITH THIS HW!!
//some useful codes!!
// Simple binary tree class that includes methods to construct a
// tree of ints, to print the structure, and to print the data
// using a preorder, inorder or postorder traversal. The trees
// built have nodes numbered starting with 1 and numbered
// sequentially level by level with no gaps in the tree. The
// documentation refers to these as "sequential trees."
//
// from buildingjavaprograms.com
import java.util.*;
public class IntTree {
private IntTreeNode overallRoot;
// pre : max > 0
// post: constructs a sequential tree with given number of
// nodes
public IntTree(int max) {
if (max
throw new IllegalArgumentException("max: " + max);
}
overallRoot = buildTree(1, max);
}
public IntTree() {
overallRoot = null;
}
// constructor added so we can build page 1029 reference trees
public IntTree(IntTreeNode given) {
overallRoot = given;
}
//Exercise #7, Chapter 17
public boolean isFull() {
return (overallRoot == null || isFull(overallRoot));
}
private boolean isFull(IntTreeNode root) {
if(root.left == null && root.right == null) {
return true;
} else {
return (root.left != null && root.right != null && isFull(root.left) && isFull(root.right));
}
}
// post: returns a sequential tree with n as its root unless
// n is greater than max, in which case it returns an
// empty tree
private IntTreeNode buildTree(int n, int max) {
if (n > max) {
return null;
} else {
return new IntTreeNode(n, buildTree(2 * n, max),
buildTree(2 * n + 1, max));
}
}
// post: prints the tree contents using a preorder traversal
public void printPreorder() {
System.out.print("preorder:");
printPreorder(overallRoot);
System.out.println();
}
// post: prints the tree contents using a preorder traversal
// post: prints in preorder the tree with given root
private void printPreorder(IntTreeNode root) {
if (root != null) {
System.out.print(" " + root.data);
printPreorder(root.left);
printPreorder(root.right);
}
}
// post: prints the tree contents using a inorder traversal
public void printInorder() {
System.out.print("inorder:");
printInorder(overallRoot);
System.out.println();
}
// post: prints in inorder the tree with given root
private void printInorder(IntTreeNode root) {
if (root != null) {
printInorder(root.left);
System.out.print(" " + root.data);
printInorder(root.right);
}
}
// post: prints the tree contents using a postorder traversal
public void printPostorder() {
System.out.print("postorder:");
printPostorder(overallRoot);
System.out.println();
}
// post: prints in postorder the tree with given root
private void printPostorder(IntTreeNode root) {
if (root != null) {
printPostorder(root.left);
printPostorder(root.right);
System.out.print(" " + root.data);
}
}
// post: prints the tree contents, one per line, following an
// inorder traversal and using indentation to indicate
// node depth; prints right to left so that it looks
// correct when the output is rotated.
public void printSideways() {
printSideways(overallRoot, 0);
}
// post: prints in reversed preorder the tree with given
// root, indenting each line to the given level
private void printSideways(IntTreeNode root, int level) {
if (root != null) {
printSideways(root.right, level + 1);
for (int i = 0; i
System.out.print(" ");
}
System.out.println(root.data);
printSideways(root.left, level + 1);
}
}
}
// Class for storing a single node of a binary tree of ints
// from buildingjavaprograms.com
public class IntTreeNode {
public int data;
public IntTreeNode left;
public IntTreeNode right;
// constructs a leaf node with given data
public IntTreeNode(int data) {
this(data, null, null);
}
// constructs a branch node with given data, left subtree,
// right subtree
public IntTreeNode(int data, IntTreeNode left,
IntTreeNode right) {
this.data = data;
this.left = left;
this.right = right;
}
}
//AND LASTLY THE TEST CODE TO MAKE SURE IT'S RIGHT
// Code to post with Quiz17 IntTree tree = new IntTree(new IntTreeNode(9,new IntTreeNode(8),new IntTreeNode(7))); tree.printSideways(); tree.slightStutter(); System.out.println("---------------------------------"); tree.printSideways();Submit IntTree.java with the following methods added: A. slightStutter); a public non-recursive method that changes this tree to add stutter nodes on the left when possible, otherwise add a stutter node on the right, but if both left and right are not null then the node cannot be stuttered. See example below. slightStutter(IntTreeNode root); a private recursive method helper, returns an IntTreeNode, that does the tree traversal and modification for public slightStutter You can either use your existing IntTree from your homework exercises (assuming you did your TODO?). Or you can start from scratch with this file that you should have started with days ago: IntTree,javawill also use IntTreeNode.javaas is, no changes. Further explanation (probably needed): stutter) has been done in almost every chapter, so let's do it again. Each node has a copy added to the left, or to the right, or not at all, thus we don't exactly double the size. A and B are really one problem if you've followed along how these tree exercises go, the client calls a public method that calls a recursive helper. Hint: The way I did this one was to draw from Exercise #12 (removeLeavesO) that I did in the homework, and realized that slightStutter) is similar in logic, but is just adding leaves as the stutter nodes. In removeLeaves) I have just two cases in an if-block, and in my slightStutter) method, I needed three cases (left,right,none) in a similar if-block. Once l saw that, I was done in 5 minutes. For example, here's some starter code I'm using that might help: Submit IntTree.java with the following methods added: A. slightStutter); a public non-recursive method that changes this tree to add stutter nodes on the left when possible, otherwise add a stutter node on the right, but if both left and right are not null then the node cannot be stuttered. See example below. slightStutter(IntTreeNode root); a private recursive method helper, returns an IntTreeNode, that does the tree traversal and modification for public slightStutter You can either use your existing IntTree from your homework exercises (assuming you did your TODO?). Or you can start from scratch with this file that you should have started with days ago: IntTree,javawill also use IntTreeNode.javaas is, no changes. Further explanation (probably needed): stutter) has been done in almost every chapter, so let's do it again. Each node has a copy added to the left, or to the right, or not at all, thus we don't exactly double the size. A and B are really one problem if you've followed along how these tree exercises go, the client calls a public method that calls a recursive helper. Hint: The way I did this one was to draw from Exercise #12 (removeLeavesO) that I did in the homework, and realized that slightStutter) is similar in logic, but is just adding leaves as the stutter nodes. In removeLeaves) I have just two cases in an if-block, and in my slightStutter) method, I needed three cases (left,right,none) in a similar if-block. Once l saw that, I was done in 5 minutes. For example, here's some starter code I'm using that might help
Step 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