Question

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top