Question
package cs1501_p1; public class BST > implements BST_Inter { private BTNode root = null; public void put(T key) // Add a new key to the
package cs1501_p1;
public class BST
public void put(T key) // Add a new key to the BST { BTNode
private void put(BTNode public boolean contains(T key) // Search the BST for a key, return true if the key is present, false if it is not. { BTNode public void delete(T key) // Remove a key from the BST { if (root == null) { return; } BTNode while (node != null && key.compareTo(node.getKey()) != 0) { parent = node; if (key.compareTo(node.getKey()) child = node.getLeft() != null ? node.getLeft() : node.getRight(); if (parent == null) { root = child; } else if (parent.getLeft() == node) { parent.setLeft(child); } else { parent.setRight(child); } } else // Case 3: Node with two children { BTNode private BTNode while (current != null) { successorParent = successor; successor = current; current = current.getLeft(); } if (successor != delNode.getRight()) //if successor is not the right side of the delnode { successorParent.setLeft(successor.getRight()); successor.setRight(delNode.getRight()); } successor.setLeft(delNode.getLeft()); return successor; } public int height() // Return the height of the BST { BTNode private int height(BTNode public boolean isBalanced() // Return true if the BST is height-balanced { BTNode private boolean isBalanced(BTNode public String inOrderTraversal() // Perform an in-order traversal of the tree and produce a String containing the keys in ascending order, separated by ':''s. { BTNode private String inOrderTraversal(BTNode String s = ""; s += inOrderTraversal(node.getLeft()); s += node.getKey() + ":"; s += inOrderTraversal(node.getRight()); return s; } public String serialize() { return "R(" + root.getKey() + ")," + serialize(root); } private String serialize(BTNode public BST_Inter private BTNode output: Expected: R(10),I(5),L(2),X(NULL),I(37),X(NULL),L(45) Actual: R(45),X(NULL),I(2),L(5),I(10),X(NULL),I(37),X(NULL),I(45),X(NULL)
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