Treemap - Binary Search Tree - Java

5.5k views Asked by At

How can we insert some nodes into a binary search tree using the TreeMap that implements a Red-Black tree class. Then use the entrySet method to get back a Set version of the TreeMap through which we can Iterate over, and get ALL of the data.

1

There are 1 answers

0
GOTO 0 On BEST ANSWER

You could do something like this:

Map<String, String> map = new TreeMap<String, String>(); // create TreeMap

map.put("Key1", "Value1"); // insert a node
map.put("Key2", "Value2"); // insert another node

for (Entry<String, String> entry : map.entrySet()) { // iterate over entrySet
    String key = entry.getKey();
    String value = entry.getValue();
    System.out.println(key + ": " + value);
}

Yours is a very basic question, so I suggest you give a look at the TreeMap documentation for more information.