Question: public class BST
public class BSTextends Comparable super T>> { // DO NOT ADD OR MODIFY INSTANCE VARIABLES. private BSTNode root; private int size;
/** * Finds and retrieves the k-largest elements from the BST in sorted order, * least to greatest. * * In most cases, this method will not need to traverse the entire tree to * function properly, so you should only traverse the branches of the tree * necessary to get the data and only do so once. Failure to do so will * result in the efficiency penalty. * * EXAMPLE: Given the BST below composed of Integers: * * 50 * / \ * 25 75 * / \ * 12 37 * / \ \ * 10 15 40 * / * 13 * * kLargest(5) should return the list [25, 37, 40, 50, 75]. * kLargest(3) should return the list [40, 50, 75]. * * Should have a running time of O(log(n) + k) for a balanced tree and a * worst case of O(n + k). * * @throws java.lang.IllegalArgumentException if k > n, the number of data * in the BST * @param k the number of largest elements to return * @return sorted list consisting of the k largest elements */ public ListkLargest(int k) { . . . .//code . . }
public class BSTNodeextends Comparable super T>> { private T data; private BSTNode left; private BSTNode right; /** * Create a BST node with the given data. * * @param data the data to be stored in this node. */ public BSTNode(T data) { this.data = data; } /** * Get the data in this node. * * @return data in this node. */ public T getData() { return data; } /** * Set the data in this node. * * @param data data to be placed into the node. */ public void setData(T data) { this.data = data; } /** * Get the node to the left of this node. * * @return node to the left. */ public BSTNode getLeft() { return left; } /** * Set the node to the left of this node. * * @param left new node to the left. */ public void setLeft(BSTNode left) { this.left = left; } /** * Get the node to the right of this node. * * @return node to the right. */ public BSTNode getRight() { return right; } /** * Set the node to the right of this node. * * @param right new node to the right. */ public void setRight(BSTNode right) { this.right = right; } }
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
