Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Intelligent Information And Database Systems Asian Conference Aciids 2012 Kaohsiung Taiwan March 2012 Proceedings Part 2 Lnai 7197

Authors: Jeng-Shyang Pan ,Shyi-Ming Chen ,Ngoc-Thanh Nguyen

2012th Edition

3642284892, 978-3642284892

More Books

Students also viewed these Databases questions

Question

Name some good career planning approaches that individuals can use.

Answered: 1 week ago