Suppose each binary search tree node is modified by adding a field that stores t
ID: 3813865 • Letter: S
Question
Suppose each binary search tree node is modified by adding a field that stores the number of nodes in its LEFT subtree. This modification is useful to answer the question: what is the k-th SMALLEST element in the binary search tree? (k = 1 means smallest element, k = 2 means second smallest element, etc.)
You are given the following enhanced BSTNode class definition:
}
(a) Implement the following recursive method to return the k-th smallest element in a given enhanced BST. The method should throw a NoSuchElementException if k is out of range. You may implement helper methods if necessary.
(b) Compute the most and least number of units of running time (not big O) for your implementation, by showing all scenarios of tree shape and value of k for which these occur, if there are n nodes in the tree. State the basic unit time operations you are counting (ignore time to compare a pointer against null), show how many times each of these operations are done for each of the scenarios, then add up the operations to get a number that is a function in n and/or k.
Explanation / Answer
import java.util.Scanner;
/* category BSTNode */
category BSTNode
builder */
public BSTNode()
/* creator */
public BSTNode(int n)
/* perform to line left node */
public void setLeft(BSTNode n)
/* perform to line right node */
public void setRight(BSTNode n)
/* perform to induce left node */
public BSTNode getLeft()
come left;
}
/* perform to induce right node */
public BSTNode getRight()
come right;
}
/* perform to line knowledge to node */
public void setData(int d)
/* perform to induce knowledge from node */
public int getData()
come data;
}
}
/* category BST */
class BST
non-public BSTNode root;
/* creator */
public BST()
/* perform to ascertain if tree is empty */
public Boolean isEmpty()
come root == null;
}
/* Functions to insert knowledge */
public void insert(int data)
/* perform to insert knowledge recursively */
non-public BSTNode insert(BSTNode node, int data)
come node;
}
/* Functions to delete knowledge */
public void delete(int k)
rval)
r = r.getRight();
else
{
found = true;
break;
" id="tip_70">
found = search(r, val);
}
come found;
}
/* perform for inorder traversal */
public void inorder()
non-public void inorder(BSTNode r)
non-public void preorder(BSTNode r)
non-public void postorder(BSTNode r)
making object of BST */
BST bst = new BST();
System.out.println("Binary Search Tree Test ");
char ch;
/* Perform tree operations */
do