-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTreeProblems.java
More file actions
86 lines (68 loc) · 2.41 KB
/
Copy pathTreeProblems.java
File metadata and controls
86 lines (68 loc) · 2.41 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
78
79
80
81
82
83
84
85
86
/*
* *** Ryan Compas / COMP 272-001 ***
*
* 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.
// create temporary TreeSets
Set<Integer> treeSetA= new TreeSet<>(setA);
Set<Integer> treeSetB= new TreeSet<>(setB);
// make treeSetA retain common nodes and remove them from setA
treeSetA.retainAll(treeSetB);
setA.removeAll(treeSetA);
// remove common nodes in treeSetA from treeSetB and add remaining treeSetB nodes to setA
treeSetB.removeAll(treeSetA);
setA.addAll(treeSetB);
return setA;
}
/**
* 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) {
// create iterator to move through the treeMap
Iterator<Map.Entry<Integer, String>> iterator = treeMap.entrySet().iterator();
// iterate through treeMap, deleting keys if they are even
while (iterator.hasNext()){
Map.Entry<Integer, String> entry = iterator.next();
if (entry.getKey() % 2 == 0){
iterator.remove();
}
}
return;
}
/**
* 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 ) {
// use equals method to compare trees
if (tree1.equals(tree2)){
return true;
}
else {
return false;
}
}
} // end treeProblems class