Question
how to do these two? File: TreePrinter.java // TreePrinter class implementation import java.util.*; public class TreePrinter { // Node class (inner class) implementation private class
how to do these two?
File: TreePrinter.java
// TreePrinter class implementation import java.util.*; public class TreePrinter { // Node class (inner class) implementation private class Node { // instance variables for the Node class private int value; private Node left; private Node right;
// constructor implementation public Node(int val) { value = val; left = null; right = null; } // end of constructor } // end of inner class // instance variables for the TreePrinter class private Node root;
// default constructor implementation public TreePrinter() { root = null; } // end of default constructor // add method implementation public void add(int val) { root = addHelper(root, val); } // end of add method
// addHelper method implementation private Node addHelper(Node node, int val) { if(node == null) { node = new Node(val); } else { if(val
// remove method implementation public void remove(int val) { if(isEmpty()) { System.out.println("Tree is empty"); } else if(contains(val) == false) { System.out.println(val + " is not present in the tree"); } else { root = removeHelper(root, val); System.out.println(val + " is removed from the tree"); } } // end of remove method
// removeHelper method implementation private Node removeHelper(Node node, int val) { Node temp1; Node temp2; Node curr; if(node.value == val) { Node leftTree = node.left; Node rightTree = node.right; if(leftTree == null && rightTree == null) { return null; } else if(leftTree == null) { temp1 = rightTree; return temp1; } else if(rightTree == null) { temp1 = leftTree; return temp1; } else { temp2 = rightTree; temp1 = rightTree; while(temp1.left != null) { temp1 = temp1.left; } temp1.left = leftTree; return temp2; } } if(val
// countNodes method implementation public int countNodes() { return countNodesHelper(root); } // end of countNodes method
// countNodesHelper method implementation private int countNodesHelper(Node node) { if(node == null) return 0; else { int count = 1; count += countNodesHelper(node.left); count += countNodesHelper(node.right); return count; } } // end of countNodesHelper method
// contains method implementation public boolean contains(int val) { return containsHelper(root, val); } // end of contains method
// containsHelper method implementation private boolean containsHelper(Node node, int val) { boolean found = false; while((node != null) && !found) { int rval = node.value; if(val rval) node = node.right; else { found = true; break; } found = containsHelper(node, val); } return found; } // end of containsHelper method
// inorder method implementation public void inorder() { inorderHelper(root); } // end of inorder method
// inorderHelper method implementation private void inorderHelper(Node node) { if(node != null) { inorderHelper(node.left); System.out.print(node.value + " "); inorderHelper(node.right); } } // end of inorderHelper method
// preorder method implementation public void preorder() { preorderHelper(root); } // end of preorder method
// preorderHelper method implementation private void preorderHelper(Node node) { if(node != null) { System.out.print(node.value + " "); preorderHelper(node.left); preorderHelper(node.right); } } // end of preorderHelper method // postorder method implementation public void postorder() { postorderHelper(root); } // end of postorder method
// postorderHelper method implementation private void postorderHelper(Node node) { if(node != null) { postorderHelper(node.left); postorderHelper(node.right); System.out.print(node.value + " "); } } // end of postorderHelper method // printTree method implementation public void printTree() { Stack // getHeightHelper method implementation private int getHeightHelper(Node node) { if(node == null) { return 0; } else { return (1 + Math.max(getHeightHelper(node.left), getHeightHelper(node.right))); } } // end of getHeightHelper method } // end of TreePrinter class File: TreePrinterTest.java // TreePrinterTest class implementation import java.util.*; public class TreePrinterTest { // start main method public static void main(String[] args) { // create an object for Random class Random rand = new Random(); // create an object for TreePrinter class TreePrinter tree = new TreePrinter(); /* generate a BST that has height 5 and contains values from 10 through 99, using random numbers */ while(tree.getHeight() Second, create an AVL tree node by node. Generate 35 unique random integers 10-99 to insert into the tree. Print the tree after each insertion to verify that you are keeping it balanced. Each time you do a rebalancing, print a message indicating which rotation operation and which node. For example: Double left-right rotation: 76 As you did with the BST, repeatedly delete the root of your AVL tree. Print the tree after each deletion to verify that you are keeping it balanced. A handy AVL tree balance checker is included in your lecture slides for AVL Trees Second, create an AVL tree node by node. Generate 35 unique random integers 10-99 to insert into the tree. Print the tree after each insertion to verify that you are keeping it balanced. Each time you do a rebalancing, print a message indicating which rotation operation and which node. For example: Double left-right rotation: 76 As you did with the BST, repeatedly delete the root of your AVL tree. Print the tree after each deletion to verify that you are keeping it balanced. A handy AVL tree balance checker is included in your lecture slides for AVL Trees
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