Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

(0) (10) For this part, refer to the code below which is a skeleton of a linked

ID: 3906068 • Letter: #

Question

(0) (10) For this part, refer to the code below which is a skeleton of a linked implementation of the binary search tree of integers. class Nodet int data; Node left; Node right; public Node (int d, Node 1, Node r)f data d; leftl; rightr;) public class BinSearchTreetf private Node root; public BinSearchTree(...)... //constructor public void printRange(int high, int low) (...) // define more methods here if required Write the Java code to implement the method printRange). This method takes as input two integers high and low and prints out all the nodes, N, in the tree such that high2 N 2lov, i.e., it prints all of the nodes which are in the range high to lou inclusive. In addition, the nodes must be printed sorted in descending order. For example, using the binary search tree given at the beginning of this question, the call printRange (54,22) will have the output: 48 42 40 37 32 24 22 If there are n nodes in the tree and k which lie in the range, what is the average run time and memory usage of your algorithm in O() notation? If k

Explanation / Answer

public void PrintRange(int high, int low)
{
PrintRangeHelper(root, high, low);
System.out.println();
}

//additional recursive helper method
private void PrintRangeHelper(Node n, int high, int low)
{
if(n == null || n.data > high)
return;

PrintRangeHelper(n.right, high, low);

if(n.data < low)
return;
  
System.out.print(n.data + " ");
  
PrintRangeHelper(n.left, high, low);
}


The algorithm is of O(log n) since it searches for elements in the given range based on BST property.