Question: I'm trying to write a method for getting the qth smallest element in a binary search tree, and this doesn't work with tests. My methods

I'm trying to write a method for getting the qth smallest element in a binary search tree, and this doesn't work with tests. My methods are: E qSmallest(int q) throws java.lang.Exception { if(root == null){ throw new Exception(""); } else{ return qSmallestAux(q,root); } }

E qSmallestAux(int k, BNode node) throws java.lang.Exception { if(k == 0){ return node.key; } else if (q > node.leftSubTreeSize){ q = (q - node.leftSubTreeSize); return qSmallestAux(q, node.right); } else{ return qSmallestAux(q,node.left); } }

For a test, I'm trying to do this: BinarySTree E; HashSet HS; int[] alpha = { 6, 3, 5, 2, 6, 10, 9 }; int[] alphaSorted = { 2, 3, 5, 6, 9, 10 }; for (int j = 0; j j < alpha.length; i++) { E.insert(alpha[j]); HS.add(alpha[j]); } for (int j = 0; J < alphaSorted.length; i++) { int small = E.qSmallest(j); assertEquals(small, alphaSorted[j]); }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!