0% found this document useful (0 votes)
0 views6 pages

CS201 Midterm3 Helper Sheet

The document provides an overview of various data structures in Java, including ArrayList, HashSet, and HashMap, along with their methods and performance characteristics. It also covers algorithms for sorting, tree structures, and the Huffman compression process, detailing both compression and decompression steps. Additionally, it includes code snippets for manipulating lists and implementing basic data structure operations.

Uploaded by

6km456927g
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)
0 views6 pages

CS201 Midterm3 Helper Sheet

The document provides an overview of various data structures in Java, including ArrayList, HashSet, and HashMap, along with their methods and performance characteristics. It also covers algorithms for sorting, tree structures, and the Huffman compression process, detailing both compression and decompression steps. Additionally, it includes code snippets for manipulating lists and implementing basic data structure operations.

Uploaded by

6km456927g
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/ 6

List.

contains(“hello”)
List.get(i)
ArrayList
ArrayList can’t hold primitive like int,
char
ArrayList<String> students = new ArrayList<String>();
BUT use Integer, Character
studentList.add("John"); studentList.remove(0);
studentList.remove("Lily"); int bat = Collections.frequency(list,”bat”) Traverse array:
for (int i = 0; i <
arrayName.length; i++)
Array
dataType[] arrayName = new dataType[size];
for (dataType element :
Changing Elements: arrayName[index] = value / arrayName.length
arrayName)
String[] words = orginal.split(“ “) Growing array my multiplying is linear but by
adding elements is quadratic
Iterator<Integer> it = numbers.iterator();
while (it.hasNext()) {
System.out.println(it.next());
Hash Set
// Example:
for (Integer num : numbers) {
HashSet<dataType> setName = new HashSet<>();
System.out.println(num);
setName.add(element);
}
int size = setName.size();
Hash Set
Iterating through a HashSet
Using Iterator
HashSet<dataType> setName = new
Iterator<dataType> iterator = setName.iterator();
HashSet<>();
while (iterator.hasNext()) {
setName.add(element);
dataType element = iterator.next();
int size = setName.size();
System.out.println(element);

Using Enhanced for-loop (for-each loop)


for (dataType element : setName) { Constructor with parameters
System.out.println(element); //instance variables
} String make;
HashSet<Type> Set = new HashSet<>(Arrays.asList(s)) String model;
int year;
Hash Map public Car(String make, String model, int
HashMap<KeyType, ValueType> mapName = new HashMap<>(); year)
{
mapName.put(key, value);
this.make = make;
this.model = model;
ValueType value = mapName.get(key); this.year = year;
boolean exists = mapName.containsKey(key);
boolean exists = mapName.containsValue(value); Default Constructor
public Car()
model = “”;
ValueType value = mapName.getOrDefault(key, defaultValue);
year””;

Iterating through Keys


for (KeyType key : mapName.keySet()) Map.putIfAbsent(key, value)

If value is arrayList and you


String methods, length(), .charAt(i), .substring(a,b), s.toLowerCase()
want to add to arraylist use:
Static belongs to class not object just like Math class List.get(key).add(value)
Join array of string: String.join(“ “, words)

Code
Stacks
Queue
Last in, First out structure •First in, First out structure aka FIFO
Add and remove from end of ArrayList Add at end of LinkedList, remove from
Achieve O(1) time for add and remove front Achieve O(1) time for add and
remove
•Both add (offer) and remove (poll) are
O(1)
• push (add), pop (remove), peek
Depth-first search

Simple Sorting Algorithms (O(n2) )


Trees: Selection Sort n2 comparisons, n swaps – find min
• O(log N) search, add, delete, range queries
swap to front
Binary search tree performance: . O(log N) Insertion Sort – n2 comparisons no swaps – If 2 nd
FULL BST has 2^n - 1 nodes element smaller add it to front and shift
Post order- root last everything
Preorder – root frist Bubble Sort – if first element is greater then swap
Inorder- least to greatest – alphabetical with second – largest element moves to end

Efficient sort (nlogn)


DIVIDE AND CONQUER
Quicksort – choose pivot everything goes left. Then get pivot chosen from middle of that
half. Recurse on left and right
Merge Sort: divide in half, sort them, merge them together – recursion

Comparing – uppercase LESS than lowercase; ant compare to zebra is less that 0

AUTOCOMPELTE
PERCOLATION
Huffman Compressing
Compression Process
1. Count Character Frequencies
getCounts(BitInputStream in)
 What Happens:
o Reads the input data one word (typically 8 bits, a byte) at a time.
o Maintains a frequency count for each character in an array of size ALPH_SIZE + 1.
o Adds an additional special marker PSEUDO_EOF to the array with a frequency of 1, ensuring the
compressed file ends uniquely.
 Purpose:
o The frequency count is the basis for building the Huffman Tree.
2. Build the Huffman Tree
Method: makeTree(int[] counts)
 What Happens:
o A PriorityQueue is used to create the Huffman tree:
1. Each non-zero frequency character (and PSEUDO_EOF) is added to the queue as a HuffNode.
2. The queue always keeps nodes with the smallest frequency at the front (due to the compareTo
method in HuffNode).
3. Two nodes with the smallest weights are removed, combined into a new parent node, and
reinserted into the queue.
4. This continues until only one node (the root of the tree) remains.
 Purpose: Encodes the relative frequency of characters into a binary tree structure.
3. Generate Encodings
Method: makeEncodings(HuffNode root, String path, String[] encodings)
 What Happens:
o Recursively traverses the Huffman tree.
o Assigns binary codes (path) to each character:
 Moving left adds 0 to the path.
 Moving right adds 1 to the path.
o Leaf nodes (representing characters) are assigned their complete binary paths.
 Purpose: Creates a mapping between characters and their Huffman codes for compression.
4. Write Tree and Encoded Data
 Write Tree: Method: writeTree(HuffNode root, BitOutputStream out)
o Serializes the structure of the Huffman tree to the output stream.
 For leaf nodes: Writes a 1 followed by the character value.
 For internal nodes: Writes a 0 and recursively processes the child nodes.
o This ensures that the decompression process can reconstruct the exact tree.
 Write Encoded Data: Method: writeEncodings(String[] encodings, BitInputStream in,
BitOutputStream out)
o Reads the input file again.
o For each character, looks up its binary encoding in the encodings array and writes it to the output.
o At the end, writes the encoding for PSEUDO_EOF to signify the end of the file.

Decomporession:
1. Validate File Header
Method: decompress(BitInputStream in, BitOutputStream out)
 What Happens:
o Reads the first 32 bits (magic number) from the input.
o Confirms that the number matches HUFF_TREE.
o Throws an exception if the file is invalid
2. Reconstruct the Huffman Tree
Method: readTree(BitInputStream in)
 What Happens:
o Reads the serialized tree structure:
 For a 0: Recursively reconstructs the left and right child nodes.
 For a 1: Reads the next bits to retrieve the character value for a leaf node.
o Reconstructs the exact Huffman tree used during compression.
 Purpose: The Huffman tree is needed to decode the compressed data.
3. Decode Encoded Data
 What Happens:
o Starts at the root of the Huffman tree.
o Reads one bit at a time from the compressed input:
 0 moves to the left child.
 1 moves to the right child.
o When a leaf node is reached:
 If the value is PSEUDO_EOF, stops decoding.
 Otherwise, writes the character value to the output and resets to the root.
 Purpose: Translates the binary data back into the original characters

public class ListLastFirst { public class ListStretch {


public ListNode move(ListNode list) { public ListNode stretch (ListNode list, int
if (list == null){ amount){
return null; if(list == null){
} return null; }
if (list.next == null){ ListNode head = null;
return list; ListNode current = null;
while(list != null){
} for (int i=0; i<amount; i++){
ListNode newNode = new
ListNode current = list; ListNode(list.info);
while (current.next.next != null){ if(head == null){
current=current.next; } head = newNode;
ListNode lastNode = current.next; current = newNode; }
current.next = null; else{
current.next= newNode;
lastNode.next = list; current = current.next; } }
return lastNode; list=list.next;}
}} return head;
}}

You might also like