Jump to content

TreeMap & List implementation


Recommended Posts

Hi,

I need to use the TreeMap included with the JDK to store some sortable data.

In addition to maintaining the TreeMap, I need to create a Linked-List, while adding elements to the TreeMap.

At the end of the Insertion/Deletion, it should look like this:

treelist2.gif

(no need to be circular)

any Ideas?

Link to comment
Share on other sites

  • 3 weeks later...

//this code time complexity: O(log(n))
//MyEntry is the type of the objects within the tree nodes.
//key is of type Object
//m_treemap is an instance of TreeMap
SortedMap tailMap = m_treemap.tailMap(key);
MyEntry succ = null;
MyEntry pred = null;
if (!tailMap.isEmpty()) {
succ = (MyEntry) m_treemap.get(tailMap.firstKey());
pred = succ.m_pred;
} else {
//no successor - the pred is the last key in the tree
if (!m_treemap.isEmpty()) {
 pred = (MyEntry) m_treemap.get(m_treemap.lastKey());
}
}
//now succ and pred contains the real successor and predecessor of key

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...