need some code in java

Closed Posted 7 years ago Paid on delivery
Closed

Concerning the size field in each Node

1.1 Get a listing by index

Our goal is to implement a get(int i) method which takes an integer zero-based index i and

return the i-th smallest (with respect to the price) listing in the BST. There are many ways to

accomplish this, in fact, it can be done in at least two ways by using methods you implemented

in Homework 8. For example, we can call getMin to get the listing at index 0, then keep calling

getNext until we find the i-th listing. This will take O(ilog n) time on a balanced BST with n

nodes which is OK for small i but is not efficient when i approaches n. Alternatively we can use

the toArray method to dump the tree into a sorted array then simply return the listing at index

i. This approach will take O(n) time and O(n) extra space.

In order to do it more efficiently (namely, in O(log n) time), we add a size field to each node

which keeps record of the number of nodes in the subtree rooted at this node. Given this field, we can

easily determine (why?) whether the target node is to the left, to the right, or is just at the current

node. We recommend that you do this using a recursive helper method doGet(Node r, int i)

but this is not an absolute requirement.

1.2 Reimplement the add method

Notice that in order to keep the size field of each node correct, our add method has to be modified

to update it. Recall the add method needs to first find the correct position to add a new element, all

the nodes on this search path need to have their size fields updated unless we did not add the new

element (which will happen if there is some other listing with the same price). This can be done by

first add the node, if succeeded, then go through this search path again to update size. However

this is not efficient in the sense it does twice as many comparisons as necessary. Keeping track of

all the nodes encountered on the search path using a stack will work but in order to give you more

practice on recursion, we will NOT do it. Instead, we will re-implement the add method using a

recursive helper method Node doAdd(Node r, listing l) which tries to add the new listing into

a subtree rooted at r and returns the (possibly modified) root of this subtree.

1.3 New invariant

In addition to the above changes, we need to check if the size field in each node is correct as

an invariant. This can be done by augmenting the _checkInRange method you implemented in

C Programming C++ Programming Engineering Java SQL

Project ID: #12082489

About the project

12 proposals Remote project Active 7 years ago

12 freelancers are bidding on average ₹316/hour for this job

giobong

WTF? What an understandable and unreadable project(................................................................... .............................

₹277 INR / hour
(2 Reviews)
2.6
denekefik

Hi I'm ready to help you

₹200 INR / hour
(4 Reviews)
1.8
FHTeam

We are a team of new Freelancers. We have good experience in Java Coding. We can finish your project in less than a week. We are willing to give you the least price because all we are looking forward to is customer's t More

₹111 INR / hour
(0 Reviews)
0.0
taiwofagbamila

I currently live in calgary where I lead the development of a huge, critical java application where performance is critical.

₹277 INR / hour
(0 Reviews)
0.0
elakktech

We are a team of more than 10+ years of experience in different domains, as part of this project we will deliver within stipulated period.

₹533 INR / hour
(0 Reviews)
0.0
karthikmuthyam

A proposal has not yet been provided

₹333 INR / hour
(0 Reviews)
0.0