Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

Questions about Data Structures in Java?

1. A Heap is defined as a special kind of BinaryTree and a BinarySearchTree is a kind of BinaryTree, yet neither of these two containers is an extension of BinaryTree, Explain Why.

2. For any Container to be useful, we must be able to add and remove objects, yet Container does not define any commands for adding and removing, Why?

3. In terms of accessing all the elements in a container, how does an Iterator object offer an advantage over simple query methods that actually iterate through all objects in the container for you? When is it better to use a simple query as opposed to returning an Iterator?

4. If Java provided the capability for generic types, how would the Container hierarchy be affected? Specially, how would interface Container change?

3 Answers

Relevance
  • 1 decade ago
    Favorite Answer

    I'll just comment on the first one.

    A heap is NOT defined as a type of Binary Tree. It is a type of TREE, but it is perfectly valid to be a multi-branch tree, not limited to 2 sub nodes. In fact, in order to efficiently merge (which is necessary to efficiently delete), it should be a binomial tree, with no limit on branches at each node.

    In theory, iterators are less prone to programmer error and can be more efficient. For a vector or a list, the iterators are trivial and that isn't really true, but for a tree the iterator maintains a lot more information and it can be much simpler than implementing a recursive function.

    Container is a base class and cannot be used stand-alone. The parameters for add and remove methods may vary based on the type of container, so they are not included in the base interface.

    EDIT: I didn't really understand your question about iterators before. If there is a query function you can use, you should use it, but it is probably even more common that you need to iterate through for another purpose than those provided for you (printing them out, modifying the values, performing some operation on every element)

  • Anonymous
    5 years ago

    Any generic collection will work for storing the data. Choose a data structure depending on how you intend to access the data. If you just need to glue two unrelated things together, maybe to return as a single function result, then an Object[] array is an easy option. return new Object[] {rainfallData, ageAndFootballList}; If that's in a getRainfallAndFootball() method, then the caller could use: Object[] result = getRainfallAndFootballList(); myRainfallData = result[0]; myFootballList = result[1]; It's hard to tell exactly what's needed, since you don't say what you plan to do with the data.

  • 1 decade ago

    Thank you for your questions.

    No. 2 bothered me the most. You have to have a diagram of the hierarchy to fully appreciate this...

    java.lang.Object

    ..|

    ..+--java.awt.Component

    .........|

    ..........+--java.awt.Container

    .......... |

    ........... +--javax.swing.JComponent

    Note that Container extends abstract Component. Then javax.JComponent extends Container.

    Therefore, Swing components can mix and match.

    You can add to awt.Container. But you will need another Container to add to any components' internal spaces.

    =============

    I would really like your answers if you ever get these...

    I'm still researching the others...

    Source(s): for a better picture of the component heirarchy http://www.cs.qub.ac.uk/~P.Hanna/JavaProgramming/L...
Still have questions? Get your answers by asking now.