CS201 Midterm3 Helper Sheet
CS201 Midterm3 Helper Sheet
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);
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
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