I have a class that extends java.util.TreeMap, I was wondering if anyone knows how I can get the root node, and for that manner how I can manually traverse the tree if I want to. When I look at the source for treemap at http://javasourcecode.org/html/open-source/jdk/jdk-6u23/java/util/TreeMap.java.html the root node is private, thus I can't neccessarily access it directly, however perhaps there is a way to access it using a combination of the methods already in treemap?
Please don't write any code for me, just point me in the right direction/pseudocode is ok.
TreeMap stores key-value pairs. The main difference is that TreeMap sorts the key in ascending order. TreeMap is sorted as the ordering of its keys, or by a Comparator provided at map creation time, depending on which constructor is used. TreeMap(Map map) It creates a TreeMap with the entries from a map.
hashCode and equals method are not required for TreeSet and TreeMap as the sorting depends on either the compareTo or compare method as been provided by the client.
In Java, a TreeMap is a red-black tree, which is a self-balancing binary search tree.
You can access private members using reflection. That defeats the purpose of encapsulation in OO languages though. You can learn about reflection in this tutorial from oracle.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With