Win a copy of Cloud Native PatternsE this week in the Cloud forum
or Natural Language Processing in the AI/ML forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Class that implements the abstract type Dictionary using Binary Search Trees

 
Ranch Hand
Posts: 66
Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello to all, for a task I was assigned to create an interface Dictionary <K, V> like this:


and an interface that extends the previous one, like this:


Well, now I have to implement these interfaces using a class that uses binary search trees. So I wrote a Node Class and a SearchTree class, like this:


and:


(I just copied the methods of the two interfaces).
But Eclipse generates an error saying that lack of methods, and resolve the error by creating these methods:


Why replace the values V and K with Object? Why not go well those created by me?

Other questions:
  - I implemented the method isEmpty() this way:

  - Then I was trying to write the find() method but do not understand why written in this way is not good...


I hope I explained myself, otherwise I'll add more details.

Thanks to anyone who will help me.


 
Marshal
Posts: 65002
246
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not what happened when I tried it.
I told Eclipse to create a new package, called dictionary.
Then I told it to create two interfaces Dictionary and SortedDictionary extends Dictionary and Node and SearchTree classes, and it gave me this sort of methods:... but with red marks against them all.
I then added a constructor, as you have, but changed the root declaration to
private Node<K, V> root;
Then I added a main methodand changed the isEmpty() method to ...
return root == null;


...


And Eclipse happily ran the whole thing and printed true at the console.

No idea what is going wrong. I am using Eclipse Luna on Ubuntu 14.04LTS with JDK8u45.
 
Campbell Ritchie
Marshal
Posts: 65002
246
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have already told you what to do with the isEmpty() method.
I would write a getValue method recursively
return isEmpty() ? null : root.getValue(key);
… which necessitates a
private V getValue(K key)
method in the Node class.

Make the Node class a private inner class in the Tree class; you never need to access a node from outside a tree.

I shall leave you to work out what the errors with < in your original version are. Remember what the parameters for the tree class are.
 
Alicia Perry
Ranch Hand
Posts: 66
Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Strange..
I modified the method isEmpty() as recommended and I added the parameters K and V to the field root.

I also use Eclipse Luna with Windows 7, with JDK8.
 
Alicia Perry
Ranch Hand
Posts: 66
Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:I have already told you what to do with the isEmpty() method.
I would write a getValue method recursively
return isEmpty() ? null : root.getValue(key);
… which necessitates a
private V getValue(K key)
method in the Node class.

Make the Node class a private inner class in the Tree class; you never need to access a node from outside a tree.

I shall leave you to work out what the errors with < in your original version are. Remember what the parameters for the tree class are.



Thank you for your help.
I created the two methods getValue (one in the Node class and another class Tree).
Then I changed the higher / lower with compareTo but remains an error:

In lines 4 and 6 Eclipse says I should make a cast to (V) and then write:

Why?
I feel so stupid ..
 
Campbell Ritchie
Marshal
Posts: 65002
246
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Coffea arabica. That's what you need. If it doesn't work when put in boiling water and drunk, then have it injected directly into your central nervous system

And put your feet up for half an hour.

Then come back and delete the getValue method from the tree class. Because you already know you have to use find() for that. And you already know what find() is supposed to look like.
Then delete the getValue() method from node because you are going to have to start again. Take it easy because these things are simpler than you think. You have already worked out that you need compareTo rather than < but you haven't worked out what you are comparing to what.

And either inflict upon Eclipse what it inflicted on me … or turn it off and work from the command line. Or both. That way you won't be messing about with casts.
 
Alicia Perry
Ranch Hand
Posts: 66
Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I looked at everything calmly and my solution would be this:



Do you think is a fair solution? Or am I wrong again?
Thanks
 
Campbell Ritchie
Marshal
Posts: 65002
246
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Getting better

But decide which classes those methods should be in. I think find needs to be in the tree class, but I would have put the other method in the node class.
I would write the find method without using return twice. But you already know that.
 
Alicia Perry
Ranch Hand
Posts: 66
Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, so I modified methods.

In SearchTree Class:


In Node Class:


 
Campbell Ritchie
Marshal
Posts: 65002
246
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Getting better but that looks like a static method. I would like to see an instance method. Heading:
private V getValue(K key)
Remember I am assuming your Node class is an inner class in the tree class so you can use private access.
 
Campbell Ritchie
Marshal
Posts: 65002
246
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I told you yesterday what to use for the find(K) method. Why have you changed it?
 
Alicia Perry
Ranch Hand
Posts: 66
Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The professor wants that the auxiliary methods are visibility package and not private.
This is because he wants that all methods are tested with JUnit...
 
Campbell Ritchie
Marshal
Posts: 65002
246
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aaaaaaaaaaaaaaaaaaaaaaah. New information. Thank you.
In which case give the finding method in the Node class package‑private access, and make the Node class package‑private in the same package as the tree class otherwise you might not be able to do the unit testing. The tree class will of course need public access.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!