allNotes-v2-3-26-2025
allNotes-v2-3-26-2025
Table of Contents
Chapter 1 - Syllabus ............................................................................................................................................ 3
Chapter 2 - Java Review ..................................................................................................................................... 4
2.1 - Generics .................................................................................................................................................. 4
2.2 - Testing .................................................................................................................................................... 8
2.3 - Junit Testing Worksheet ....................................................................................................................... 10
2.4 - Java Collections .................................................................................................................................... 12
2.5 - Exceptions ............................................................................................................................................ 13
Chapter 3 - ArrayList ........................................................................................................................................ 14
Chapter 4 - Linked List ..................................................................................................................................... 17
4.1 - Linked List ............................................................................................................................................ 17
4.2 - Iterator .................................................................................................................................................. 19
Chapter 5 - Runtime Analysis ........................................................................................................................... 25
5.1 - Iterator and Runtime Worksheet........................................................................................................... 31
Chapter 6 - Hashing .......................................................................................................................................... 33
Chapter 7 - Stack and Queue ............................................................................................................................ 37
Chapter 8 - Priority Queue (Heap) .................................................................................................................... 43
Chapter 9 - Trees ............................................................................................................................................... 50
9.1 – Binary Tree .......................................................................................................................................... 51
9.2 - Binary Search Tree ............................................................................................................................... 54
9.3 - Tree and BST worksheet ...................................................................................................................... 57
Chapter 10 - Sorting .......................................................................................................................................... 59
Chapter 11 - CSE 12 Reviews .......................................................................................................................... 67
Chapter 12 - Review exercises .......................................................................................................................... 69
2
CSE 12 – Basic Data Structures Paul Cao UC San Diego
Chapter 1 - Syllabus
Important course sites
1. canvas.ucsd.edu is our material repo and gradebook.
2. gradescope.com is our homework submission site
3. piazza.com is our discussion forum
4. autograder.ucsd.edu is our tutor help site (also has course calendar)
5. zybooks is our textbook and reading site (has to link from canvas to zybooks!)
6. Course syllabus: https://bit.ly/12cao-syllabus
7. Course schedule: https://bit.ly/12cao-schedule
Topics
• Java Collections Library
• The use of generics, interfaces. abstraction, ADTs, invariants
• Exception design and handling
• Runtime analysis
• List structures: ArrayList and LinkedList
• Hashing technique
• Stack and Queue
• Heap and Priority Queue
• Binary Tree and Binary Search Tree
• Sorting algorithms (comparison based and non-comparison based)
Grading
• Reading assignments from zyBooks
• Class participation
• Quizzes
• Midterm
• PA
• Final exam (a portion of the final can be used to sub the midterm)
Academic Integrity
• All work individually done
• No public github repo pls
• Penalty for AI violation
• Looking at/Googling for “generic”, API-level code is fine, and even encouraged.
• Looking at (or copying) code that has anything at all to do with the assignment you are working on is not OK,
even if it’s easy to find, unless it was explicitly provided to you in class or in the textbook. If you are using such
code, you must cite it properly.
• Discussing the assignments at a high level is OK. Writing any kind of code or pseudocode together is NOT OK.
4
CSE 12 – Basic Data Structures Paul Cao UC San Diego
A. Yes B. No
A few notes
You are not allowed to use Generics as follows
• In creating an object of that type:
new T() // error
• In creating an array with elements of that type:
new T[100] // error
• As an argument to instanceof:
someref instanceof T // error
• To ensure that certain methods can be called, we can constrain the generic type to be subclass of an
interface or class
public class MyGenerics <E extends Comparable>{ ………}
Runtime
Generics Worksheet
import java.io.*;
import java.util.*;
public class GenericWorksheet<E>{
public E[] data;
public GenericWorksheet(){
5
CSE 12 – Basic Data Structures Paul Cao UC San Diego
public E findLast(){
}
public E removeFirst(){
}
public String toString(){
}
public static void main(String[] args){
Integer[] array = new Integer[]{5, 6, 33, 11, 99};
__________________________________________________________________
System.out.println(ref.findMedian());
System.out.println(ref.findLast());
System.out.println(ref.removeFirst());
System.out.println(ref);
//What if I have another class of my own and want to use it for this
generic class?
}
}
6
CSE 12 – Basic Data Structures Paul Cao UC San Diego
Data Abstraction
Abstract Data Types vs specific implementations
7
CSE 12 – Basic Data Structures Paul Cao UC San Diego
2.2 - Testing
Black box testing
• You don’t know (or you pretend you don’t know) how something is implemented
• You test only based on inputs and outputs
White box testing
• (Also known as “clear-box testing”)
• If you can look inside the black box and see how a method is implemented, you can do more detailed
testing
Time-consuming and tedious − Since test cases are Fast − Automation runs test cases significantly faster than
executed by human resources, it is very slow and tedious. human resources.
Huge investment in human resources − As test cases Less investment in human resources − Test cases are
need to be executed manually, more testers are required in executed using automation tools, so less number of testers are
manual testing. required in automation testing.
Less reliable − Manual testing is less reliable, as it has to More reliable − Automation tests are precise and reliable.
account for human errors.
Non-programmable − No programming can be done to Programmable − Testers can program sophisticated tests to
write sophisticated tests to fetch hidden information. bring out hidden information.
Junit Testing
• Test method: a method in a Java class that contains a single unit test.
• Test class: a Java class containing one or more test methods.
• Assertion: a statement that you include in a test method to check that the results of a test are as
expected.
• Test fixture: a class that is used to set up state for multiple tests; typically used when the set up routines
are “expensive” or take a long time to execute.
• Test suite: a grouping of test classes that are run together.
8
CSE 12 – Basic Data Structures Paul Cao UC San Diego
public class CSE12 { import static org.junit.Assert.*;
private int numStudents; import org.junit.Before;
private boolean largeClass; import org.junit.Test;
If I am testing CSE12 class’s getNum method, should I also test if the largeClass instance variable
has the correct value?
A. Yes B. No
9
CSE 12 – Basic Data Structures Paul Cao UC San Diego
2.3 - Junit Testing Worksheet
We write a Person class and we have the following methods
Fields: age (int), name (String), height (String in the format of f’ii) where f is a one digit
number and ii is a two digit number).
a. Default constructor: age is 0, name is null, height is null
b. A constructor that takes an age, name, and height and initialize all instance variables with parameter
values. If age < 0 or name or height isn’t in the correct format, throw an Exception with error message
“Wrong parameter!”
c. A function getNameLength that returns the number of characters in the name (not including spaces).
Throws an Exception if name is null
@Test
public void testCtor1(){
boolean exception = false;
try{
}
catch (Exception e){
exception = true;
}
assertEquals("Wrong parameters!", true, exception);
}
@Test
public void testCtor2(){
boolean exception = false;
try{
}
catch (Exception e){
exception = true;
}
10
CSE 12 – Basic Data Structures Paul Cao UC San Diego
@Test
public void testCtor3(){
boolean exception = false;
try{
}
catch (Exception e){
exception = true;
}
assertEquals("Wrong parameters!", true, exception);
}
@Test
public void testGetNameLength(){
boolean exception = false;
try{
}
catch (Exception e){
exception = true;
}
11
CSE 12 – Basic Data Structures Paul Cao UC San Diego
2.4 - Java Collections
public interface Collection<E> is the root type of Java Collections. It defines common operations
for lists, vectors, stacks, queues, priority queues, and sets. Java doc is at
https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/doc-files/coll-overview.html
Which of the following lines of Java code are legal? Assume default constructors exist for all classes.
A. Collection c = new List();
B. ArrayList a = new LinkedList();
C. List myList = new ArrayList();
D. List myLinkedList = new LinkedList();
Collection myCollection = myLinkedList;
E. More than one choices are correct
12
CSE 12 – Basic Data Structures Paul Cao UC San Diego
2.5 - Exceptions
An exception is an event that disrupts the normal flow of your application. Two types of exceptions; Checked and
Unchecked.
import java.io.*;
import java.util.*;
public class ExceptionsWorksheet{
public static void main(String[] args){ //Begin: Multiple catch exception
try {
//Begin: Catch arithmetic exception // Write code so it is caught in the general
try { // exception and NOT ArithmeticException
// Write code to generate
ArithmeticException
}
catch(ArithmeticException e){
System.out.println("Can't divide a number by
zero.");
}
catch(Exception e){
} System.out.println("General exception
catch(______________________ e){ caught");
}
System.out.println("ArithmeticException //End: Multiple catch exception
caught");
}
//End: Catch ArithmeticException //Begin: Throw your own ArithmeticException
try {
// Throw your own arithmetic exception
//Begin: Catch number format exception
try {
// Write code to generate
NumberFormatException }
catch(ArithmeticException e){
System.out.println("Exception caught");
}
//End: Throw your own ArithmeticException
13
CSE 12 – Basic Data Structures Paul Cao UC San Diego
Chapter 3 - ArrayList
ArrayList is an implementation of the List interface. Simulating the behavior of an array
- It allows “dynamic” increase or decrease of array size
- Supports dictionary operations
- https://docs.oracle.com/en/java/javase/18/docs/api/java.base/java/util/ArrayList.html
- List interface has 20+ methods, AbstractList has about 10+ methods unimplemented.
Worksheet
public class MyArrayList<E>{
public Object[] data;
private static final int DEFAULT_CAPACITY = 5;
private int size;
14
CSE 12 – Basic Data Structures Paul Cao UC San Diego
/**
* Remove and return the first element in the list
* @return the first element in the array list
*/
public E removeFirst() {
/**
* Return a string representation of this arraylist.
*/
public String toString(){
// TODO: This method does not work. Fix it so that it does.
if (this.data.length > 0) {
toReturn += this.data[this.data.length-1];
}
toReturn += "]";
return toReturn;
15
CSE 12 – Basic Data Structures Paul Cao UC San Diego
System.out.println(ref);
System.out.println("Elem at index 3 is: " + ref.get(3));
System.out.println("Removing first two elements...");
System.out.println(ref.removeFirst());
System.out.println(ref.removeFirst());
System.out.println(ref);
}
}
16
CSE 12 – Basic Data Structures Notes By Paul Cao
// Somewhere else in the code… still inside Node class (can access next)
Node<Integer> n1 = new Node<Integer>(new Integer(5));
Node<Integer> n2 = new Node<Integer>(new Integer(11));
n2.next = n1;
}
LinkedList Implementation
• Linked Lists are implemented with a Node class.
• The Node forms the structure of the list. It contains:
• A reference to the data stored at that position in the list
• A reference to the next node in the list
• Optionally (for a doubly linked list (PA3!!)) a reference to the previous node in the list.
• The Linked List itself usually contains only a reference to the first node in the list (head), and sometimes a reference
to the last node (tail). It also might store the list’s size.
Singly Linked List with dummy Node
17
CSE 12 – Basic Data Structures Notes By Paul Cao
Complete the add function form SinglyLinkedList class (Assume we have dummy nodes)
ref.add(1, item)
}
18
CSE 12 – Basic Data Structures Notes By Paul Cao
Types of Linked List
• Single linked list
4.2 - Iterator
Iterators in List https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/ListIterator.html
Imagine that you are working in the MyListIterator class. How would you access the field marked by Paul from
the iterator object (this)? (for this question, assume Node's next and prev are public)
A. You can’t
B. current.prev
C. this.right.next
D. this.right.prev
E. this.right.next.prev
19
CSE 12 – Basic Data Structures Notes By Paul Cao
D. The item stored in left.data
E. The method would throw a NoSuchElementException
What instance variables would potentially update their values in it during a call to it.next()?
A. left, right
B. left, right, idx
C. right, idx
D. left, right, idx, canRemove
E. Other
If canRemove is true but forward is false, which node would be removed with a call to it.remove()?
A. The node with the element 1
B. The node with the element 2
C. A different node
D. This would throw an IllegalStateException
20
CSE 12 – Basic Data Structures Notes By Paul Cao
Linked List Iterator Worksheet
import java.io.*;
import java.util.*;
interface CSE12List<E>{
public void insert(int index, E element);
public void print();
//other functions such as remove, find, etc
}
class FriendList<E> implements CSE12List<E>{
//Inner class that is a node
class Node{ //inner class
E data;
Node next;
public Node(){
data = null;
next = null;
}
public Node(E data, Node before){
if(before != null){
___________________________________//assign data
___________________________________//link to the element behind
___________________________________//link from before
}
}
public Node Next(){
return next;
}
}
size = 0;
}
//insert method
public void insert(int index, E element){
if (index < 0 || index > size){
return;
}
if (element == null){
return;
}
//loop through the list to find the location
Node curr = ___________________________;//start from the beginning
for (int i = 0; i < index; i++){
________________________________________//move ahead
}
__________________________________________//insert the new node
size++;
}
//print method
public void print(){
if (size == 0){
return;
}
Node curr = head;
for (int i = 0; i < size; i++){
curr = curr.Next();
if (curr!= null){
//can be useful for debugging
System.out.println(curr+" "+curr.data+" "+curr.Next());
}
}
}
24
CSE 12 – Basic Data Structures Notes By Paul Cao
Chapter 5 - Runtime Analysis
It includes approaches that computer science people compare ideas.
Approach 1: Bench mark
• code things up and run it
• E.g. implement a list using array or nodes connected by references
• Pro and con of this approach?
Imagine that this list is implemented as an array and that there are currently 20 songs in my playlist, but now
you can’t see what any of them are.
How many places do I have to look to determine whether “Riptide” is in my playlist in the BEST case?
A. 1
B. 10
C. 15
D. 20
E. Other
How many places do I have to look to determine whether “Riptide” is in my playlist in the WORST case?
A. 1
B. 10
C. 15
D. 20
E. Other
One part of figuring out how long a program takes to run is figuring out how “lucky” you got in your input.
• You might get lucky (best case), and require the least amount of time possible
• You might get unlucky (worst case) and require the most amount of time possible
• Or you might want to know “on average” (average case) if you are neither lucky or unlucky, how long does
an algorithm take.
In CSE 12 when we do analysis, we are doing WORST CASE analysis unless otherwise specified.
Exercise: How many instructions do you have to execute to find out if the element is in the list in the worst
case, if n represents the length of the list?
if ( theList[i].equals( toFind ))
return true;
}
return false;}
25
CSE 12 – Basic Data Structures Notes By Paul Cao
f3 f2 ∈ O(f1)
A. True B. False
f2 f1 ∈ O(f2)
A. True B. False
Common Big-O confusions when trying to argue that f2 is O(f1):
f1 f1 ∈ O(f3)
• What if we multiply f2 by a large constant, so that c*f2 is larger than f1 ?
A. True B. False
• What about when n is less than 10? Isn’t f2 larger than f1?
f3 ∈ O(f1)
Steps for calculating the asymptotic bound on code or algorithms
A. True B. False
1. Count the number of instructions in your code (or algorithm) as precisely as possible as a function of n,
which represents the size of your input (e.g. the length of the array). This is your f(n).
• Make sure you know if you are counting best case, worst case or average case – could be any of these!
2. Simplify your f(n) to find a simple g(n) such that the asymptotic notation is found.
3. In general, the dominant term matters the most
26
CSE 12 – Basic Data Structures Notes By Paul Cao
Commonly used runtime classes
Exercise: Answer these questions and we don’t use the tight bound assumption.
27
CSE 12 – Basic Data Structures Notes By Paul Cao
Big- Ω Notation
We say a function f(n) is “big-omega” of another function g(n), and write f(n) ∈ Ω(g(n)), if there are positive
constants c and n0 such that:
• f(n) ≥ c g(n) for all n ≥ n0.
In other words, for large n, can you multiply g(n) by a constant and have it always be smaller than or equal to
f(n)
f3 f ∈ Ω (f )
2 1
A. True B. False
f2 f1 ∈ Ω (f2)
A. True B. False
f1 f1 ∈ Ω (f3)
A. True B. False
f3 ∈ Ω (f1)
A. True B. False
28
CSE 12 – Basic Data Structures Notes By Paul Cao
Big-θ notation
• Tight bound on a function.
• If f(n) ∈ O(g(n)) and f(n) ∈ Ω(g(n)), then f(n) ∈ θ(g(n)).
• Basically it means that f(n) and g(n) are interchangeable
• Examples:
• 3n+20 = θ(10n+7)
• 5n2 + 50n + 3 = θ(5n2 + 100) f2 ∈ θ (f1)
f3 A. True B. False
f1 ∈ θ (f2)
Notes about asymptotic notations A. True B. False
• Sometimes people say, “This algorithm
f2 is O(n2)” when it would be more precise to say that it is θ(n2)
• They are intending to give a tight bound,f1but
∈ θuse
(f3)the looser “big-O” term instead of the “big-θ” term that
actually means tight bound A. True B. False
f1
• Not wrong, but not as precise
f3 ∈ θ (f1)
A. True B. False
29
CSE 12 – Basic Data Structures Notes By Paul Cao
30
CSE 12 – Basic Data Structures Notes By Paul Cao
5.1 - Iterator and Runtime Worksheet
1. When we examine an iterator with instance variables left,right, idx, canRemove, and forward, which
variables are modified by the following function calls to an iterator object?
• next
• previous
• remove
• add
2. If we have the initial state of a doubly linkedlist with an iterator, please draw the correct linkedlist and iterator after the
given sequential function calls
• next(). Also what is returned from
this function call?
• add(“w”).
• remove().
3. Compare the runtime. Look at the two blocks of code and select the correct choice for list traversal. What can you about
the speed?
//Block 1
LinkedList<Integer> myL = new LinkedList<Integer>();
// Add n elements to myL (code not shown)
ListIterator<Integer> it = myL.listIterator();
while (it.hasNext()) {
System.out.println(it.next());
}
//Block 2
LinkedList<Integer> myL = new LinkedList<Integer>();
// Add n elements to myL (code not shown)
for (int i = 0; i<myL.size(); i++) {
System.out.println(myL.get(i));
}
31
CSE 12 – Basic Data Structures Notes By Paul Cao
4. Answer True or False for the following statements. You can assume that O notation means the tightest bound
unless otherwise specified.
• If f(n) is O(n), then we can definitely say f(n) is O(n^2) (not the tightest bound here)
• If f(n) is O(n), and g(n) is O(n^2), then we say f(n) is better than g(n) (not the tightest bound here)
• The O-notation cares about the efficiency when n is large
• In CSE 12, we assume that we consider the worst case by default.
• When we have an array and we will insert an element in the array, the runtime is O(n)
• When we try to search for an element in an array, the runtime is O(n^2)
• When we try to delete an element in an array, the runtime is O(1)
• When we try to insert an element into a linked list, the cost is O(1)
• When we try to search for an element in a linked list, the cost is O(n)
• When we try to delete an element in a linked list, the cost is O(1)
b).
c).
for (int i = n; i > 0; i/=2){
for (int j = 0; j < i; j++){
x++;
}
}
d).
for (int i = 0; i < n; i++){
arr[i] = foo(i);
}
....
int foo(int i){
int total = 0;
for (int j = 0; j < i; j++){
total += arr[i];
}
return total;}
32
CSE 12 – Basic Data Structures Notes By Paul Cao
Chapter 6 - Hashing
It is a technique for fast dictionary operations.
Motivation #1
Consider the 2-sum problem: Given an unsorted array (A) of N integers between 0 and 1,000,000, find all pairs
of elements that sum to a given number T
Method 2:
Method 1: bool Hashtable[1000001]={false};
for (i = 0; i < N; i++){ for (i = 0; i<N; i++){
for (j = i;j < N; j++){ Hashtable[A[i]]=true;
if ((A[i]+A[j])==T) }
store (A[i], A[j]); for (i = 0; i<N; i++){
} if (Hashtable[T-A[i]]) == true)
} store (A[i], T-A[i]);
}
Take away:
Motivation #2
We have a student record system (i.e. ArrayList) and we will try to insert/delete/search for student
class Student{ public class StudentRecord{
int pid;//unique ArrayList<Student> data;
double gpa; int capacity;
public Student(int pid, double gpa){ int size;
this.pid = pid; // ctor etc
this.gpa = gpa; public boolean search(Student key){
} for (int i = 0; i < size; i++){
public boolean equals(Object o){ if (data.get(i).equals(key)){
//compare if two students are the same return true;
if (o == null){ }
return false; }
} return false;
if (o.getClass() != Student.class){ }
return false; }
}
Student ref = (Student)o;
if(this.pid == ref.pid
&& this.gpa == ref.gpa) return true;
else return false;
}
}
What will affect how fast I can search for this key?
A. It depends on the capacity
B. It depends on where the element is in the list
C. It depends on the size
D. A combination of some factors above
33
CSE 12 – Basic Data Structures Notes By Paul Cao
34
CSE 12 – Basic Data Structures Notes By Paul Cao
Hash code
public int hashCode()
Returns a hash code value for the object. This method is supported for the benefit of hash tables such as those
provided by HashMap.
• Whenever it is invoked on the same object more than once during an execution of a Java application,
the hashCode method must consistently return the same integer, provided no information used
in equals comparisons on the object is modified. This integer need not remain consistent from one
execution of an application to another execution of the same application.
• If two objects are equal according to the equals(Object) method, then calling the hashCode method
on each of the two objects must produce the same integer result.
• It is not required that if two objects are unequal according to the equals(java.lang.Object) method,
then calling the hashCode method on each of the two objects must produce distinct integer results.
However, the programmer should be aware that producing distinct integer results for unequal objects may
improve the performance of hash tables.
As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for
distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but
this implementation technique is not required by the Java™ programming language.)
In general, what is the difference between the value returned by hashCode() and the index
location where the item ends up in a particular hash table?
A. Nothing. The value returned by hashCode can be used directly as the index for the item in
any hash table
B. The value returned by hashCode might be larger than the size of the hash table
C. The hashCode function must return different values if two objects are different.
D. The hashCode function might return different values for two objects that are considered
equal (and for hash tables, two values that are considered equal must have the same
hashcode/index value)
Collision resolution
Separate chaining
Insert the numbers below into a hash table of size M=11 using the hash function H(k)=k % M. Perform each of
the following:
1. Separate chaining collision resolution without resizing What is the load factor?
2. Separate chaining collision resolution WITH resizing with the 8th element is inserted (double M to 22).
What is the final load factor?
35
CSE 12 – Basic Data Structures Notes By Paul Cao
Linear probing
{22, 32, 43, 11, 12, 35, 24, 7} . Use h(k) = k%11
Hash function
• To be useful, a hash function must be fast
• Its performance should not depend on the particular key.
• Runs in "constant time" (more on this later…)
• A hash function must also be deterministic:
• Given the same value, it must always return the same array index. (Otherwise, how would we find
something we stored earlier?)
• A “good” hash function should also be uniform:
• Each “slot” i in the array should be equally likely to be chosen as any other slot j.
36
CSE 12 – Basic Data Structures Notes By Paul Cao
Chapter 7 - Stack and Queue
Stacks and Queues are both linear structures. But they are different from the list.
• You shouldn’t access items in the stack or queue other than the ends
• Stack is first in, last out.
• Queue is first in, first out.
• Both can be simulated using a list (can be linked list or array list)
• Pop
To put-off, get or remove some item from top of the stack is the
pop operation.
• Peek
Looks at the object at the top of this stack without removing it
from the stack.
• IsEmpty
Stack considered empty when there is no item on top. IsEmpty
operation return true when there is no item in stack else false.
==============================================================================
Queue (ADT) • Enqueue (offer in java)
To insert an item into the queue
• Peek
Retrieves but does not remove the head of the queue
import java.util.*;
public class QueueStackDemo {
public static void main(String[] args){
Queue<String> names = new LinkedList<>();
names.offer("cse");
names.offer("12");
names.offer("solis107");
System.out.println(names.peek());
names.poll();
System.out.println(names.poll());
38
CSE 12 – Basic Data Structures Notes By Paul Cao
2. You are given a 2D maze and you start at a location (specified by (r, c)). There are blocking stones at certain
locations of the maze. You should find the exit and print the path from start to the exit. Or you should report
that a path doesn’t exist.
The maze is given using the following format. Assume n and m is <= 1000
n m k
k rows of pairs indicating the block locations
r, c indicating the start location
r, c indicating the exit location
39
CSE 12 – Basic Data Structures Notes By Paul Cao
DFS and BFS code
40
CSE 12 – Basic Data Structures Notes By Paul Cao
41
CSE 12 – Basic Data Structures Notes By Paul Cao
42
CSE 12 – Basic Data Structures Notes By Paul Cao
Chapter 8 - Priority Queue (Heap)
There are many correct ways to implement a Priority Queue,
but they vary in their efficiency
1. Sorted array
• Always insert based on the priority sorted order
• Remove from the back
2. Unsorted linked list
• Insert new element in front
• Remove by searching list for highest-priority item
3. Sorted linked list
• Always insert new elements where they go in priority-sorted order
• Remove from front
Runtime analysis
Enqueue Dequeue
Sorted array
Unsorted LinkedList
Sorted LinkedList
Heap
1. Heaps are one kind of binary tree.
2. A Binary Tree is linked structure where:
• each node has at most two links pointing to other nodes
• each node has one node pointing to it, except for the root, which has 0
3. They have a few special restrictions, in addition to the usual binary tree requirements:
• Must be complete. This means all levels must be full, except for the last level, which must be filled in from
left to right.
• Ordering of data must obey heap property
• Min-heap version: a parent’s data is always ≤ its children’s data
• Max-heap version: a parent’s data is always ≥ its children’s data
43
CSE 12 – Basic Data Structures Notes By Paul Cao
Tree, Binary Tree, Complete Binary Tree, Full Binary Tree
• Tree: a connected acyclic graph
• Binary tree: each node has at most 2 children
• Complete binary tree: a binary tree in which every level, except possibly the last, is completely filled and
all nodes in the last level are as far left as possible
• Full binary tree: a binary tree where every node has 0 or 2 children.
We actually do NOT typically use a node object to implement heaps. Because they must be complete, they
fit nicely into an array, so we usually do that
Size = 9
1. In the heap above, the value 14 is at index 5. At what index is 14's parent?
2. In the heap above, the value 27 is at index 8. At what index is 27's parent?
3. In general, if a value in the heap (that starts at index 1) is stored at index i, at what index can you find the
value's parent, in terms of i?
A. i-2 B. i/2 C. (i-1)/2 D. 2*i
4. In general, if a value in the heap (that starts at index 1) is stored at index i, at what index can you find the
value's LEFT child, in terms of i?
A. i+1 B. i+2 C. 2*i+1 D. 2*i
5. In general, if a value in the heap (that starts at index 1) is stored at index i, at what index can you find the
value's RIGHT child, in terms of i?
A. i+1 B. i+2 C. 2*i+1 D. 2*i
8. What value is the direct parent of the red location in the array? Assume that cells 1, 2 and 7 all store values
that are not shown. Assume the heap is 1-indexed
45
CSE 12 – Basic Data Structures Notes By Paul Cao
9. In what cells would you find the left child of the red cell? Assume that the heap is 1-indexed and it is fully
occupied to index 10.
Heap implementation
Remove
Discuss
What node will be removed? How should we remove it while maintaining heap property?
46
CSE 12 – Basic Data Structures Notes By Paul Cao
Insert
Try to insert the following data (these insertions are not sequential to each other). Only use the array to
manipulate. Don’t draw the tree.
16 8 3
47
CSE 12 – Basic Data Structures Notes By Paul Cao
Runtime of heap operations
Insert
Remove
Heapify (runtime?)
48
CSE 12 – Basic Data Structures Notes By Paul Cao
Proof of Heapify runtime
49
CSE 12 – Basic Data Structures Notes By Paul Cao
Chapter 9 - Trees
Tree, Binary Tree, Complete Binary Tree, Full Binary Tree
• Tree: a connected acyclic graph
• Binary tree: each node has at most 2 children
• Complete binary tree: a binary tree in which every level, except possibly the last, is completely filled and
all nodes in the last level are as far left as possible
• Full binary tree: a binary tree where every node has 0 or 2 children.
class TNode{
________ left;
________ right;
Integer value;
}
What should be the type of left and right?
A. Integer
B. Object
C. TNode
D. Anything that implements Comparable
interface
E. Something else
50
CSE 12 – Basic Data Structures Notes By Paul Cao
9.1 – Binary Tree
Practice: Inside the BinTree class, please write the contains method.
// Return true if toFind is in the Tree. We will use recursion here
public boolean containsHelper(________________________________) {
}
What is the WORST CASE cost for doing find() in a Tree (tightest Big-O, on this and future questions)?
A. O(1)
B. O(log n)
C. O(n)
D. O(n log n)
E. O(n2)
51
CSE 12 – Basic Data Structures Notes By Paul Cao
public class BinaryTree {
/** Inner class to represent the nodes in a Binary Tree */
class TNode {
TNode left;
TNode right;
Integer value;
public TNode(Integer value)
{
this.value = value;
}
}
/** Build a complete binary tree from the values in the array */
public BinaryTree(int[] values)
{
52
CSE 12 – Basic Data Structures Notes By Paul Cao
/** Print the tree with a level order traversal. */
public void printTree()
{
}
public static void main(String[] args)
{
int[] values = {42, 32, 65, 30, 38};
BinaryTree bTree = new BinaryTree(values);
bTree.printTree();
}
53
CSE 12 – Basic Data Structures Notes By Paul Cao
9.2 - Binary Search Tree
A binary tree where the key in each node must be greater than or equal to any key stored in the left sub-tree, and
less than or equal to any key stored in the right sub-tree
54
CSE 12 – Basic Data Structures Notes By Paul Cao
BST Operations
Insert
Try to insert 42 Try to insert 10 Try to insert 80
55
CSE 12 – Basic Data Structures Notes By Paul Cao
Remove from BST
Find the node while keeping track of the parent of the node you are about to visit
Delete the node
1. Node to delete is a leaf node
Traversal of a BST
Inorder
Preorder
Postorder
56
CSE 12 – Basic Data Structures Notes By Paul Cao
9.3 - Tree and BST worksheet
1. Select True or False for the following statements about binary search trees. Assume n is the number of nodes in the tree
1. Binary search tree must be a complete tree
2. Binary search tree’s height will be O(log n)
3. A heap is a binary search tree
4. A binary search tree always have about half of the nodes as leaves
5. A binary search tree always has n-1 edges
6. The leftmost leaf of a binary search tree will be the minimal of the entire tree
7. If the successor function is put in the BSTNode class, then the class should have a parent reference.
8. The root of a binary search tree will be the median of all the nodes in the tree.
9. There is only one BST for the following data [12, 13, 9, 22, 5].
10. We can’t do in order traversal on a binary tree but can do it for a binary search tree.
11. If we traverse a BST using inorder traversal, we will end up with sorted data
12. If we use inorder traversal of BST, the runtime will be O(n)
2. We want to write a method to verify if a given binary tree is a binary search tree. You can create helper methods if
needed.
boolean isbst(BSTNode root){
57
CSE 12 – Basic Data Structures Notes By Paul Cao
3. Write the height method for a binary tree
}
public int heightHelper (TNode curr){
4. Given the post order traversal result and the in order traversal result of a binary tree, please print out the pre order traversal of the
tree
e.g. BACD is post order and BDAC is in order, the preorder should be DBCA
58
CSE 12 – Basic Data Structures Notes By Paul Cao
Chapter 10 - Sorting
Tree sort
Build a BST from the given data and then complete an inorder traversal of the resulting tree
[5 12 1 0 6 3 2]
59
CSE 12 – Basic Data Structures Notes By Paul Cao
Heap sort
Build a heap from the given data and then successively remove the root.
[5 2 1 0 6 3] [12 4 9 3 15 8 19 2]
Bubble sort
Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they
are in wrong order. Hence the name of bubble sort
Optimization
What is the runtime of bubble sort in the worst case for the optimized version?
A. O(n) B. O(n logn) C. O(n2) D. None of the given choices
60
CSE 12 – Basic Data Structures Notes By Paul Cao
Selection sort
Insertion sort
The input array is logically split into a sorted part (initially only the first element) and an unsorted part. We
repeated insert the first element in the unsorted part into its correct position by swapping.
Use insertion sort to sort [12 4 9 3 15 8 19 2]
61
CSE 12 – Basic Data Structures Notes By Paul Cao
Merge Sort: An Example
}
Quicksort
Given an array, select a pivot value. Then partition the elements in the array into two parts
• left part is smaller or equal to pivot
• right part is bigger than or equal to pivot
Then recurse on both parts
63
CSE 12 – Basic Data Structures Notes By Paul Cao
Quicksort runtime
How many levels will there be if you choose a pivot that divides the list in half?
A. 1 B. logn C. n D. n logn E. n2
Which of these choices would be the worst choice for the pivot?
A. The minimum element in the list
B. The last element in the list
C. The first element in the list
D. A random element in the list
64
CSE 12 – Basic Data Structures Notes By Paul Cao
Counting sort
Given an array of non-negative integers, A, whose max value is at most k. Sort the array.
sort A=[1, 3, 2, 6, 6, 5, 3]
65
CSE 12 – Basic Data Structures Notes By Paul Cao
Radix sort
Sort integers starting from the least significant digit to the most significant digit.
Input: A[N], k = max num of digits for all A[i]
Output: A[N] sorted
for digit from 0 to k-1:
shuffle A based on digit (counting sort)
66
CSE 12 – Basic Data Structures Notes By Paul Cao
Chapter 11 - CSE 12 Reviews
1. Java generics
• generic class and function (How to write, how to call)
• various containers in Java (ArrayList, LinkedList, Deque, Stack, Queue, PriorityQueue, HashSet,
TreeSet, HashMap, etc)
• Exception handling in Java
• ADT vs Backend Data Structure
2. ArrayList
• Implementation of ArrayList using arrays
• Basic operation: add, remove, find. Understand the meaning of capacity in ArrayLists
• The efficiency of basic ArrayList operations
3. LinkedList implementation
• Definition of Singly and Doubly linked lists
• Sentinel nodes and their purposes
• Basic operations for LinkedLists (add, remove, search) and their efficiencies
• Iterators in LinkedLists (next, previous, hasNext, hasPrevious, canRemove status, forward status)
4. Hashing
• Idea of Hashing (use array as backend storage), rehashing, and load factor
• Hash function (basic hashing functions)
• Collision resolution (separate chaining and linear probing)
• Efficiency of basic hashing (insert, delete, search)
5. Deque implementation
• How to implement a Deque (front, back, insert at the front, insert at the back, remove, iterator,
search)
• basic queue adaption from Deque
• basic queue operations (enqueue, dequeue, peek)
o application of queue for BFS
• basic stack adaption
• basic stack operation (push, pop, peek)
o application of stack for expression evaluation
o application of stack for DFS
6. Priority queue
• Complete binary tree and definition of a heap
• Array-based heap implementation (parent and children positions for both situations where heap
elements start at index 0 or 1, percolate down and bubble up operations, heapify an array)
• Priority queue (push, pop, peek)
7. Tree
• Definition of trees (node, child, leaf, internal nodes, height, full tree, complete tree)
• Binary tree
• BFS, DFS on binary trees
• insert, delete, and search in trees both recursively and iteratively
• Binary search tree
• definition of a BST
• Insert, delete, search
• three order of traversals
8. Runtime analysis
• Wall clock benchmark vs asymptotic notation
67
CSE 12 – Basic Data Structures Notes By Paul Cao
• Big O formal and intuitive definitions
• Worst case and best case
• Analysis of a code’s runtime in tightest bound
• Analysis of runtime for data structures we have implemented
9. Sorting and Searching
• Comparison based sorting (bubble, insertion, selection, heapsort, treesort, merge, quick, counting,
radix)
• Runtime analysis of sorting algorithms for best case and worst case
68
CSE 12 – Basic Data Structures Notes By Paul Cao
Chapter 12 - Review exercises
1. Java Generics
We want to write a generic class whose generic type implements the Comparable interface. Complete the
findMin function
import java.util.*;
class GenericTest<E extends Comparable> {
ArrayList<E> ref;
GenericTest(ArrayList<E> data){
this.ref = data;
}
public E findMin(){
E min = _________BLANK A__________;
int size = ref.size();
for (int i = 0;i < size; i++){
if(_________BLANK B__________> 0){
min = _________BLANK C__________;
}
}
return min;
}
2. ArrayList
In our PA, we wrote our own ArrayList using an array as the backend data structure. Please state true or false on
the following statements about ArrayLists in the context of our implementation in CSE 12. Assume n is the
capacity of the backend array.
a). To insert into the beginning of an ArrayList, the worst-case cost is O(n) when the backend array is currently
full and we need to resize the array.
b). To insert into the end of an ArrayList, the best case cost is O(n).
d). To find something in an ArrayList, the worst case is O(n) when we can’t find the element we search for.
e). The cost to access an element in the ArrayList given an index is constant time due to the properties of arrays
in Java.
3 Iterators
Please answer true or false for the following statements about iterators
a). We can only have iterators for linked lists, not for array lists.
b). For linked lists, it has the same efficiency to traverse a linked list using an iterator and to traverse using
indices.
c). For linked lists, we can only delete a node using an iterator when the previous call to the iterator is either
previous or next (assume the only methods in this iterator are previous, next, and remove).
d). We usually make an iterator class an inner class of a linked list class so it has access to private data in the
linked list class.
69
CSE 12 – Basic Data Structures Notes By Paul Cao
4 Linked List
Please look at the following code on connecting nodes together and answer the questions.
class Node{
Integer data;
Node next;
public Node(Integer theData) {
data = theData;
next = this;
}
public Node(Integer theData, Node other) {
data = theData;
this.next = other;
}
public Node(Node other, Integer theData){
data = theData;
this.next = other.next;
other.next = this;
}
}
public class P3{
public static void main(String[] args){
Node n1 = new Node(7);
Node n2 = new Node(12, n1);
Node n3 = new Node(n2, 15);
Node n4 = new Node(18, n1);
}
}
Please write true or false about if the two references listed in the choices refer to the same object.
a). n1 n2.next b). n2 n3.next
c). n3 n4.next d). n1.next n1
e). n2.next.next n1 f). n4.next n3.next
g). n4.next.next n1.next h). n2 n3
5 Hashing
We have the following data to hash and please answer the questions. It will be helpful to draw out the hash table
with values in it before answering the questions. We won’t assign any points to the hashtable you filled in. The
size of the table is 13 (cells numbered from 0 to 12). We have given you the hash values of the data. The hash
value is given in the parenthesis after each data point, e.g. 65 (0) means the data to insert is 65 and its hash
value based on the hash function is 0. You don’t have to worry about how the hash values are calculated.
Please state True or False for the following questions. You don’t have to justify your answer.
Please state True or False for the following questions. You don’t have to justify your answer.
a). 36 and 42 are in the same linked list
b). When inserting 25, there isn’t any collision
c). When inserting 36, there isn’t any collision
d). The longest linked list in the table contains 3 values
Part 1: If we try to use DFS to go from S to E, would we use a queue or a stack as the data structure?
_________________
Part 2: Run DFS on the maze from location S and find out all the cells that have been visited (i.e. pushed or
enqueued into the data structure). Then answer true or false for the following statements
71
CSE 12 – Basic Data Structures Notes By Paul Cao
a). Cell (1, 3) is the first to be inserted into the data structure after S is removed from the data structure.
b). Cell (3, 0) is inserted after cell (2, 0) has been removed from the data structure
c). Cell (2, 3) is the last cell to be removed before the exit is found (i.e. found the exit cell means the exit cell is
inserted into the data structure)
d). Cell (2, 1) never got removed from the data structure before the exit is found.
e). DFS may not yield the shortest path from the start to the exit but for this problem, it does yield the shortest
path.
Part 3: Run BFS on the maze and find out all the cells that have been visited (i.e. pushed or enqueued into the
data structure). Then answer true or false for the following statements
a). Cell (1, 3) is the first to be inserted into the data structure after S is removed from the data structure.
b). Cell (3, 0) is inserted after cell (2, 0) has been removed from the data structure
c). Cell (2, 3) is the last cell to be removed before the exit is found (i.e. found the exit cell means the exit cell is
inserted into the data structure)
d). Cell (0, 1) never got removed from the data structure before the exit is found.
e). BFS yields the shortest path from the start to the exit.
Part 1. If we convert this array into a complete tree, please state whether the following statements are true or
false.
a). Data 3 is a child of data 2
b). The root of the tree is data 15
c). The right child of data 9 is data 5
d). The tree has 3 leaves nodes and 4 parent nodes
72
CSE 12 – Basic Data Structures Notes By Paul Cao
Part 2. After heapifying the array, one of the students ends up with the following structure. Please state whether
the following statements are true or false.
a). Given these data in the heap, there is only one possible configuration of a min-heap
b). In fact, the given structure isn’t a correct min-heap.
Part 3. Starting from the following min-heap, we insert a 3, insert a 2, and remove 1. Please answer the
following questions about the resulted heap after the operations.
Part 2. For a binary tree, if we are given the root of the tree, we can traverse a tree using pre-order, in-order, or
post-order. Please answer the following questions if we traverse the following binary trees
73
CSE 12 – Basic Data Structures Notes By Paul Cao
a). If we do in-order traversal of the tree from the root 5, 3 will be printed out earlier than 5
b). If we do in-order traversal of the tree from the root 5, 4 will be printed out earlier than 8
c). If we do pre-order traversal of the tree from the root 5, 3 will be printed out earlier than 5
d). If we do pre-order traversal of the tree from the root 5, 3 will be printed out earlier than 4
e). If we do post-order traversal of the tree from the root 5, 3 will be printed out earlier than 5
f). If we do post-order traversal of the tree from the root 5, 4 will be printed out earlier than 8
Part 3. Given a BST node, please complete the blanks in the following code that will return the successor of this
node.
9 Sorting
Please answer true or false about various sorting algorithms we learned in class
a). Quick sort and merge sort have the same worst case runtime asymptotically.
b). Merge sort always splits the list roughly in half and recursively sorts each half.
c). Tree sort and quicksort have the same worst case runtime asymptotically.
74
CSE 12 – Basic Data Structures Notes By Paul Cao
d). Heap sort and merge sort have the same worst case runtime asymptotically.
e). Bubble sort and insertion sort have the same worst case runtime asymptotically.
10 Efficiency analysis
What is the running time of the following algorithm? Write your answer in the tightest O-notation possible.
Part 1
for (int i = 1; i < n; i++){
for (int j = i; j < n; j *= 2){
System.out.println("one");
}
for (int j = n ; j > 0; j--){
System.out.println("two");
}
}
Part 2
int i = 1;
while(i < n){
for (int j = 0; j < i; j++){
System.out.println("one");
}
n = n / 3;
}
75