Repository navigation
Expand file tree
/
Copy pathTreeProblems.java
More file actions
77 lines (56 loc) · 1.92 KB
/
Copy pathTreeProblems.java
File metadata and controls
77 lines (56 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
* *** Rodrigo Rejas / Section 002 ***
*
* This java file contains several simple tree problems that need to be
* codified. These routines must use the TreeMap and TreeSet library
* classes from the Java Collection Framework.
*
*/
import java.util.*;
public class TreeProblems {
/**
* Method different()
*
* Given two TreeSets of integers, return a TreeSet containing all elements
* that are NOT in both sets. In other words, return a TreeSet of all the
* elements that are in one set but not the other.
*/
public static Set<Integer> different(Set<Integer> setA, Set<Integer> setB) {
// INSERT CODE HERE - DO NOT FORGET TO PLACE YOUR NAME ABOVE
//
// This can be done numerous ways, but once such will only that
// *several* lines of code. Hint: create two temporary TreeSets and utilize the
// methods retainAll(), addAll(), and removeAll(). But in the end, get something to work.
Set<Integer> result = new TreeSet<>(setA);
Set<Integer> temp = new TreeSet<>(setB);
result.removeAll(setB);
temp.removeAll(setA);
result.addAll(temp);
return result;
}
/**
* Method removeEven()
*
* Given a treeMap with the key as an integer, and the value as a String,
* remove all <key, value> pairs where the key is even.
*/
public static void removeEven(Map<Integer, String> treeMap) {
// INSERT CODE HERE.
Iterator<Integer> iterator = treeMap.keySet().iterator();
while (iterator.hasNext()) {
if (iterator.next() % 2 == 0) {
iterator.remove();
}
}
}
/**
* Method treesEqual()
*
* Given two treeMaps, each with the key as an integer, and the value as a String,
* return a boolean value indicating if the two trees are equal or not.
*/
public boolean treesEqual(Map<Integer, String> tree1,Map<Integer, String> tree2 ) {
// INSERT CODE HERE
return tree1.equals(tree2);
}
} // end treeProblems class