0% found this document useful (0 votes)
17 views8 pages

Comp201 Tut

The document covers various data structures in Java, including HashSet, LinkedHashSet, TreeSet, HashMap, LinkedHashMap, and TreeMap, explaining their characteristics and use cases. It also discusses sorting methods, the differences between ArrayList and LinkedList, and includes code examples for comparing objects and manipulating linked lists. Additionally, it outlines operations for stacks and queues, including enqueue and dequeue methods.

Uploaded by

Calvin Pillay
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views8 pages

Comp201 Tut

The document covers various data structures in Java, including HashSet, LinkedHashSet, TreeSet, HashMap, LinkedHashMap, and TreeMap, explaining their characteristics and use cases. It also discusses sorting methods, the differences between ArrayList and LinkedList, and includes code examples for comparing objects and manipulating linked lists. Additionally, it outlines operations for stacks and queues, including enqueue and dequeue methods.

Uploaded by

Calvin Pillay
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

COMP201 Tutorial 2:

1. Hashset – Can be used to store duplicate free elements,


elements are not in order.
Linked Hashset – Elements are stored in the order in which
they were inserted.
TreeSet – Elements are stored in natural order, Elements
sorted.

2. Hashmap – Locating a value (No duplicate elements) have


key-value pairs.
LinkedHashMaps – Supposed ordering of entities, (same as
above)
TreeMap – Traversing keys in sorted order.

3. A. Uses comparable, use the Collections.sort(container)


method.

B. A comparator that compares object of the same type as


the elements in the container need to be defined.
public void compare(Object o1, Object o2);

4. An arraylist uses an array as an underlying container. If


item has to be added at index p, all elements from p to n-1
have to be moved one step forward. Likewise if the item to
be removed; elements from index n-1 moved backwards.

1 2 3 4 5

Add element

1 2 Element 3 4 5

+1 +1 +1
A LinkedList is a linked data structure were every element
(except the last) has reference to the next element in the
list. To add the element at a given index, p, involves
making the element at p-1, point to the new element
which was at index p. Involve few operations com[pared to
arraylist.
|

5. Hashmap – Allows for duplicate names in instances of


different names.

6. HashMap – Has (key, value) and can store players name


and information. Can store all of the person’s information
as an object.

7. a. No errors – ArrayList can take objects at any type since


type for elements is not given (Arraylist dates = new
ArrayList();)

b. Yes, the arrayList can only take elements of type date. A


string cannot be accepted.

8. a. Set1.addAll(set2);
- “red”, “yellow”, ”green”, “blue” : set1
- “red”, “yellow”, “blue” : set2

b. Set1.add(set2);
- Set1: (Red, yellow, green[“red”, “yellow”, “blue”]

c. Set1.RemoveAll(set2)
- Set1: “green”

d. Set1.Remove(set2)
- [red, yellow, green] – set1
- [red, yellow, blue] – set2

e. Set1.RetainAll(set2)
- Set1: “red”, “yellow”

f. set.clear();
- set1: [] – empty

9. a. Myst method is designed to count how many times


keyword from “kw” array appears in the “tk” array and
returns the count to the map.

b. Constructor new HashMap<String, Integer>


(KeyLength) enhances by pre-sizing “Hashmap” to exact
number of keywords. Reduces need for rehashing and
minimize memory.

c. O(n).

10.
a. Bubble sort – O(n2)

b. Quick Sort – O(n2)

11.
Outer loop i | 1 3 9 … n (i≤ n)
k
Inner loop | 1 3 9 … 3
Executed i times

T(n) = 30 + 31 + 32 + …. + 3k
k+1
3 −1
n = 3k
3−1
k = log3n
1
= 3k+1 – 2
1
= 3log3n-+1 - 2
1 1
= 3log3n * 3 2 - 2
T(n) = O(n)

Innermost Runs: O(n)


Total Time = O(n) x O(n)
= O(n2)

12. a.

public class RealNumber implements


Comparable<RealNumber> {
private int i;
private double f;

public RealNumber(int i, double f) {


if (f < 0 || f >= 1) {
throw new IllegalArgumentException("Fractional part
must be in the range [0, 1).");
}
this.i = i;
this.f = f;
}

public int getI() {


return i;
}
public double getF() {
return f;
}

@Override
public int compareTo(RealNumber other) {
if (this.i != other.i) {
return Integer.compare(this.i, other.i);
} else {
return Double.compare(this.f, other.f);
}
}

b.
public class CompareF implements Comparator<RealNumber>
{
@Override
public int compare(RealNumber rn1, RealNumber rn2) {
// Compare the f-components first
int result = Double.compare(rn1.getF(), rn2.getF());

// If the f-components are equal, compare the i-


components
if (result == 0) {
result = Integer.compare(rn1.getI(), rn2.getI());
}
return result;
}

public static void main(String[] args) {


RealNumber rn1 = new RealNumber(3, 0.5);
RealNumber rn2 = new RealNumber(3, 0.2);
RealNumber rn3 = new RealNumber(-2, 0.5);
RealNumber rn4 = new RealNumber(4, 0.5);

CompareF comparator = new CompareF();

System.out.println("Comparing rn1 and rn2: " +


comparator.compare(rn1, rn2)); // Output > 0 (because 0.5 >
0.2)
System.out.println("Comparing rn1 and rn3: " +
comparator.compare(rn1, rn3)); // Output > 0 (because 3 > -2)
System.out.println("Comparing rn1 and rn4: " +
comparator.compare(rn1, rn4)); // Output < 0 (because 3 < 4)
System.out.println("Comparing rn1 and rn1: " +
comparator.compare(rn1, rn1)); // Output 0 (because they are
equal)
}
}

13. a. At the last position

b. Checks if the list only has one node, making the empty
by setting
head, tail to null and element removed & returned.
c. Purpose is to traverse the elements in a doubly linked
list to reach
second-to-last node (node before tail)

d.
public E removeNode() {
if (head == null) {
return null;
} else if (head.next == null) {
DLLNode<E> temp = head;
head = tail = null;
size--;
return temp.element;
} else {
DLLNode<E> temp = tail;
tail = tail.prev;
tail.next = null;
size--;
return temp.element;
}
}

15. a. IndexSize – 1

b. push(): This operation adds an element to the top of


the stack. For a linked list, this corresponds to adding a new
node at the head of the list. The new node will be created and
its next pointer will be set to point to the current head of the
list. The head pointer of the list will then be updated to this new
node. This operation is O(1).
pop(): This operation removes the element from the top of the
stack and returns it. For a linked list, this corresponds to
removing the node at the head of the list. The head pointer will
be updated to point to the next node in the list (i.e., head.next).
The removed node can then be returned or its value extracted.
This operation is also O(1).

16.
1. Enque
public void enqueue (E element) {
arraylist.add(element);
}

2. Dequeue
if (arraylist.isEmpty()) {
return null;
}
int highest = 0;
for (int i = 0; i < arrayList.Size(); i++){
if (arrayList.get(i).compareTo()){
if (arrayList.highestP > 0){
highest = c;
}
}
}

You might also like