0% found this document useful (0 votes)
16 views75 pages

allNotes-v2-3-26-2025

The document is a note packet for CSE 12 - Basic Data Structures at UC San Diego, authored by Paul Cao, and last updated in March 2025. It includes a comprehensive syllabus, Java review topics, data structures such as ArrayList and Linked List, runtime analysis, and various testing methodologies including Junit testing. The document serves as a guide for course logistics, grading, academic integrity, and diversity and inclusion considerations.

Uploaded by

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

allNotes-v2-3-26-2025

The document is a note packet for CSE 12 - Basic Data Structures at UC San Diego, authored by Paul Cao, and last updated in March 2025. It includes a comprehensive syllabus, Java review topics, data structures such as ArrayList and Linked List, runtime analysis, and various testing methodologies including Junit testing. The document serves as a guide for course logistics, grading, academic integrity, and diversity and inclusion considerations.

Uploaded by

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

CSE 12 – Basic Data Structures Paul Cao UC San Diego

CSE 12 – Basic Data Structures


Note Packet
Author: Paul Cao

Last update: March 2025


1
CSE 12 – Basic Data Structures Paul Cao UC San Diego

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

Import course logistics (refer to syllabus)


Instructor: Paul Cao
Contact: EBU 3B 2102, [email protected] [https://sites.google.com/ucsd.edu/paul-cao/]

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.

Diversity and Inclusion


Students with Disabilities
Basic needs and food insecurity
Emergency Situation
• Before due date
• On the due date
• After the due date
3
CSE 12 – Basic Data Structures Paul Cao UC San Diego
Chapter 2 - Java Review
2.1 - Generics
The purpose of generics in Java is to separate data types from algorithms that apply on data types.
Generics is in general associated with data structures (CSE 12) and algorithms.
Generic Class
Create a class which has multiple generic methods (like our ArrayList, Set, HashMap classes)

public class MyGList<T> { @Override


private T[] nums; public String toString() {
public MyGList() { String str = "";
nums=null; for (T n : nums) {
} if (n instanceof Object){
public MyGList(int size) { str += (n.toString() + ", ");
this.nums = (T[])new Object[size]; }
} }
public int size() { return str;
return this.nums.length; }
} public static void main(String[] args){
public void update(int ind, T data) { MyGList<Integer> vals = new
nums[ind] = data; MyGList<Integer>(3);
} System.out.println(vals);
vals.update(2, 7);
vals.update(0, 4);
vals.update(1, 3);
System.out.println(vals);
}
}

public interface Collection<E> extends Iterable<E>


What does the <E> mean in the above code?
A. That this collection can only be used with objects of a built-in Java type called E
B. That an object reference that implements Collection can be instantiated to work with any object type
C. That a single collection can hold objects of different types

Will the main method compile?


public class Generics<T> {
private ArrayList<T> elements;
public Generics() {…}
public T add(T element) {…}
public int getNumElements() {…}
public T getLastElement() {…}
public static void main(String[] args) {
Generics<T> rr = new Generics<T>();
rr.add(1);
System.out.println("Last elem was " + rr.getLastElement());
}
}

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>{ ………}

• Important for data structures in general


public class MyList<E>{
//codes that use E }
• Type erasure during compile time
• Compiler checks if generic type is used properly. Then replace them with Object
• Runtime doesn’t have different generic types
MyList<String> ref1 = new MyList<String>();
MyList<Integer> ref2 = new MyList<Integer>();
Compile time

Runtime

Generics Worksheet
import java.io.*;
import java.util.*;
public class GenericWorksheet<E>{
public E[] data;
public GenericWorksheet(){

________________________________//Initialize data to null


}
public GenericWorksheet(E[] data){
//complete a shallow copy
________________________________________________

//what if we want to have a deep copy?


}
public E findMedian(){

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

True or False: Array is an ADT, not a data structure


A. True B. False

True or False: ADT and API are the same


A. True B. False

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

Test driven development


• first writing tests, and then writing the software that will be tested
• Increases quality of code, can increase productivity (Bissi, W., Neto, A. G. S. S., & Emer, M. C. F. P.
(2016). The effects of test driven development on internal quality, external quality and productivity: A
systematic review. Information and Software Technology, 74, 45-54)

Manual Testing Automated Testing


Executing a test cases manually without any tool support Taking tool support and executing the test cases by using an
is known as manual testing. automation tool is known as automation 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;

public CSE12() { public class CSE12Tester {


numStudents = 0; private CSE12 ref;
largeClass = true;
} @Before
public CSE12(int num) { public void setup() throws Exception{
numStudents = num; ref = new CSE12();
if (numStudents > 100) { }
largeClass = true; @Test
} else { public void ctor_default() {
largeClass = false; assertEquals(0, ref.getNum());
} }
}
public int getNum() { @Test
return numStudents; public void ctor_2() {
} ref.setNum(406);
public void setNum(int num) { assertEquals(406, ref.getNum());
numStudents = num; assertTrue("Should be a large class",
if (numStudents > 100) { ref.getLargeClass());
largeClass = true; }
} else {
largeClass = false; @Test
} public void testOfficeHour() {
} ref.setNum(90);
public boolean getLargeClass(){ assertEquals(90, ref.getNum());
return largeClass; assertFalse("Should be a small class",
} ref.getLargeClass());
public int officeHour() { assertEquals(90 / 8, ref.officeHour());
return numStudents / 10; }
} }
}

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

Please write a tester about the class.


@Test
public void testDefaultCtor(){
Person ref = new Person();

@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

• All collection classes and interfaces are in java.util package


• Collection interface provides add, remove, addAll, removeAll, retainAll, etc basic
functionalities. It also has size, contains, containsAll, and isEmpty method.

Java Collections Framework

Which of the following statements is/are correct?


A. A LinkedList "is a" List
B. An ArrayList "is a" LinkedList
C. A List "is a" ArrayList
D. A LinkedList "is a" Collection
E. More than one choices are correct

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

//Begin: Checked exception


} try {
catch(______________________ e){ checkedExceptionWithThrows();
System.out.println("Number format }
exception"); catch(FileNotFoundException e){
} System.out.println("Checked exception
//End: Catch number format exception caught");
}
//End: Checked exception
//Begin: Catch array index out of bounds }
exception private static void
try { checkedExceptionWithThrows()
// Write code to generate _________________________ {
ArrayIndexOutOfBoundsException
// Write code to throw a checked exception
FileNotFoundException
}
catch(ArrayIndexOutOfBoundsException e){
System.out.println("Array index out of }
bounds");
} }
//End: Catch array index out of bounds
exception

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.

Backend data structure: an array with a fixed size

Worksheet
public class MyArrayList<E>{
public Object[] data;
private static final int DEFAULT_CAPACITY = 5;
private int size;

public MyArrayList (){


//Initialize data to an array of Objects of size DEFAULT_CAPACITY
this.data = new Object[DEFAULT_CAPACITY];
this.size = 0;
}

/** Append an element to the end of the ArrayList


* For now, don't worry about what happens when the array is out of
* capacity, but if you have time, handle that case too.
*/
public void append(E elem)
{

/** Return the element at the specified index */


public E get(int index) {

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.

String toReturn = new String("[");


for (int i = 0; i < this.data.length-1; i++) {
toReturn += this.data[i] + ", ";
}

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

public static void main(String[] args){


MyArrayList<Integer> ref = new MyArrayList<Integer>();
ref.append(5);
ref.append(12);
ref.append(23);
ref.append(7);
ref.append(20);

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

Chapter 4 - Linked List


4.1 - Linked List
Idea

Draw the memory model for the following code


public class Node<E> {
E data;
Node next;
/** Constructor to create a Node with next set to be null */
public Node(E element){
data = element;
next = null;
}

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

What type is head in the


MySinglyLinkedList class?
A. Node
B. E
C. MySingleLinkedList
D. String
E. LinkedList

17
CSE 12 – Basic Data Structures Notes By Paul Cao

Does this list use a sentinel node?


A. Yes
B. No
C. Not sure

Draw the memory model on the following code


class Node<E> {
E data; public static void main(){
Node next; Node<Integer> n0 =
public Node() { new Node<Integer>();
data = null; Node<Integer> n1=
next = null; new Node( new Integer(1),n0);
} Node<Integer> n2 =
public Node(E theData, Node before) { new Node( new Integer(2), n0);
data = theData; }
next = before.next;
before.next = this;
}
}

Complete the add function form SinglyLinkedList class (Assume we have dummy nodes)
ref.add(1, item)

public Node(E theData, Node before) {


data = theData;
next = before.next;
before.next = this;
}
public void add(int index, E newItem) {
//throw index out of bounds exception if index is out of bounds

}
18
CSE 12 – Basic Data Structures Notes By Paul Cao
Types of Linked List
• Single linked list

• Double linked list

• Circular linked list


• Circular double 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

What would be returned by a call to it.next()?


A. The Node referenced by right
B. The Node referenced by left
C. The item stored in right.data

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

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

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;
}
}

class FriendListIterator implements Iterator { //inner class


private int index;
private Node left;
private Node right;
private boolean canRemove;
//constructor for the iterator
public FriendListIterator() {
___________________________________//initialize left
21
CSE 12 – Basic Data Structures Notes By Paul Cao
___________________________________//initialize right
___________________________________//initialize index
___________________________________//initialize canRemove
}
//override hasNext method
public boolean hasNext() {
return ___________________________//decision based on size
}
//next method
public E next() {
E result = null;
if (size == 0){
return null;
}
if (((String)right.data).startsWith("a")){//for execise only
result = right.data;
}
if (right.Next() != null){ //move to the next element
______________________________________
______________________________________
______________________________________
______________________________________
}
return result;
}
}
//instance variables for FriendList
private Node head;
private Node tail;
private int size;

//constructor for FriendList


public FriendList() {
head = ______________________________//points to sentinel node
tail = ______________________________//points to sentinel node
_____________________________________//connect the two sentinels
//need two lines if it is doubly linked
22
CSE 12 – Basic Data Structures Notes By Paul Cao

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());
}
}
}

//iterator method to get an interator


public Iterator<E> iterator() {
return ________________________________________
23
CSE 12 – Basic Data Structures Notes By Paul Cao
}
}
public class Wk3TWorksheet {
public static void main(String[] args) {
FriendList<String> myfriends = new FriendList<String>();
myfriends.insert(0, "alice");
myfriends.insert(1, "bob");
myfriends.insert(2, "abigail");
myfriends.insert(3, "charlie");
//print entire list
myfriends.print();
//use iterator to specially print
for(__________________________________________; ______________;) {
String s = itr.next();
if (s != null)
System.out.println(s);
}
}
}

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?

Approach 2: Count the number of operations in code or pseudo-code


• 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?

boolean find( String[] theList, String toFind ) {

for ( int i = 0; i < theList.length; i++ ) {

if ( theList[i].equals( toFind ))

return true;
}
return false;}

25
CSE 12 – Basic Data Structures Notes By Paul Cao

boolean find( String[] theList, String toFind ) {


for ( int i = 0; i < theList.length; i++ ) {
if ( theList[i].equals(toFind) )
return true;
}
return false;
}
boolean mysteryFind( String[] theList, String toFind ) {
int count = 0;
for ( int i = 0; i < theList.length; i++ ) {
count = count + 1;
if ( theList[i].equals(toFind) )
return true;
}
return false;
}
Which method is faster if we benchmark them?
A. find
B. mysteryFind
C. They are about the same

Big-O notation (f1 is flat, f2 is linear, f3 is quadratic)


We say a function f(n) is “big-O” of another function g(n), and write f(n) ∈O(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 bigger than or equal to
f(n). n is the “size of your problem”

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.

Let f(n) = 3 log2 n + 4 n log2 n + n


Let f(n) = 546 + 34n + 2n2
Which of the following is true?
Which of the following is NOT a correct bound?
A. f(n) ∈ O(log2n)
A. f(n) ∈ O(log2n)
B. f(n) ∈ O(nlog2n)
B. f(n) ∈ O(nlog2n)
C. f(n) ∈ O(n2)
C. f(n) ∈ O(n2)
D. f(n) ∈ O(n)
D. f(n) ∈ O(n)
E. More than one of these
E. More than one of these

Let f(n) = 2n + 14n2 + 4n3 Let f(n) = 100


Which of the following is true? Which of the following is NOT a correct bound?

A. f(n) ∈ O(2n) A. f(n) ∈ O(2n)


B. f(n) ∈ O(n2) B. f(n) ∈ O(n2)
C. f(n) ∈ O(n) C. f(n) ∈ O(n)
D. f(n) ∈ O(n3) D. f(n) ∈ O(n100)
E. More than one of these E. None of these

27
CSE 12 – Basic Data Structures Notes By Paul Cao

Count how many times each line executes, then


which O( ) most tightly and correctly characterizes
the growth?
int maxDifference(int[] arr){ What if I change j++ to j+=2?
max = 0;
for (int i=0; i<arr.length; i++) {
for (int j=0; j<arr.length; j++) { What if we change the inner for loop to
if (arr[i] – arr[j] > max) for(int j = 0; j < 1000000; j+=2)
max = arr[i] – arr[j];
}
}
return max;
}
A. f(n) ∈ O(2n)
B. f(n) ∈ O(n2)
C. f(n) ∈ O(n)
D. f(n) ∈ O(n3)
E. Other/none/more
(assume n = arr.length)

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

What if I change int range = arr.length/100;


Count how many times each line executes, then
which θ ( ) most tightly and correctly characterizes
the growth?
int sumTheMiddle(int[] arr){
int range = 100;
int start = arr.length/2 – range/2;
int sum = 0;
for (int i=start; i<start+range; i++)
{
sum += arr[i];
}
return sum;
}
A. f(n) ∈ θ(2n)
B. f(n) ∈ θ(n2)
C. f(n) ∈ θ(n)
D. f(n) ∈ θ(1)
E. None of these
(assume n = arr.length)

29
CSE 12 – Basic Data Structures Notes By Paul Cao

Last note on best/worst/average vs O/ Ω/ θ

Which version of the problem? What kind of bound?

Best case Upper bound (O)

Average case Lower bound (Ω)

Worst case Tight bound (Θ)

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?

• next(). What is returned from this


function call?

• add(“w”).

• next(). What is returned from this


function call?

• 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)

5. What is the runtime of the following code


a).
for (int i = 0; i < n; i++){
for (int j = 0; j < i; j++){
arr[i] = arr[j] + 2;
}
}

b).

for (int i = 0; i < n; i++){


arr[i] = 0;
}
for (int i = n-1; i >=0; i--){
arr[i]++;
for (int j = i+1; j < n; j++){
arr[j]++;
}
}

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]);
}

Which approach is faster asymptotically?


A. Method 1
B. Method 2
C. They are the same

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

Can we do better than linear time?

Map interface (https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/util/Map.html )


• A map contains values on the basis of key, i.e. key and value pair.
• Each key and value pair is known as an entry.
• Contains unique keys
• Useful when you have to search, update or delete elements on the basis of a key
• Does not allow duplicate keys, but you can have duplicate values.

What is a hash map? How about hash set?

• A Hash Table is a data structure.


• Each list known as a bucket. The position of the bucket is identified by calling the hashcode() method.
A hashtable contains values based on the key.
• Contains unique elements
• Does not allow null key or value
• Offers fast insertion and searching
• They are limited in size because they are based on arrays
• Can be resized, but it should be avoided

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.

The general contract of hashCode is:

• 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?

22, 32, 43, 11, 12, 35, 24, 7

What is the runtime for insert/search/delete?

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.

public static int hashFunc(String key) {


int hashVal = key.charAt(0);
int pow27 = 1; // 1, 27, 27*27, etc

for (int j=1; j<key.length(); j++) { // left to right


int letter = key.charAt(j) – ‘a’; // char code
hashVal = (hashVal*27 + letter) % tableSize; // multiply and add
}

return hashVal; //you have to consider negative value case here


}

Last notes about hashing

• Probably most used and important data structure in CSE


• Probably in every system you’ve ever used
• Part of every modern programming language
• Remember it’s derived from Dictionary ADT
o Key, value pair (insert, delete, and search)
o Does the key exist, if so, give me the item associated with that key
o Assuming no two items have the same key
• Load factor: N/M

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)

Stack (ADT) • Push


To insert an item from Top of stack is called push operation.

• 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.

Consider doing the following operations on an initially empty stack, s:


s.push(4)
s.push(10)
s.push(13)
s.pop()
s.push(5)
What are the contents of the stack, from top (left) to bottom (right):
A. 4, 10, 13, 5 B.5, 13, 10, 4 C. 5, 10, 4 D. 5, 13, 10 E. other

==============================================================================
Queue (ADT) • Enqueue (offer in java)
To insert an item into the queue

• Dequeue (poll in java)


Retrieves and removes the head of the queue

• Peek
Retrieves but does not remove the head of the queue

Consider doing the following operations on an initially empty queue, q:


q.enqueue(4)
q.enqueue(10)
q.enqueue(13)
q.dequeue()
q.enqueue(5)
What are the contents of the queue, from front (left) to rear (right):
A. 4, 10, 13, 5 B.10, 13, 5 C. 4, 10, 5 D. 5, 10, 4 E. other
37
CSE 12 – Basic Data Structures Notes By Paul Cao

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());

Stack<Integer> nums = new Stack<>();


nums.push(99);
nums.push(22);
nums.push(11);
System.out.println(nums.peek());
nums.pop();
System.out.println(nums.pop());
}
}

Applications of stack and queue


1. Given a list of parentheses "(", ")", "[" and "]" determine if they are matched.
E.g.
( ) [ ( ) ] [ [ ] ( ) ] → matched
[ ( ) ( ) ( [ ) ] ] → unmatched
Complete the following code to judge if a string of parentheses is good.
public static boolean match(String f){

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

Input format for the maze to the left


4 4 4
0 0
0 2
2 1
2 2
2 3
3 0

Two ideas to explore the maze

Idea one: Right hand rule

Idea two: water ripple

39
CSE 12 – Basic Data Structures Notes By Paul Cao
DFS and BFS code

40
CSE 12 – Basic Data Structures Notes By Paul Cao

public class Maze{


ArrayList<String> maze;
int sr, sc;//start
int er, ec;//end

public boolean dfs(int row, int col, boolean[][] visited){

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.

Which of the following are valid heaps

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

Parent leaves: size /2


Given node i, parent: i/2
Children: left 2*i, right 2*i+1
44
CSE 12 – Basic Data Structures Notes By Paul Cao
Heap worksheet

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

6. Draw the heap represented by the array below

7. Given the heap structure, which node corresponds to


the red location in the array? We assume heap is 1-indexed

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?

TrickeDown(index) // You might need more arguments


If value at index is a leaf, return
If value at index has no children less than it, return
Swap value at index with its smaller child (at childInd)
TrickleDown( childInd )

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

BubbleUp( index ) // You might need more arguments


If value at index shouldn’t move any more, return
Swap value at index with its parent (at parentInd)
BubbleUp( parentInd )

47
CSE 12 – Basic Data Structures Notes By Paul Cao
Runtime of heap operations

Insert

Remove

Build the heap?

Successive insertion (runtime?)

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

What methods we should NOT put into the TNode class?


A. getLeftChild
B. getValue
C. setValue
D. setRightChild
E. getRoot

What should come to mind when we think of a tree

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(________________________________) {

// Return true if toFind is in the Tree. We will use iteration


public boolean contains(________________________________) {

}
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;
}
}

TNode root; // // The root of the tree


// Storing the size and the height can save time later
private int size; // The number of nodes in the tree.
private int height; // The height of the tree

/** Default constructor creates an empty tree */


public BinaryTree()
{
this.root = null;
this.size = 0;
this.height = -1;
}

/** 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

Which of the following is/are a binary search tree?

Practice: Rewrite the contains method for a BST class

boolean containsHelper (______________________________________________){

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

boolean add( E toAdd ) {


if (toAdd == null) throw new NullPointerException();
if (root == null) {
root = new BSTNode(toAdd);
return true; Which of these is/are a base case for
} addHelper?
return addHelper(root, toAdd ); A. currRoot is null
}
B. currRoot’s element is equal to toAdd
boolean addHelper( BSTNode currRoot, E toAdd )
{ C. Both A & B
D. Neither of these

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

2. Node to delete only has one child

3. Node to delete has two children

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 height(){

}
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]

What is the runtime of building the tree in the worst case?


A. O(n) B. O(n logn) C. O(n2) D. None of the given choices

What is the runtime of traversal of the tree in the worst case?


A. O(n) B. O(n logn) C. O(n2) D. None of the given choices

How about best or average cases?

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]

What is the runtime of building the heap in the worst case?


A. O(n) B. O(n logn) C. O(n2) D. None of the given choices

What is the runtime of removing the root n times?


A. O(n) B. O(n logn) C. O(n2) D. None of the given choices

How about best or average cases?

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

Given data 0-indexed array of size n.

for pass in 0 to n-1:


for index2 in 0 to n-1-pass:
if (data[index2] > data[index2+1])
swap(data[index2], data[index2+1])

Use bubble sort to sort [5 2 1 0 6 3 2]

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

While the size of the unsorted part is greater than 1


find the position of the smallest element in the unsorted part
move this smallest element to the last position in the sorted part
increase the size of the sorted part and decrement the size of the unsorted part

Use selection sort to sort [12 4 9 3 15 8 19 2]

What is the runtime of selection sort in the worst case?


A. O(n) B. O(n logn) C. O(n2) D. None of the given choices

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]

What is the runtime of insertion sort in the worst case?


A. O(n) B. O(n logn) C. O(n2) D. None of the given choices

61
CSE 12 – Basic Data Structures Notes By Paul Cao
Merge Sort: An Example

public void mergeSort( int[] toSort )


{
int mid = toSort.length / 2;
int[] firstHalf = makeList(toSort, 0, mid);
int[] secondHalf = makeList( toSort, mid, toSort.length );
mergeSort( firstHalf );
mergeSort( secondHalf );
merge( firstHalf, secondHalf, toSort );

Use merge sort to sort [12 4 9 3 15 8 19 2]

Does this mergeSort method work?


A. Yes B. No

How many split layers are there?


A. log n B. n C. n2

How many merge layers are there?


A. log n B. n C. n2

How many comparisons are made at each merge layer?


A. log n B. n C. n2 D. It depends

Complete the merge function


62
CSE 12 – Basic Data Structures Notes By Paul Cao
public void merge(int [] a, int[] b, int[] result){

}
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

Use quicksort to sort [12 4 9 6 15 8 19 2]

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

What is the runtime of quicksort if we have good partitions?


A. O(1) B. O(logn) C. O(n) D. O(n logn) E. O(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

Worst case of runtime for quicksort

Other factors on sorting


Not only do we care about runtime, but we also care about
• Space: do we need extra storage?
• stable: if we have duplicates, do we maintain the same ordering?

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.

Input: A[N], K = max(A), count[K+1]


Output: result[N]

for i from 0 to N-1:


count[A[i]]++
for i from 1 to K:
count[i]+=count[i-1]
for i from N-1 to 0:
count[A[i]]--
result[count[A[i]] = A[i]

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)

sort A=[120, 45, 55, 80, 905, 52, 3, 66]

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).

c). To find something in an ArrayList, the best case is O(1).

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.

Part 1. Linear probing.


Data to be hashed (in the order they are listed from left to right):
36 (12), 42 (12), 25 (1), 8 (10), 11 (10), 9 (10)

Please state True or False for the following questions. You don’t have to justify your answer.

a). 9 is at index 10 in the table


b). 42 has at a bigger index location compared with 11
c). 9 is at a bigger index location compared with 36
70
CSE 12 – Basic Data Structures Notes By Paul Cao
d). When inserting 11, we encounter the maximal number of collisions compared with other values
e). When deleting 8, even if we don’t set a special marker at its location, we will still be able to use the hash
table properly for this problem.

Part 2. Open hashing (i.e. separate chaining).


We insert new nodes at the end of the list.
Data to be hashed (in the order they are listed from left to right):
36 (12), 42 (12), 25 (1), 8 (10), 11 (10), 9 (10)

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

6 Stack and Queues


Given the following maze, and we assume that we always visit the neighbors in the order of east, south, west,
and north, please answer the questions. The shaded cells are not passable and we start at location S (row 0, col
3) and try to reach E (row 3, col 0). We use (row number, col number) to name each cell.

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.

7 Heap and priority queue


Given the following array, please answer the questions on how to make this array a min heap.

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.

a). Data 2 is the new root


b). Data 3 is parent of data 5
c). Data 5 has two children
d). Data 6 is a leaf node

8 Binary trees and binary search trees


Part 1. Please answer true or false for the following statements
a). A binary tree is always a binary search tree
b). A binary search tree is always a binary tree
c). To find a key in a binary tree, we compare the search key with the current node, if they aren’t the same, we
need to pick either the left substree or the right subtree to search, not both subtrees.

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.

static class MyBSTNode<K,V>{


K key; V value;
MyBSTNode<K,V> parent;
MyBSTNode<K,V> left = null;
MyBSTNode<K,V> right = null;
/*Methods you may use
public MyBSTNode(K key, V value, MyBSTNode<K, V> parent)
public K getKey()
public V getValue()
public MyBSTNode<K,V> getParent()
public MyBSTNode<K,V> getLeft()
public MyBSTNode<K,V> getRight() */

public MyBSTNode<K, V> successor(){


if(________BLANK A________){
MyBSTNode<K,V> curr = ________BLANK B________;
while(________BLANK C________){
curr = ________BLANK D________;
}
return curr;
}
else{
MyBSTNode<K,V> parent = this.getParent();
MyBSTNode<K,V> curr = this;
while(________BLANK E________){
curr = ________BLANK F________;
parent = ________BLANK G________;
}
return parent;
}
}
}

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

You might also like